java poi读取excel
Posted 亲爱的阿道君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi读取excel相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于java poi读取excel的主要内容,如果未能解决你的问题,请参考以下文章
java使用poi读取excel时,电话号码变成了科学计数法,整数变成double,怎么改过来
java如何导出excel表格,如果用poi,java代码如何实现.,求代码!!!