首先咱們要知道MS excel文件由什麼組成,好比說sheet(一個工做表格),表格的行,單元格,單元格格式,單元格內容格式…這些都對應着poi裏面的一個類。java
一個excel表格:HSSFWorkbook wb = new HSSFWorkbook();web
或者 XSSFWorkbook wb = new XSSFWorkbook();正則表達式
一個工做表格:HSSFSheet sheet = wb.createSheet("表格");spring
一行:HSSFRow row = sheet.createRow(0);apache
一個單元格(cell)HSSFCell cell = row.createCell((short)0)數組
單元格格式(cellstyle):HSSFCellStyle style = wb.createCellStyle()session
單元格內容格式:HSSFDataFormat format= wb.createDataFormat();app
讀寫xls和xlsx不一樣格式的excel文檔時,HSSFWorkbook針對.xls,XSSFWorkbook針對.xlsx的格式。xss
PS:xlsx是Microsoft Office EXCEL 2007文檔的擴展名。xlsx是Office2007使用的,是用新的基於XML的壓縮文件格式取代了其目前專有的默認文件格式,在傳統的文件名擴展名後面添加了字母x(即:docx取代doc、.xlsx取代xls等等),使其佔用空間更小。函數
導入.xls格式的excel表格,讀取、篩選其中的內容。將正確的內容存入系統中,提示用戶錯誤導入的數目,並提供用戶下載導入失敗的條目。
@RequiresPermissions("repair:repairItem:edit") @RequestMapping(value = "importExcel") public String repairItemInfoInport(MultipartFile repairItemExcel, HttpServletRequest request,RedirectAttributes redirectAttributes,Model model) throws InvalidFormatException, IOException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); User user =UserUtils.getUser(); Community community = user.getCommunity(); String communityId = community.getId(); Map<String, Object> result = new HashMap<String, Object>(); List<List<Object>> errorExcel=new ArrayList<>(); String Filename = repairItemExcel.getOriginalFilename(); // 判斷條件:1.導入Excel文件後綴必須爲.xls if (Filename.contains(".xls")&& resolver.isMultipart(request) && (StringUtils.isNotBlank(communityId)&& null != repairItemExcel)) { String[] header = {}; List<List<Object>> data = new ArrayList<>(); try { InputStream is = repairItemExcel.getInputStream(); header = excelReader.readExcelTitle(is); data = excelReader.readExcelContent(is); is.close(); } catch (IOException e) { logger.debug("Import fail"); addMessage(model, "導入失敗"); } if (data.size() != 0 && header != null) { /* 把表格中的數據寫到數據表中 */ result = repairItemService.txaddExcelRepairItem(data,communityId); if(result==null||"".equals(result)){ logger.debug("Import fail"); addMessage(model, "導入失敗,請檢查導入表格的格式是否符合模板中的要求"); return importItem(model); } // 建立ImportExcel操做對象ei ImportExcel ei = null; try { ei=new ImportExcel(repairItemExcel, 0, 0); }catch (Exception e) { e.printStackTrace(); addMessage(model, "操做失敗,請重試!"); } // 獲取導入錯誤數據 for (Map<String, Object> m : (List<Map<String, Object>>) result.get("errorRowList")) { Integer rowNumber = (Integer) m.get("errorRow"); Row row = ei.getRow(rowNumber); List<Object> list = new ArrayList<>(); boolean passFlag = false; for (int j = 0; j <= ei.getLastCellNum(); j++) { Object val = ei.getCellValue(row, j); String s = null; if (val instanceof Double) { s = new DecimalFormat("0").format(val); } else { s = val.toString(); } if (s.indexOf("測試") != -1) { passFlag = true; break; } if (j <= 4 || StringUtils.isNotBlank(s)) { list.add(s); } } if (passFlag) { continue; } list.add(m.get("message")); errorExcel.add(list); } // 寫出導入錯誤的數據excel this.errorData=errorExcel; // 成功條數 Integer successNumber = (Integer) result.get("successNumber"); // 失敗條數 Integer errorNumber = (Integer) result.get("errorNumber"); if (errorNumber == null || errorNumber == 0) { addMessage(redirectAttributes, "成功導入全部"); } else { addMessage(redirectAttributes, "成功導入" + successNumber + "條," + errorNumber + "條導入失敗!"); } } }else{ addMessage(model, "導入失敗,請選擇.xls文件"); return importItem(model); } return "redirect:"+Global.getAdminPath()+"/repair/repairItem/?repage"; } /** * 導出錯誤的excel * * @param response */ @RequestMapping("exportError") public void exportError(HttpServletResponse response) { // 寫出錯誤記錄excel if (errorData != null && !errorData.isEmpty()) { String[] headers = {"類型", "名稱", "單價(元)", "單位", "備註","錯誤信息"}; try { WriteExcelUtil.writeExcel(response, "錯誤記錄", headers, errorData); errorData.removeAll(errorData); errorData.clear(); } catch (IOException e) { e.printStackTrace(); } } }
操做Excel表格的功能類 ExcelReader.java
import org.apache.poi.hssf.usermodel.*; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 操做Excel表格的功能類 */ public class ExcelReader { private POIFSFileSystem fs; private HSSFWorkbook wb; private HSSFSheet sheet; private HSSFRow row; /** * 讀取Excel表格表頭的內容 * @param * @return String 表頭內容的數組 */ public String[] readExcelTitle(InputStream is) { try { fs = new POIFSFileSystem(is); //is.close(); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); // 標題總列數 int colNum = row.getPhysicalNumberOfCells(); /*System.out.println("colNum:" + colNum);*/ String[] title = new String[colNum]; int i; for ( i = 0; i < colNum; i++) { //title[i] = getStringCellValue(row.getCell((short) i)); title[i] = getCellFormatValue(row.getCell((short) i)); } return title; } /** * 讀取Excel數據內容 * @param * @return Map 包含單元格數據內容的Map對象 */ /*public List<String[]> readExcelContent(InputStream is) { List<String[]> content = new ArrayList<>(); String str = ""; try { fs = new POIFSFileSystem(is); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); // 獲得總行數 int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文內容應該從第二行開始,第一行爲表頭的標題 for (int i = 1; i <= rowNum; i++) { String[] temp=new String[colNum]; row = sheet.getRow(i); int j = 0; while (j < colNum) { // 每一個單元格的數據內容用"-"分割開,之後須要時用String類的replace()方法還原數據 // 也能夠將每一個單元格的數據設置到一個javabean的屬性中,此時須要新建一個javabean // str += getStringCellValue(row.getCell((short) j)).trim() + // "-"; temp[j]=getCellFormatValue(row.getCell( j)).trim(); // temp[j]=str; j++; } content.add(temp); str = ""; } return content; }*/ /** * 讀取Excel數據內容 * @param * @return List<List<Object>>包含單元格數據內容的List<List<Object>>對象 */ public List<List<Object>> readExcelContent(InputStream is) { /*try { //fs = new POIFSFileSystem(is); //is.close(); //wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); }*/ int rowNum = sheet.getLastRowNum(); int colNum = row.getPhysicalNumberOfCells(); // 正文內容應該從第二行開始,第一行爲表頭的標題 List<List<Object>> data = new ArrayList<List<Object>>(); List<Object> l; for (int i = 1; i <= rowNum; i++) { l = new ArrayList<Object>(); row = sheet.getRow(i); int j = 0; while (j < colNum) { // 也能夠將每一個單元格的數據設置到一個javabean的屬性中,此時須要新建一個javabean // if (getCellFormatValue(row.getCell(j))) l.add(getStringCellValue(row.getCell(j)).trim()); j++; } data.add(l); } return data; } /** * 獲取單元格數據內容爲字符串類型的數據 * * @param cell Excel單元格 * @return String 單元格數據內容 */ private String getStringCellValue(HSSFCell cell) { String strCell = ""; if (cell==null){ return strCell; } switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: DecimalFormat df = new DecimalFormat("#"); strCell = df.format(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (strCell.equals("") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; } /** * 獲取單元格數據內容爲日期類型的數據 * * @param cell * Excel單元格 * @return String 單元格數據內容 */ private String getDateCellValue(HSSFCell cell) { String result = ""; try { int cellType = cell.getCellType(); if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { Date date = cell.getDateCellValue(); result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate(); } else if (cellType == HSSFCell.CELL_TYPE_STRING) { String date = getStringCellValue(cell); result = date.replaceAll("[年月]", "-").replace("日", "").trim(); } else if (cellType == HSSFCell.CELL_TYPE_BLANK) { result = ""; } } catch (Exception e) { System.out.println("日期格式不正確!"); e.printStackTrace(); } return result; } /** * 根據HSSFCell類型設置數據 * @param cell * @return */ private String getCellFormatValue(HSSFCell cell) { String cellvalue = ""; if (cell != null) { // 判斷當前Cell的Type switch (cell.getCellType()) { // 若是當前Cell的Type爲NUMERIC case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_FORMULA: { // 判斷當前的cell是否爲Date if (HSSFDateUtil.isCellDateFormatted(cell)) { // 若是是Date類型則,轉化爲Data格式 //方法1:這樣子的data格式是帶時分秒的:2011-10-12 0:00:00 //cellvalue = cell.getDateCellValue().toLocaleString(); //方法2:這樣子的data格式是不帶帶時分秒的:2011-10-12 Date date = cell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); cellvalue = sdf.format(date); } // 若是是純數字 else { // 取得當前Cell的數值 cellvalue = String.valueOf(cell.getNumericCellValue()); } break; } // 若是當前Cell的Type爲STRIN case HSSFCell.CELL_TYPE_STRING: // 取得當前的Cell字符串 cellvalue = cell.getRichStringCellValue().getString(); break; // 默認的Cell值 default: cellvalue = " "; } } else { cellvalue = ""; } return cellvalue; } }
導入Excel文件(支持「XLS」和「XLSX」格式): ImportExcel.java
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; import com.google.common.collect.Lists; import com.qwkj.zxl.common.utils.Reflections; import com.qwkj.zxl.common.utils.excel.annotation.ExcelField; import com.qwkj.zxl.modules.sys.utils.DictUtils; /** * 導入Excel文件(支持「XLS」和「XLSX」格式) * @author ThinkGem * @version 2013-03-10 */ public class ImportExcel { private static Logger log = LoggerFactory.getLogger(ImportExcel.class); /** * 工做薄對象 */ private Workbook wb; /** * 工做表對象 */ private Sheet sheet; /** * 標題行號 */ private int headerNum; /** * 構造函數 * @param path 導入文件,讀取第一個工做表 * @param headerNum 標題行號,數據行號=標題行號+1 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, int headerNum) throws InvalidFormatException, IOException { this(new File(fileName), headerNum); } /** * 構造函數 * @param path 導入文件對象,讀取第一個工做表 * @param headerNum 標題行號,數據行號=標題行號+1 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(File file, int headerNum) throws InvalidFormatException, IOException { this(file, headerNum, 0); } /** * 構造函數 * @param path 導入文件 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工做表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(new File(fileName), headerNum, sheetIndex); } /** * 構造函數 * @param path 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工做表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(File file, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(file.getName(), new FileInputStream(file), headerNum, sheetIndex); } /** * 構造函數 * @param file 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工做表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex); } /** * 構造函數 * @param path 導入文件對象 * @param headerNum 標題行號,數據行號=標題行號+1 * @param sheetIndex 工做表編號 * @throws InvalidFormatException * @throws IOException */ public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { if (StringUtils.isBlank(fileName)){ throw new RuntimeException("導入文檔爲空!"); }else if(fileName.toLowerCase().endsWith("xls")){ this.wb = new HSSFWorkbook(is); }else if(fileName.toLowerCase().endsWith("xlsx")){ this.wb = new XSSFWorkbook(is); }else{ throw new RuntimeException("文檔格式不正確!"); } if (this.wb.getNumberOfSheets()<sheetIndex){ throw new RuntimeException("文檔中沒有工做表!"); } this.sheet = this.wb.getSheetAt(sheetIndex); this.headerNum = headerNum; log.debug("Initialize success."); } /** * 獲取行對象 * @param rownum * @return */ public Row getRow(int rownum){ return this.sheet.getRow(rownum); } /** * 獲取數據行號 * @return */ public int getDataRowNum(){ return headerNum+1; } /** * 獲取最後一個數據行號 * @return */ public int getLastDataRowNum(){ return this.sheet.getLastRowNum()+headerNum; } /** * 獲取最後一個列號 * @return */ public int getLastCellNum(){ return this.getRow(headerNum).getLastCellNum(); } /** * 獲取單元格值 * @param row 獲取的行 * @param column 獲取單元格列號 * @return 單元格值 */ public Object getCellValue(Row row, int column){ Object val = ""; try{ Cell cell = row.getCell(column); if (cell != null){ if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ val = cell.getNumericCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_STRING){ val = cell.getStringCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA){ val = cell.getCellFormula(); }else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){ val = cell.getBooleanCellValue(); }else if (cell.getCellType() == Cell.CELL_TYPE_ERROR){ val = cell.getErrorCellValue(); } } }catch (Exception e) { return val; } return val; } /** * 獲取導入數據列表 * @param cls 導入對象類型 * @param groups 導入分組 */ public <E> List<E> getDataList(Class<E> cls, int... groups) throws InstantiationException, IllegalAccessException{ List<Object[]> annotationList = Lists.newArrayList(); // Get annotation field Field[] fs = cls.getDeclaredFields(); for (Field f : fs){ ExcelField ef = f.getAnnotation(ExcelField.class); if (ef != null && (ef.type()==0 || ef.type()==2)){ if (groups!=null && groups.length>0){ boolean inGroup = false; for (int g : groups){ if (inGroup){ break; } for (int efg : ef.groups()){ if (g == efg){ inGroup = true; annotationList.add(new Object[]{ef, f}); break; } } } }else{ annotationList.add(new Object[]{ef, f}); } } } // Get annotation method Method[] ms = cls.getDeclaredMethods(); for (Method m : ms){ ExcelField ef = m.getAnnotation(ExcelField.class); if (ef != null && (ef.type()==0 || ef.type()==2)){ if (groups!=null && groups.length>0){ boolean inGroup = false; for (int g : groups){ if (inGroup){ break; } for (int efg : ef.groups()){ if (g == efg){ inGroup = true; annotationList.add(new Object[]{ef, m}); break; } } } }else{ annotationList.add(new Object[]{ef, m}); } } } // Field sorting Collections.sort(annotationList, new Comparator<Object[]>() { public int compare(Object[] o1, Object[] o2) { return new Integer(((ExcelField)o1[0]).sort()).compareTo( new Integer(((ExcelField)o2[0]).sort())); }; }); //log.debug("Import column count:"+annotationList.size()); // Get excel data List<E> dataList = Lists.newArrayList(); for (int i = this.getDataRowNum(); i < this.getLastDataRowNum(); i++) { E e = (E)cls.newInstance(); int column = 0; Row row = this.getRow(i); StringBuilder sb = new StringBuilder(); for (Object[] os : annotationList){ Object val = this.getCellValue(row, column++); if (val != null){ ExcelField ef = (ExcelField)os[0]; // If is dict type, get dict value if (StringUtils.isNotBlank(ef.dictType())){ val = DictUtils.getDictValue(val.toString(), ef.dictType(), ""); //log.debug("Dictionary type value: ["+i+","+colunm+"] " + val); } // Get param type and type cast Class<?> valType = Class.class; if (os[1] instanceof Field){ valType = ((Field)os[1]).getType(); }else if (os[1] instanceof Method){ Method method = ((Method)os[1]); if ("get".equals(method.getName().substring(0, 3))){ valType = method.getReturnType(); }else if("set".equals(method.getName().substring(0, 3))){ valType = ((Method)os[1]).getParameterTypes()[0]; } } //log.debug("Import value type: ["+i+","+column+"] " + valType); try { if (valType == String.class){ String s = String.valueOf(val.toString()); if(StringUtils.endsWith(s, ".0")){ val = StringUtils.substringBefore(s, ".0"); }else{ val = String.valueOf(val.toString()); } }else if (valType == Integer.class){ val = Double.valueOf(val.toString()).intValue(); }else if (valType == Long.class){ val = Double.valueOf(val.toString()).longValue(); }else if (valType == Double.class){ val = Double.valueOf(val.toString()); }else if (valType == Float.class){ val = Float.valueOf(val.toString()); }else if (valType == Date.class){ val = DateUtil.getJavaDate((Double)val); }else{ if (ef.fieldType() != Class.class){ val = ef.fieldType().getMethod("getValue", String.class).invoke(null, val.toString()); }else{ val = Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(), "fieldtype."+valType.getSimpleName()+"Type")).getMethod("getValue", String.class).invoke(null, val.toString()); } } } catch (Exception ex) { log.info("Get cell value ["+i+","+column+"] error: " + ex.toString()); val = null; } // set entity value if (os[1] instanceof Field){ Reflections.invokeSetter(e, ((Field)os[1]).getName(), val); }else if (os[1] instanceof Method){ String mthodName = ((Method)os[1]).getName(); if ("get".equals(mthodName.substring(0, 3))){ mthodName = "set"+StringUtils.substringAfter(mthodName, "get"); } Reflections.invokeMethod(e, mthodName, new Class[] {valType}, new Object[] {val}); } } sb.append(val+", "); } dataList.add(e); log.debug("Read success: ["+i+"] "+sb.toString()); } return dataList; } // /** // * 導入測試 // */ // public static void main(String[] args) throws Throwable { // // ImportExcel ei = new ImportExcel("target/export.xlsx", 1); // // for (int i = ei.getDataRowNum(); i < ei.getLastDataRowNum(); i++) { // Row row = ei.getRow(i); // for (int j = 0; j < ei.getLastCellNum(); j++) { // Object val = ei.getCellValue(row, j); // System.out.print(val+", "); // } // System.out.print("\n"); // } // // } }
@RequiresPermissions("repair:repairItem:edit") @RequestMapping(value = "exportExcel") public void repairItemExportTemplateDown(HttpServletResponse response,HttpSession session){ try { //讀寫xls和xlsx格式時,HSSFWorkbook針對xls,XSSFWorkbook針對xlsx HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(session.getServletContext().getRealPath("/") + "excel\\導入模板.xls")); //response.setHeader("Content-disposition", "filename= repairItem.xls"); //repairItemService.downRepairItemMould(header, response.getOutputStream()); response.setHeader("Content-Disposition", "attachment;filename=" + new String("導入模板.xls".getBytes(), "ISO-8859-1")); wb.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } }
@RequestMapping("exportPayBillRecord") public void exportPayBillRecord(PayBillrecord payBillrecord, HttpServletResponse response) { List<PayBillrecord> PayBillrecordLists = payBillrecordService.findList(payBillrecord); List<List<Object>> data = new ArrayList<List<Object>>(); List<PayBillrecord> countList=payBillrecordService.findList(payBillrecord); //所繳金額總數 BigDecimal countMoney =new BigDecimal("0"); for(int i=0; i<countList.size();i++){ countMoney=countMoney.add(countList.get(i).getPaymoney()); } // 製做data for (PayBillrecord PayBillrecordList : PayBillrecordLists) { List<Object> list = new ArrayList<Object>(); list.add(PayBillrecordList.getCostBillNo()); list.add(PayBillrecordList.getTradeNo()); list.add(PayBillrecordList.getPayTypeName()); list.add(PayBillrecordList.getPaymoney()); if(PayBillrecordList.getUser()!=null){ list.add(PayBillrecordList.getUser().getUserRealName()); } else{ list.add(""); } list.add(PayBillrecordList.getCommunity().getName()); data.add(list); } String[] headers = {"繳費單號", "交易單號", "繳費類型", "所繳金額", "繳費方式", "繳費人","所屬小區","繳費時間"}; try { WriteExcelUtil.writeExcel(response, "繳費歷史信息統計", headers, data); } catch (Exception e) { e.printStackTrace(); } }
/** * 寫出普通的excel * * @param response HttpServletResponse * @param fileName 寫出文件的文件名,若沒有添加文件類型後綴,則默認添加".xls" * @param headers 標題行內容 * @param data 數據行內容(須要和標題行順序同樣,只支持各類普通類型) */ public static void writeExcel(HttpServletResponse response, String fileName, String[] headers, List<List<Object>> data) throws IOException { // 基礎操做 HSSFWorkbook workbook = baseHandle(headers, data); // 文件名操做 fileNameHandle(response, fileName); // 寫出excel workbook.write(response.getOutputStream()); }
導出的實際效果圖以下:
還封裝了一下寫出Excel的工具類: WriteExcelUtil.java
package com.qwkj.zxl.common.utils.excel; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.CellRangeAddressList; import org.apache.poi.hssf.util.HSSFColor; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; /** * 2016/3/9 * 寫出Excel工具類 */ public class WriteExcelUtil { /** * 寫出帶有選擇框限制的excel * @param response HttpServletResponse * @param fileName 寫出文件的文件名,若沒有添加文件類型後綴,則默認添加".xls" * @param headers 標題行內容 * @param data 數據行內容(須要和標題行順序同樣,只支持各類普通類型) * @param maps 須要作選擇框限制的列的集合 說明:1.map中key爲"titleContent"中需存入,須要作選擇框的列頂部標題內容 2.map中key爲"data"中需存入,所能選擇的數據集合 * @throws Exception */ public static void writeExcelHaveSelectBox(HttpServletResponse response, String fileName, String[] headers, List<List<Object>> data, List<Map<String, Object>> maps) throws Exception { if(maps == null || maps.isEmpty()){ throw new RuntimeException("參數maps爲空"); } // 基礎操做 HSSFWorkbook workbook = baseHandle(headers, data); // 文件名操做 fileNameHandle(response, fileName); // 若是maps裏有數據,則生成選擇框限制 writeSelectionBox(headers, maps, workbook); // 寫出文件 workbook.write(response.getOutputStream()); } /** * 寫出普通的excel * * @param response HttpServletResponse * @param fileName 寫出文件的文件名,若沒有添加文件類型後綴,則默認添加".xls" * @param headers 標題行內容 * @param data 數據行內容(須要和標題行順序同樣,只支持各類普通類型) */ public static void writeExcel(HttpServletResponse response, String fileName, String[] headers, List<List<Object>> data) throws IOException { // 基礎操做 HSSFWorkbook workbook = baseHandle(headers, data); // 文件名操做 fileNameHandle(response, fileName); // 寫出excel workbook.write(response.getOutputStream()); } /** * 寫出excel的一些基礎操做 * @param headers 標題行內容 * @param data 數據行內容 * @return 返回操做的HSSFWorkbook * @throws Exception */ private static HSSFWorkbook baseHandle(String[] headers, List<List<Object>> data) throws IOException { // 聲明一個工做薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(); // 設置表格默認列寬度爲20個字節 sheet.setDefaultColumnWidth(20); // 寫入標題行 writeTitleRow(headers, workbook); // 若是data有數據,則遍歷集合數據,產生數據行 if(data != null && !data.isEmpty()){ writeDataRow(data, workbook); } // 返回操做的HSSFWorkbook return workbook; } /** * 文件名操做 * @param response HttpServletResponse * @param fileName 寫出文件的文件名,若沒有添加文件類型後綴,則默認添加".xls" */ private static void fileNameHandle(HttpServletResponse response, String fileName) throws IOException{ // 若是fileName沒有".",說明沒有加文件類型後綴,則默認添加".xls" if (fileName.indexOf(".") == -1) { fileName += ".xls"; } // 文件名轉碼 fileName = new String(fileName.getBytes(), "ISO-8859-1"); // 在請求頭中寫入文件名 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); } /** * 生成選擇框限制 * @param headers 標題行內容(用於肯定,有選擇框限制的列位置) * @param maps 須要作選擇框限制的列的集合 說明:1.map中key爲"titleContent"中需存入,須要作選擇框的列頂部標題內容 2.map中key爲"data"中需存入,所能選擇的數據集合 */ private static void writeSelectionBox(String[] headers, List<Map<String, Object>> maps, HSSFWorkbook workbook) throws Exception{ for(Map<String, Object> map : maps){ // 須要作選擇框限制的列的標題內容 String titleContent = map.get("titleContent").toString(); // 能選擇的數據 List<Object> list = (List<Object>) map.get("data"); // 所能的數據不能超過50條(通過測試得出,超過50則會報錯) if(list.size() > 50){ throw new RuntimeException("所能選擇的選項不能超過50"); } // 肯定有選擇框限制的列位置 Integer index = null; for(int i = 0; i < headers.length; ++i){ if(headers[i].equals(titleContent)){ index = i; break; } } // 若是肯定不了位置,則彈出一個錯誤 if(index == null){ throw new RuntimeException("尋找不到指定列"); } // 產生下拉框 DVConstraint constraint1 = DVConstraint.createExplicitListConstraint(list.toArray(new String[list.size()])); // 設置數據有效性加載在哪一個單元格上,四個參數分別是:起始行、終止行、起始列、終止列 CellRangeAddressList regions1 = new CellRangeAddressList(1, 500, 1, 1); // 數據有效性對象 HSSFDataValidation data_validation_list1 = new HSSFDataValidation(regions1, constraint1); workbook.getSheetAt(0).addValidationData(data_validation_list1); } } /** * 寫入標題行 * @param headers 標題行內容 * @param workbook HSSFWorkbook */ private static void writeTitleRow(String[] headers, HSSFWorkbook workbook){ // 獲取表格 HSSFSheet sheet = workbook.getSheetAt(0); // 生成標題樣式 HSSFCellStyle titleStyle = workbook.createCellStyle(); // 設置這些樣式 titleStyle.setBottomBorderColor(HSSFColor.SKY_BLUE.index);// 設置用於底部邊框的顏色 titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);// 設置前景色填充顏色注意:保證前顏色設置背景色。 titleStyle.setFillBackgroundColor(HSSFColor.SKY_BLUE.index);// 設置背景填充顏色 titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 設置爲一個充滿前景顏色的單元格 titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 設置單元格的底邊框的類型 titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 設置單元格的左邊框的類型 titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 設置單元格的右邊框的類型 titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 設置單元格的頂邊框的類型 titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 設置單元格的水平對齊方式 // 生成標題字體 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index);// 字體顏色 font.setFontHeightInPoints((short) 12);// 字體高度 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字體粗細 // 把標題字體應用到標題樣式 titleStyle.setFont(font); //產生表格標題行 HSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(titleStyle); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } } /** * 寫入數據行 * @param data 數據 * @param workbook HSSFWorkbook */ private static void writeDataRow(List<List<Object>> data, HSSFWorkbook workbook){ // 獲取表格 HSSFSheet sheet = workbook.getSheetAt(0); // 生成並設置內容樣式 HSSFCellStyle contentStyle = workbook.createCellStyle(); contentStyle.setFillForegroundColor(HSSFColor.WHITE.index); contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成內容字體 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字體應用到當前的樣式 contentStyle.setFont(font2); int index = 0; for (List<Object> list : data) { index++; HSSFRow row = sheet.createRow(index); for (int i = 0; i < list.size(); i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(contentStyle); Object value = list.get(i); String textValue = null; if (value instanceof Boolean) { boolean bValue = (Boolean) value; textValue = "是"; if (!bValue) { textValue = "否"; } } else if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); textValue = sdf.format(date); } else if (value instanceof byte[]) { //畫圖的頂級管理器,一個sheet只能獲取一個(必定要注意這點) HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 有圖片時,設置行高爲60px; row.setHeightInPoints(60); // 設置圖片所在列寬度爲80px,注意這裏單位的一個換算 sheet.setColumnWidth(i, (short) (35.7 * 80)); byte[] bsValue = (byte[]) value; HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, index, (short) 6, index); anchor.setAnchorType(2); //插入圖片 patriarch.createPicture(anchor, workbook.addPicture(bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { //其它數據類型都看成字符串簡單處理 if (value != null) { textValue = value.toString(); } else { textValue = " "; } } //若是不是圖片數據,就利用正則表達式判斷textValue是否所有由數字組成 if (textValue != null) { HSSFRichTextString richString = new HSSFRichTextString(textValue); HSSFFont font3 = workbook.createFont(); richString.applyFont(font3); cell.setCellValue(richString); } } } } }