package com.lxyer.excel.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.*; /** * 使用poi报表导出工具类 * 把poi的一个调用接口抽出来,便于导出功能的管理 * @author LiangXianYou lxy208@126.com * @param * */ public class ExcelKit { /** * 导出list中map做载体的数据到excel
* 参数说明:
* list:存放了Map数据的集合
* hdNames:表头列名
* hds:对应表头的数据KEY
* xlsName:导出文件名
* @author LiangXianYou * @date 2015-6-25 上午10:22:51 */ public static boolean exportByMap(List> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{ Workbook wb = new XSSFWorkbook(); //创建工作薄 Sheet sheet = wb.createSheet(); //创建工作表 sheet.autoSizeColumn(( short ) 0 ); //自适应宽度 //写入表头---Excel的第一行数据 Row nRow = sheet.createRow(0); //创建行 for(int i=0;i boolean exportExcels(List> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{ Workbook wb = new XSSFWorkbook(); //创建工作薄 for(int i=0;i map = list.get(i); Sheet sheet = wb.createSheet(); //创建工作表 String sheetName = (String) map.get("sheetName"); wb.setSheetName(i, sheetName); //写入表头---Excel的第一行数据 Row nRow = sheet.createRow(0); //创建行 String[] hdNames = (String[]) map.get("hdNames"); for(int j=0;j data = (List) map.get("data"); String[] hds = (String[]) map.get("hds"); for(int j=0;j boolean exportExcel(List list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{ Workbook wb = new XSSFWorkbook(); //创建工作薄 Sheet sheet = wb.createSheet(); //创建工作表 sheet.autoSizeColumn(( short ) 0 ); //自适应宽度 //写入表头---Excel的第一行数据 Row nRow = sheet.createRow(0); //创建行 for(int i=0;i readExcelHead(File file, String[] fields, int lastRowNum) throws IOException { return readExcel(file, fields, lastRowNum, null); } //read excel sheet[0] public static List readExcel(File file, String[] fields) throws IOException { return readExcel(file, fields, -1, null); } //read excel sheet[0] no fields public static List readExcel(File file) throws IOException { return readExcel(file, null, -1, null); } //read excel by sheetName public static List readExcel(File file, String[] fields, String sheetName) throws IOException { return readExcel(file, fields, -1, sheetName); } //read excel by sheetName no fields public static List readExcel(File file, String sheetName) throws IOException { return readExcel(file, null, -1, sheetName); } //read excel all sheet public static Map> readExcelAll(File file, String[] fields) throws IOException { return readExcelAll(file, fields, -1); } //read all excel no fields public static Map> readExcelAll(File file) throws IOException { return readExcelAll(file, null, -1); } //read excel sheet[0] private static List readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException { Workbook wk; try { wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException }catch (OfficeXmlFileException e){ wk = new XSSFWorkbook(new FileInputStream(file)); } Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName); if (sheet == null) throw new OfficeXmlFileException("sheet["+sheetName+"] can't find"); return readExcel(sheet, fields, lastRowNum); } /** * read excel all sheet, * can read excel 2007+ and 2007- * @param file * @param fields * @param lastRowNum * @return * @throws IOException */ private static Map> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException { Workbook wk; try { wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException }catch (OfficeXmlFileException e){ wk = new XSSFWorkbook(new FileInputStream(file)); } Map> data = new LinkedHashMap<>(); //reading all sheet for (int i = 0; i < wk.getNumberOfSheets(); i++) { Sheet sheet = wk.getSheetAt(i); List maps = readExcel(sheet, fields, lastRowNum); //move head if (lastRowNum < 0 && maps.size() > 0){ maps.remove(0); } data.put(sheet.getSheetName(), maps); } return data; } /** * read excel * @author Lxyer 2016/8/1 10:32. */ private static List readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException { if (fields == null || fields.length == 0){ return readExcel(sheet, lastRowNum); } List list = new ArrayList<>(); if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ lastRowNum = sheet.getLastRowNum(); } int t = 0; r:for (int i=0; i<=lastRowNum; i++){ Row row = sheet.getRow(i); if (row == null) continue ; short cellNum = row.getLastCellNum(); //空跳过/连续三行为空结束 if (isEmptyRow(row, fields.length)) { if (t++ > 3) break; continue ; } Map map = new HashMap(); for (int j=0; j readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException { List list = new ArrayList<>(); if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ lastRowNum = sheet.getLastRowNum(); } int t = 0; r:for (int i=0; i<=lastRowNum; i++){ Row row = sheet.getRow(i); if (row == null) continue ; short cellNum = row.getLastCellNum(); //空跳过/连续三行为空结束 if (isEmptyRow(row, 3)) { if (t++ > 3) break; continue ; } Map map = new HashMap(); for (int j=0; j < cellNum; j++){ String field = (char)((j/26 ==0? ' ' : 'a')+j/26) + "" +(char)('a'+j%26); field = field.replace(" ",""); Cell cell = row.getCell(j); if (cell == null){ map.put(field, ""); continue; } if (cell.getCellTypeEnum() == CellType.NUMERIC){ map.put(field, (long)cell.getNumericCellValue()+""); }else { map.put(field, cell.getStringCellValue()); } } list.add(map); } return list; } //空跳过/连续三行为空结束 private static boolean isEmptyRow(Row row, int len){ for (int i = 0; i< row.getLastCellNum() && i < len; i++) { Cell cell = row.getCell(i);//列 if (cell != null){ if (cell.getCellTypeEnum() != CellType.NUMERIC && cell.getStringCellValue() != null && !cell.getStringCellValue().isEmpty()){ return false; }else if (cell.getCellTypeEnum() == CellType.NUMERIC && cell.getNumericCellValue() != 0){ return false; } } } return true; } }