package com.taover.util; import java.io.File; 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.FillPatternType; 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.ss.usermodel.WorkbookFactory; 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 = 100; /** * 描述:根据文件后缀,自适应上传文件的版本 * @param inStr,fileName * @return * @throws Exception */ private static Workbook getWorkbook(String filePath, boolean isRead) throws Exception{ Workbook wb = null; File tempFile = null; if(isRead){ tempFile = new File(filePath); if(!tempFile.exists()){ throw new Exception("需要读取的文件不存在!"); } } if(isRead) { wb = WorkbookFactory.create(tempFile); }else { int dotIndex = filePath.lastIndexOf("."); if(dotIndex < 0) { throw new Exception("传入的文件没有指定扩展名"); } String filteTypeLower = filePath.substring(dotIndex).trim().toLowerCase(); if(excel2003L.equals(filteTypeLower) || CSV.equals(filteTypeLower)){ wb = new HSSFWorkbook(); //2003- } else if (excel2007U.equals(filteTypeLower)){ wb = new XSSFWorkbook(); //2007+ } 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 sheetList, List>> dataList, String path) throws Exception{ if(sheetList.size() != dataList.size()){ throw new Exception("sheet size != excel size"); } Workbook wb = UtilExcel.getWorkbook(path, false); //创建Excel工作簿对象 for (int j = 0;j< dataList.size(); j++) { List> data = dataList.get(j); Sheet sheet = wb.createSheet(sheetList.get(j));//创建Excel工作表对象 for(int i=0; i dataRow = data.get(i); if(dataRow != null){ for(int k=0; k> 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(FillPatternType.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>> readExcelAllSheetMap(String filepath) throws Exception{ File file = new File(filepath); HashMap>> map = new HashMap>>(); 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); 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); if(hasRowLimit){ 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); 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\\mmm.xlsx"; List> data = null; try { Map>> map = UtilExcel.readExcelAllSheetMap(filepath); System.out.println(map); data = map.get("0"); System.out.println(data); System.out.println(data.size()); System.out.println(UtilExcel.readExcelAllSheetMap(filepath)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } List styleList = new ArrayList(); // for(int i=0; i headerList = new ArrayList(); // headerList.add("shhe1"); // headerList.add("shhe2"); // List>> dataList = new ArrayList>>(); // List> list1 = new ArrayList>(); // List list11 = new ArrayList(); // list11.add("hahaha"); // list11.add("hahaha"); // list11.add("hahaha"); // list1.add(list11); // // List> list2 = new ArrayList>(); // List list22 = new ArrayList(); // list22.add("hahaha2"); // list22.add("hahaha2"); // list22.add("hahaha2"); // list2.add(list22); // list2.add(list22); // // dataList.add(list1); // dataList.add(list2); // // UtilExcel.saveExcelContailSheet(headerList, dataList, "C:\\Users\\gaoming\\Desktop\\qwer.xls"); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } }