1. 程式人生 > >java poi讀取excel

java poi讀取excel

讀取excel logs format == -s code com erro style

POI實現java讀取excel

1.下載POI的jar包 , 雲盤下載地址: http://pan.baidu.com/s/1jH59hdk

commons-fileupload-1.3.jar
commons-io-2.4.jar
dom4j-1.6.1.jar
poi-3.10-beta2.jar
poi-ooxml-3.10-beta2.jar
poi-ooxml-schemas-3.10-beta2.jar
poi-scratchpad-3.10-beta2.jar
xmlbeans-2.3.0.jar

2.讀取excel

.xlsx 是2010版本excel 用 XSSFWorkbook處理

.xls 是2003版本excel 用 HSSFWorkbook處理

技術分享
package utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel { public static List<List<String>> readExcel(String path) throws IOException { if (path.endsWith(".xlsx")) { return readXlsx(path); } else if (path.endsWith(".xls")) { return readXlsx(path); } else { return null; } } /** * Read the Excel 2010 */ public static List<List<String>> readXlsx(String path) throws IOException { InputStream is = new FileInputStream(path); XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is); List<List<String>> list = new ArrayList<List<String>>(); // Read the Sheet int numSheets = xssfWorkbook.getNumberOfSheets();// 獲取sheet頁數 for (int numSheet = 0; numSheet < 1; numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // Read the Row int rowNums = xssfSheet.getLastRowNum(); for (int rowNum = 0; rowNum <= rowNums; rowNum++) { List<String> l = new ArrayList<String>(); // 獲取對應的行數據 XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) { // 獲取列數 int columnNum = xssfRow.getLastCellNum(); for (int cloNum = 0; cloNum < columnNum; cloNum++) { l.add(getValue(xssfRow.getCell(cloNum))); } list.add(l); } } } return list; } /** * Read the Excel 2003 */ public void readXls(String path) throws IOException { InputStream is = new FileInputStream(path); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); List<List<String>> list = new ArrayList<List<String>>(); // Read the Sheet int numSheets = hssfWorkbook.getNumberOfSheets();// 獲取sheet頁數 for (int numSheet = 0; numSheet < 1; numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // Read the Row int rowNums = hssfSheet.getLastRowNum(); for (int rowNum = 0; rowNum <= rowNums; rowNum++) { // 獲取對應的行數據 HSSFRow hssfRow = hssfSheet.getRow(rowNum); List<String> l = new ArrayList<String>(); if (hssfRow != null) { // 獲取列數 int columnNum = hssfRow.getLastCellNum(); for (int cloNum = 0; cloNum < columnNum; cloNum++) { l.add(getValue(hssfRow.getCell(cloNum))); } list.add(l); } } } } /** * 取值2010excel * * @param cell * @return */ private static String getValue(XSSFCell cell) { if (cell == null) { return ""; } String value = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { value = new SimpleDateFormat("yyyy-MM-dd").format(date); } else { value = ""; } } else { value = new DecimalFormat("0").format(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_FORMULA: // 導入時如果為公式生成的數據則無值 if (!cell.getStringCellValue().equals("")) { value = cell.getStringCellValue(); } else { value = cell.getNumericCellValue() + ""; } break; case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_ERROR: break; case HSSFCell.CELL_TYPE_BOOLEAN: value = (cell.getBooleanCellValue() == true ? "Y" : "N"); break; default: value = ""; } return value.trim(); } /** * 取值2003excel * * @param cell * @return */ private String getValue(HSSFCell cell) { if (cell == null) { return ""; } String value = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { value = new SimpleDateFormat("yyyy-MM-dd").format(date); } else { value = ""; } } else { value = new DecimalFormat("0").format(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_FORMULA: // 導入時如果為公式生成的數據則無值 if (!cell.getStringCellValue().equals("")) { value = cell.getStringCellValue(); } else { value = cell.getNumericCellValue() + ""; } break; case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_ERROR: break; case HSSFCell.CELL_TYPE_BOOLEAN: value = (cell.getBooleanCellValue() == true ? "Y" : "N"); break; default: value = ""; } return value.trim(); } }
ReadExcel.java

java poi讀取excel