POI读取Excel(兼容Excel2003、Excel2007)
生活随笔
收集整理的這篇文章主要介紹了
POI读取Excel(兼容Excel2003、Excel2007)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)自http://blog.csdn.net/jack0511/article/details/6179593,僅供學(xué)習(xí)
/*** ClassName:ExcelReader.java* Author: wenbin.ji* CreateTime: Jan 28, 2011 11:16:29 AM* Description:Excel數(shù)據(jù)讀取工具類,POI實(shí)現(xiàn),兼容Excel2003,及Excel2007**/ package com.company.utils;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; 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 ExcelReader {Workbook wb = null;List<String[]> dataList = new ArrayList<String[]>(100);public ExcelReader(String path){try {InputStream inp = new FileInputStream(path);wb = WorkbookFactory.create(inp); } catch (FileNotFoundException e) {e.printStackTrace();} catch (InvalidFormatException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}} /*** 取Excel所有數(shù)據(jù),包含header* @return List<String[]>*/public List<String[]> getAllData(int sheetIndex){int columnNum = 0;Sheet sheet = wb.getSheetAt(sheetIndex);if(sheet.getRow(0)!=null){columnNum = sheet.getRow(0).getLastCellNum()-sheet.getRow(0).getFirstCellNum();}if(columnNum>0){for(Row row:sheet){ String[] singleRow = new String[columnNum];int n = 0;for(int i=0;i<columnNum;i++){Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);switch(cell.getCellType()){case Cell.CELL_TYPE_BLANK:singleRow[n] = "";break;case Cell.CELL_TYPE_BOOLEAN:singleRow[n] = Boolean.toString(cell.getBooleanCellValue());break;//數(shù)值case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)){singleRow[n] = String.valueOf(cell.getDateCellValue());}else{ cell.setCellType(Cell.CELL_TYPE_STRING);String temp = cell.getStringCellValue();//判斷是否包含小數(shù)點(diǎn),如果不含小數(shù)點(diǎn),則以字符串讀取,如果含小數(shù)點(diǎn),則轉(zhuǎn)換為Double類型的字符串if(temp.indexOf(".")>-1){singleRow[n] = String.valueOf(new Double(temp)).trim();}else{singleRow[n] = temp.trim();}}break;case Cell.CELL_TYPE_STRING:singleRow[n] = cell.getStringCellValue().trim();break;case Cell.CELL_TYPE_ERROR:singleRow[n] = "";break; case Cell.CELL_TYPE_FORMULA:cell.setCellType(Cell.CELL_TYPE_STRING);singleRow[n] = cell.getStringCellValue();if(singleRow[n]!=null){singleRow[n] = singleRow[n].replaceAll("#N/A","").trim();}break; default:singleRow[n] = "";break;}n++;} if("".equals(singleRow[0])){continue;}//如果第一行為空,跳過(guò)dataList.add(singleRow);}}return dataList;} /*** 返回Excel最大行index值,實(shí)際行數(shù)要加1* @return*/public int getRowNum(int sheetIndex){Sheet sheet = wb.getSheetAt(sheetIndex);return sheet.getLastRowNum();}/*** 返回?cái)?shù)據(jù)的列數(shù)* @return */public int getColumnNum(int sheetIndex){Sheet sheet = wb.getSheetAt(sheetIndex);Row row = sheet.getRow(0);if(row!=null&&row.getLastCellNum()>0){return row.getLastCellNum();}return 0;}/*** 獲取某一行數(shù)據(jù)* @param rowIndex 計(jì)數(shù)從0開(kāi)始,rowIndex為0代表header行* @return*/public String[] getRowData(int sheetIndex,int rowIndex){String[] dataArray = null;if(rowIndex>this.getColumnNum(sheetIndex)){return dataArray;}else{dataArray = new String[this.getColumnNum(sheetIndex)];return this.dataList.get(rowIndex);}}/*** 獲取某一列數(shù)據(jù)* @param colIndex* @return*/public String[] getColumnData(int sheetIndex,int colIndex){String[] dataArray = null;if(colIndex>this.getColumnNum(sheetIndex)){return dataArray;}else{ if(this.dataList!=null&&this.dataList.size()>0){dataArray = new String[this.getRowNum(sheetIndex)+1];int index = 0;for(String[] rowData:dataList){if(rowData!=null){dataArray[index] = rowData[colIndex];index++;}}}}return dataArray;}}
總結(jié)
以上是生活随笔為你收集整理的POI读取Excel(兼容Excel2003、Excel2007)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网站设计的好坏对宣传的效果的影响!
- 下一篇: cent 网络设置