Java读取excel文件数据
1.需要的jar包清单如下:
dom4j-1.6.1.jar
poi-3.5.jar
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
xml-resolver-1.2.jar
xmlbeans-2.3.0.jar
XmlSchema-1.4.3.jar
2.写个工具包,里面包含3个类,包名为com.tc.readexcel.util,源代码如下:
1)ReadExcelCertainCellForXls.java
package com.tc.readexcel.util; import java.io.File; import java.io.FileInputStream; 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 ReadExcelCertainCellForXls { /** * 从file表格文件中读取第sheetNum张表单的第row行第column列 * @param file * @param sheetNum * @param row * @param column * @return */ public String getValue(String file, int sheetNum, int row, String column){ File f = new File(file); FileInputStream is = null; HSSFWorkbook wbs = null; HSSFSheet childSheet = null; try { is = new FileInputStream(f); wbs = new HSSFWorkbook(is); childSheet = wbs.getSheetAt(sheetNum-1); readExcelCell(childSheet, row, column); } catch (Exception e) { e.printStackTrace(); } return readExcelCell(childSheet, row, column); } /** * 将指定表单的指定行列读出来 * @param sheet * @param row * @param col * @return */ private String readExcelCell(HSSFSheet sheet, int row, String col) { row = row - 1; int column = letterToNumber(col); HSSFRow xrow = sheet.getRow(row); HSSFCell cell = null; if(null != xrow) cell = xrow.getCell(column); if (null != cell) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // 数字 // return (cell.getNumericCellValue() + ""); String str = cell.getNumericCellValue() + ""; if(str.endsWith(".0")){ return str.substring(0, str.length()-2); } return (cell.getNumericCellValue() + ""); case HSSFCell.CELL_TYPE_STRING: // 字符串 return (cell.getStringCellValue() + ""); case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean return (cell.getBooleanCellValue() + ""); case HSSFCell.CELL_TYPE_FORMULA: // 公式 return (cell.getCellFormula() + ""); case HSSFCell.CELL_TYPE_BLANK: // 空值 return (""); case HSSFCell.CELL_TYPE_ERROR: // 故障 return (""); default: return ("未知类型"); } } else { return ("null"); } } /** * 将输入的字符列名转化为数字列名,注意列数是从0开始,例如 A>0 G>6 AB>27 * @param col * @return */ private int letterToNumber(String col) { col = col.toUpperCase(); char[] ch = col.toCharArray(); if (ch.length == 1) { return (int) ch[0] - 64 - 1; } else { return ((int) ch[0] - (int) 'A' + 1) * 26 + ((int) ch[1] - 64) - 1; } } }
2)ReadExcelCertainCellForXlsx.java
package com.tc.readexcel.util; import java.io.File; import java.io.FileInputStream; 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 ReadExcelCertainCellForXlsx { /** * 从file表格文件中读取第sheetNum张表单的第row行第column列 * @param file * @param sheetNum * @param row * @param column * @return */ public String getValue(String file, int sheetNum, int row, String column){ File f = new File(file); FileInputStream is = null; XSSFWorkbook wbs = null; XSSFSheet childSheet = null; try { is = new FileInputStream(f); wbs = new XSSFWorkbook(is); childSheet = wbs.getSheetAt(sheetNum-1); readExcelCell(childSheet, row, column); } catch (Exception e) { e.printStackTrace(); } return readExcelCell(childSheet, row, column); } /** * 将指定表单的指定行列读出来 * @param sheet * @param row * @param col * @return */ private String readExcelCell(XSSFSheet sheet, int row, String col) { row = row - 1; int column = letterToNumber(col); XSSFRow xrow = sheet.getRow(row); XSSFCell cell = null; if(null != xrow) cell = xrow.getCell(column); if (null != cell) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_NUMERIC: // 数字 // return (cell.getNumericCellValue() + ""); String str = cell.getNumericCellValue() + ""; if(str.endsWith(".0")){ return str.substring(0, str.length()-2); } return (cell.getNumericCellValue() + ""); case XSSFCell.CELL_TYPE_STRING: // 字符串 return (cell.getStringCellValue() + ""); case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean return (cell.getBooleanCellValue() + ""); case XSSFCell.CELL_TYPE_FORMULA: // 公式 return (cell.getCellFormula() + ""); case XSSFCell.CELL_TYPE_BLANK: // 空值 return (""); case XSSFCell.CELL_TYPE_ERROR: // 故障 return (""); default: return ("未知类型"); } } else { return (""); } } /** * 将输入的字符列名转化为数字列名,注意列数是从0开始,例如 A--->0 G--->6 AB--->27 * @param col * @return */ private int letterToNumber(String col) { col = col.toUpperCase(); char[] ch = col.toCharArray(); if (ch.length == 1) { return (int) ch[0] - 64 - 1; } else { return ((int) ch[0] - (int) 'A' + 1) * 26 + ((int) ch[1] - 64) - 1; } } }
3)CellValue.java
package com.tc.readexcel.util; public class CellValue { public String getCellValue(String file, int sheetNum, int row, String column){ if(file.endsWith(".xls")){ return new ReadExcelCertainCellForXls().getValue(file, sheetNum, row, column); }else if(file.endsWith(".xlsx")){ return new ReadExcelCertainCellForXlsx().getValue(file, sheetNum, row, column); }else{ return ""; //可以弹框显示说“您选择的不是excel文件” } } }
3.读取excel文件内容
package com.tc.test; import com.tc.readexcel.util.CellValue; public class Demo { public static void main(String[] args) { // excel路径 String path = "D:\\ING\\明细表.xlsx"; CellValue cv = new CellValue(); // 读取第2行到第9行A列的数据 for (int i = 2; i < 10; i++) { String value = cv.getCellValue(path, 1, i, "A").trim(); System.out.println(i + "----------->" + value); } } }