JAVA操作excel

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();
}


}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值