Excel的表格的读取数据
2017-02-27 16:33
309 查看
Excel的表格读取工具类
在别人的基础上改了改,方便为了以后用到:修改过,其中在读取数值和String类型的时候会出现错误,其中读取格式的时候和数值的时候可能会出现,例如:10.32---》10.3200000000001的情况和10.24---》10.23999999等情况,将在readCell的时候需要对其中的类型进行判断一下
package com.lks.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.ArrayList; import java.util.IllegalFormatException; import java.util.List; import javax.xml.bind.ValidationException; 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; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; 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; /** * * @author lks * @2017年2月27日 * @下午3:02:53 */ public class ExcelUtil { public static final String EXCEL_XLS = ".xls"; public static final String EXCEL_XLSX=".xlsx"; public static final String FILE_SPLIT ="."; /** * 选择其中的方法 * @param path * @return * @throws Exception */ public static List<List<String>> changeExcelType(String path) throws Exception { int lastIndexOf = path.lastIndexOf(ExcelUtil.FILE_SPLIT); String excelType = path.substring(lastIndexOf); List<List<String>> list = null; if(EXCEL_XLS.equalsIgnoreCase(excelType)){ list = readXls(path); }else if(EXCEL_XLSX.equalsIgnoreCase(excelType)){ list = readXlsx(path); }else{ throw new ValidationException("上传的类型不存在!"); } return list; } /** * 创建读取Excel的文件 * * @param path * @return * @throws Exception */ public static List<List<String>> readXls(String path) throws Exception { InputStream in = new FileInputStream(new File(path)); HSSFWorkbook workbook = new HSSFWorkbook(in); List<List<String>> result = new ArrayList<List<String>>(); int sheetsNum = workbook.getNumberOfSheets(); //遍历sheet for (int i = 0; i <= sheetsNum; i++) { HSSFSheet sheet = workbook.getSheetAt(i); if (sheet == null) { continue; } int lastRowNum = sheet.getLastRowNum(); int firstRowNum = sheet.getFirstRowNum(); //遍历行 for (int j = firstRowNum + 1; j <= lastRowNum; j++) { List<String> rowlist = new ArrayList<String>(); HSSFRow row = sheet.getRow(j); if (row == null) { continue; } int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); //遍历单元格 for (int k = firstCellNum; k <= lastCellNum; k++) { HSSFCell cell = row.getCell(k); if (cell == null) { continue; } rowlist.add(readCell(cell)); } result.add(rowlist); } } return result; } /** * 读取excel的文件的尾部为xlsx * * @param path * @return * @throws Exception */ public static List<List<String>> readXlsx(String path) throws Exception { check(path); InputStream in = new FileInputStream(new File(path)); XSSFWorkbook workbook = new XSSFWorkbook(in); List<List<String>> result = new ArrayList<List<String>>(); int sheetsNum = workbook.getNumberOfSheets()-1; for (int i = 0; i <= sheetsNum; i++) { XSSFSheet sheet = workbook.getSheetAt(i); if (sheet == null) { continue; } int firstRowNum = sheet.getFirstRowNum(); int lastRowNum = sheet.getLastRowNum(); //遍历行 for (int j = firstRowNum; j <= lastRowNum; j++) { XSSFRow row = sheet.getRow(j); if (row == null) { continue; } int firstCellNum = row.getFirstCellNum(); int lastCellNum = row.getLastCellNum(); List<String> rowResult = new ArrayList<>(); for (int k = firstCellNum; k <= lastCellNum; k++) { XSSFCell cell = row.getCell(k); if (cell == null) { continue; } rowResult.add(readCell(cell)); } result.add(rowResult); } } return result; } /** * 读取单元格中的数据 * * @param cell * @return */ //判断其中的excel中的cell中的数字可能是不同的类型 public static String readCell(Cell cell) { String cellValue = ""; switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: cell.setCellType(Cell.CELL_TYPE_STRING); String temp = cell.getStringCellValue(); //当是double时 if(temp.contains(".")){ Double d = new Double(temp); cellValue = String.valueOf(d); }else{ cellValue = temp.trim(); } break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); if(cellValue.indexOf(".") > -1){ if(!NumberUtils.isNumber(cellValue)){ cellValue = cellValue.trim(); } Double d = new Double(cellValue); cellValue = String.valueOf(d); } break; case Cell.CELL_TYPE_BOOLEAN: cellValue = Boolean.toString(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: try { cellValue = String.valueOf(cell.getStringCellValue()); } catch (IllegalFormatException e) { cellValue = String.valueOf(cell.getNumericCellValue()); } if(cellValue != null){ cellValue = cellValue.replaceAll("#N/A","").trim(); } break; default: cellValue = ""; break; } return cellValue; } /** * 验证 * * @param path * @throws FileNotFoundException */ public static void check(String path) throws FileNotFoundException { File file = new File(path); if (!file.exists()) { throw new FileNotFoundException("文件不存在!"); } } public static void main(String[] args) { String path = "file/test.xlsx"; List<List<String>> lists = null; try { lists = changeExcelType(path); } catch (Exception e){ e.printStackTrace(); } for (int i = 0; i < lists.size(); i++) { List<String> list = lists.get(i); for (int j = 0; j < list.size(); j++) { System.out.println(list.get(j)); } } } }
相关文章推荐
- C#读取Excel表格 Visual C#读取Excel表格中的数据
- Windows下使用Python读取Excel表格数据
- android 读取excel表格数据,并存入数据库
- C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel
- java SpringMVC中 POI读取数据库数据并写入Excel表格中,并实现下载功能
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中
- vb.net2005 读取 excel 表格数据 填充 datagridview
- java poi读取excel表格数据
- 读取Excel表格数据存入mongodb数据库
- Java 读取Excel表格数据日期类型转换
- JXL读取写入excel表格数据
- C# Window编程随记——第三方读取Excel表格数据的插件(NPOI.dll)
- [转] Windows下使用Python读取Excel表格数据
- 读取excel表格数据
- C#如何读取Excel表格数据并显示到GridView控件
- 从Excel 电子表格中读取数据并插入到数据库的简单方式
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- 使用jxl.jar 读取excel表格数据
- Java读取Excel表格数据的基本步骤
- python初学者学习笔记(三)读取excel表格数据