package com.taover.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; 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.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; 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.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 int maxExcelRowNum = 5000; 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)){ if(isRead){ wb = new HSSFWorkbook(new FileInputStream(filePath)); //2003- }else{ wb = new HSSFWorkbook(); //2003- } }else if(excel2007U.equals(fileType)){ if(isRead){ wb = new XSSFWorkbook(new FileInputStream(filePath)); //2007+ }else{ 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> 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); List> result = new ArrayList>(); if(!file.exists()){ return result; } Workbook wb = UtilExcel.getWorkbook(filepath, true); return readExcelBySheetIndex(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()){ return result; } 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); List> result = new ArrayList>(); if(!file.exists()){ return result; } Workbook wb = UtilExcel.getWorkbook(filepath, true); //创建Excel工作簿对象 DecimalFormat df = new DecimalFormat("0"); Sheet sheet = wb.getSheetAt(sheetIndex); int start = sheet.getFirstRowNum(); int end = sheet.getLastRowNum(); if(end > UtilExcel.maxExcelRowNum){ end = UtilExcel.maxExcelRowNum; } for(int i=start; i dataRow = new ArrayList(); int lastCellNum = row.getLastCellNum(); if(lastCellNum > UtilExcel.maxExcelColumnNum){ lastCellNum = UtilExcel.maxExcelColumnNum; } for(int j=0; j> data = null; try { data = UtilExcel.readExcel(filepath); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } List styleList = new ArrayList(); for(int i=0; i