您的位置:首页 > 编程语言 > Java开发

JAVA操作excel

2012-01-17 23:22 134 查看
package com.czp.xsl;

import java.io.FileInputStream;

import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ReadXSL {

public static void main(String[] args) throws Exception {
InputStream myxls = new FileInputStream("D:/开发软件/PMTool/PMTool/NeInterface/ENODEB/eBBU530V100R001C00IDC00/性能接口文档.xls");
HSSFWorkbook wb     = new HSSFWorkbook(myxls);
HSSFSheet sheet = wb.getSheetAt(0);       // 第一个工作表
int rows = sheet.getLastRowNum();
for(int i=0;i<rows;i++)
{
HSSFRow row     = sheet.getRow(i);
int rs = row.getLastCellNum();
for(int j=0;j<rs;j++)
{
HSSFCell cell   = row.getCell((short)j);
if(cell!=null)
{
System.out.println(cell.getCellType());

// if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {

//  System.out.print(cell.getStringCellValue()+"\t");

// } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)

// {

// System.out.print(cell.getNumericCellValue()+"\t");

// }else {

// System.out.print(cell.getCellNum()+"\t");

// }
}
}
System.out.println();
}

}
}

===========================

package com.czp;

import java.io.FileInputStream;

import java.io.InputStream;

import java.math.BigDecimal;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

public class XLSReader {

public static void main(String[] args) throws Exception {
InputStream myxls = new FileInputStream("F:/123.xls");
Workbook wb = WorkbookFactory.create(myxls);
Sheet sheet = wb.getSheetAt(0); // 第一个工作表
int rows = sheet.getPhysicalNumberOfRows();
for (int i = sheet.getFirstRowNum(); i < rows; i++) {
Row row = sheet.getRow(i);
int rs = row.getPhysicalNumberOfCells();
for (int j = 0; j < rs; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
System.out.println(cell.getRichStringCellValue());
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
System.out.print(cell.getStringCellValue() + "\t");
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
System.out.print(BigDecimal.valueOf(cell
.getNumericCellValue()) + "\t");
} else {
System.out.print(cell.getRichStringCellValue() + "\t");
}
}
}
System.out.println();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息