java使用POI读取excel报表

留此作为记录

 package com.demo;

 import java.io.FileInputStream;
import java.util.Iterator; 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 DemoTest {
public static void main(String[] args) {
System.out.println(readXls(System.getProperty("user.dir") + "\\case\\副本业务编码大全.xls"));
} public static String readXls(String path) {
String text = "";
try {
FileInputStream is = new FileInputStream(path);
HSSFWorkbook excel = new HSSFWorkbook(is);
// 获取第一个sheet
HSSFSheet sheet0 = excel.getSheetAt(0);
for (Iterator<?> rowIterator = sheet0.iterator(); rowIterator.hasNext();) {
HSSFRow row = (HSSFRow) rowIterator.next();
for (Iterator<?> iterator = row.cellIterator(); iterator.hasNext();) {
HSSFCell cell = (HSSFCell) iterator.next();
// 根据单元的的类型 读取相应的结果
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
text += cell.getStringCellValue() + "\t";
else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
text += cell.getNumericCellValue() + "\t";
else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
text += cell.getCellFormula() + "\t";
}
text += "\n";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return text;
}
}
上一篇:js自定制周期函数


下一篇:第二十九篇-Fragment动态用法