package com.taover.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; 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.xssf.usermodel.XSSFWorkbook; public class UtilExcel { private final static String excel2003L =".xls"; //2003- 版本的excel private final static String excel2007U =".xlsx"; //2007+ 版本的excel private final static String CSV =".csv"; //csv private final static int maxExcelRowNum = 10000; private final static int maxExcelColumnNum = 50; /** * 描述:根据文件后缀,自适应上传文件的版本 * @param inStr,fileName * @return * @throws Exception */ private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ Workbook wb = null; if(isRead){ File tempFile = new File(filePath); if(!tempFile.exists()){ throw new Exception("需要读取的文件不存在!"); } } String fileType = filePath.substring(filePath.lastIndexOf(".")); if(excel2003L.equals(fileType.trim().toLowerCase())){ if(isRead){ wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- }else{ wb = new HSSFWorkbook(); //2003- } }else if(excel2007U.equals(fileType.trim().toLowerCase())){ if(isRead){ wb = new XSSFWorkbook(new FileInputStream(filePath)); //2007+ }else{ wb = new XSSFWorkbook(); //2007+ } }else if(CSV.equals(fileType.trim().toLowerCase())){ if(isRead){ wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- }else{ wb = new HSSFWorkbook(); //2003- } } else{ throw new Exception("解析的文件格式有误!"); } return wb; } /** * 创建并保存excel表 * @param sheetName * @param data * @param path */ public static File saveExcelFromListString(String sheetName, List data, String path) throws Exception{ Workbook wb = UtilExcel.getWorkbook(path, false); //创建Excel工作簿对象 Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 for(int i=0; i> data, String path) throws Exception{ Workbook wb = UtilExcel.getWorkbook(path, false); //创建Excel工作簿对象 Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 for(int i=0; i dataRow = data.get(i); if(dataRow != null){ for(int j=0; j> data, String path, List backColorList) throws Exception{ Workbook wb = UtilExcel.getWorkbook(path, false); Map cellStyleMap = new HashMap(); Sheet sheet = wb.createSheet(sheetName);//创建Excel工作表对象 for(int i=0; i dataRow = data.get(i); Short backColor = backColorList.get(i); CellStyle style = cellStyleMap.get(backColor); if(style == null){ style = wb.createCellStyle(); style.setFillPattern(style.SOLID_FOREGROUND); cellStyleMap.put(backColor, style); } if(backColor != null){ style.setFillForegroundColor(backColor.shortValue()); } if(dataRow != null){ for(int j=0; j> readExcel(String filepath) throws Exception{ File file = new File(filepath); if(!file.exists()){ throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); return readExcelBySheetIndex(wb, wb.getActiveSheetIndex()); } /** * 读取excel表--当前激活的工作表 * @param path */ public static List> readExcel(String filepath, boolean hasLimit) throws Exception{ File file = new File(filepath); if(!file.exists()){ throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); return readExcelBySheetIndex(wb, wb.getActiveSheetIndex(), hasLimit); } /** * 读取 * @param filepath * @return * @throws Exception */ public static List> readExcelExcludeHideLine(String filepath) throws Exception{ File file = new File(filepath); if(!file.exists()){ throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); return readExcelBySheetIndexExcludeHideLine(filepath, wb.getActiveSheetIndex()); } /** * 读取excel表--所有工作表 * @param path */ public static List> readExcelAllSheet(String filepath) throws Exception{ File file = new File(filepath); List> result = new ArrayList>(); if(!file.exists()){ throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); int sheetNumber = wb.getNumberOfSheets(); for(int sheetIndex=0; sheetIndex> readExcelBySheetIndex(String filepath, int sheetIndex) throws Exception{ File file = new File(filepath); if(!file.exists()){ throw new Exception("Excel["+filepath+"]文件不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); Sheet sheet = wb.getSheetAt(sheetIndex); int end = sheet.getLastRowNum(); if(end > UtilExcel.maxExcelRowNum){ //throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); } private static List> readExcelBySheetIndex(Workbook wb, int sheetIndex) throws Exception{ return readExcelBySheetIndex(wb, sheetIndex, true); } private static List> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{ Sheet sheet = wb.getSheetAt(sheetIndex); int end = sheet.getLastRowNum(); if(hasRowLimit){ if(end > UtilExcel.maxExcelRowNum) { throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false); }else { return readExcelBySheet(sheet, Integer.MAX_VALUE, false); } } /** * 读取excel表--当前激活的工作表 * @param path */ private static List> readExcelBySheet(Sheet sheet, int rowLimit, boolean excludeRowZeroHeight) throws Exception{ List> result = new ArrayList>(); int start = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); if(end > rowLimit){ end = rowLimit; } DecimalFormat df = new DecimalFormat("0"); for(int i=start; i dataRow = new ArrayList(); int lastCellNum = row.getLastCellNum(); if(lastCellNum > UtilExcel.maxExcelColumnNum){ lastCellNum = UtilExcel.maxExcelColumnNum; } for(int j=0; j> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{ File file = new File(filepath); if(!file.exists()){ throw new Exception("Excel文件["+filepath+"]不存在"); } Workbook wb = UtilExcel.getWorkbook(filepath, true); Sheet sheet = wb.getSheetAt(sheetIndex); int end = sheet.getLastRowNum(); if(end > UtilExcel.maxExcelRowNum){ throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); } return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, true); } public static void main(String args[]){ //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; String filepath = "C:\\Users\\EDZ\\Desktop\\qwer.xlsx"; List> data = null; try { data = UtilExcel.readExcel(filepath); System.out.println(data); System.out.println(data.size()); System.out.println(UtilExcel.readExcelExcludeHideLine(filepath).size()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // List styleList = new ArrayList(); // for(int i=0; i