打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
POI解析Excel2007+
需要的jar包
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
xmlbeans-2.3.0_poi.jar
[java] view plaincopy
/**
*  Description:用poi解析excel,支持office2007 xls,xlsx
*  @author liuwei  DateTime 2013-8-26 上午10:58:16
*  @param fileName
*  @param sheetNumber
*  @return
*/
public List<String[]> readByPoi(String fileName,int sheetNumber) {
org.apache.poi.ss.usermodel.Workbook workbook = null;
List<String[]> list = new ArrayList<String[]>();
try {
String fileType=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
if (fileType.equals("xls")) {
workbook = new HSSFWorkbook(new FileInputStream(fileName));
}
else if(fileType.equals("xlsx"))
{
workbook = new XSSFWorkbook(new FileInputStream(fileName));
}
else
{
log.info("您的文档格式不正确!");
}
//创建sheet对象
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(sheetNumber);
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();//获得总列数
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
int lastCellIndex = row.getLastCellNum();
String[] s=new String[coloumNum];
for(int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex ++){
Cell cell = row.getCell(cIndex);
String value = "";
int type = cell.getCellType();
if(cell != null){
value = cell.toString();
if (Cell.CELL_TYPE_NUMERIC == type) {
//                                   value = String.valueOf(cell.getNumericCellValue());
Double val = cell.getNumericCellValue();
if(val == val.longValue()){
value= "" + val.longValue();
}
}
s[cIndex]=value;
}
}
list.add(s);
}
}
} catch (Exception e) {
//          Log.error(e.getMessage());
e.printStackTrace();
}
return list;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[JAVA]POI各Jar包的作用
java – XML-BEANS编译模式:无法找到已编译的模式资源schemaorg_apache_xmlbeans / system / sE130CAA0A01A7CDE5A2B4FEB8B3
J2EE 开发常用的jar整理
poi读写excel2007和excel2003 java
POI 导出excel2007-发现不可读取的内容(有解决方法)
Java实现从Excel文件转换成XML文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服