1。VO准备DatapublicclassBuSuppMarterQuotaSettingImportVOimplementsSerializable{序号Excel(name序号,orderNum0,width20)privateStringseriNo;物料编码Excel(name物料编码,orderNum1,width20)privateStringmaterCode;物料名称Excel(name物料名称,orderNum2,width20)privateStringmaterName;PPM指标值Excel(namePPM指标值,orderNum3,width20)privateStringppmQuota;IPTV指标Excel(nameIPTV指标,orderNum4,width20)privateStringiptvQuota;启停状态Excel(name启停状态,orderNum5,width20)privateStringstartState;} 2。Controller层下载模板paramresponsereturnPostMapping(valuedownloadTemplate,producesapplicationjson;charsetUTF8)PreAuth(valuebuSuppMarterQuotaSettingYW:importExcel,projectParamConstants。DEFAULTPROJECT,connectParamConstants。DEFAULTPERMISSIONCONNECT,logicalLogical。OR)Log(titleTITLECONTENT下载模板,navNameNAVNAME,actionUniqueKeyNEMUNAME,buSuppMarterQuotaSettingYW:importExcel,interfaceTypeInterfaceType。INSIDE,logType2)publicvoiddownloadTemplate(RequestBodySrmRecVOvo,HttpServletResponseresponse){buSuppMarterQuotaSettingYWService。downloadTemplate(response);} 3。Impl层下载模板paramresponsereturnOverridepublicvoiddownloadTemplate(HttpServletResponseresponse){ListBuSuppMarterQuotaSettingImportVOlistnewArrayList();HSSFWorkbookworkbookgetImportErrorExcel(list,BuSuppMarterQuotaSettingImportVO。class);ServletOutputStreamfosnull;try{fosresponse。getOutputStream();mime类型response。setContentType(applicationvnd。openxmlformatsofficedocument。spreadsheetml。sheet);response。setHeader(Contentdisposition,attachment;filenametemplate。xls);workbook。write(fos);fos。close();}catch(Exceptione){e。printStackTrace();}}获取Excel信息paramlistreturnprivateHSSFWorkbookgetImportErrorExcel(Listlt;?list,Classlt;?objectClass){ExportParamsexportParamsnewExportParams(导入模板,物料价格维护单,ExcelType。HSSF);WorkbookbookExcelExportUtil。exportExcel(exportParams,objectClass,list);HSSFWorkbookworkbook(HSSFWorkbook)book;HSSFSheetsheetworkbook。getSheetAt(0);清空表格数据sheet。shiftRows(1,sheet。getLastRowNum(),1,true,true);冻结第一行7列sheet。createFreezePane(6,1);创建批注的绘图HSSFPatriarchpatrsheet。createDrawingPatriarch();HSSFRowrowsheet。getRow(0);获取表头for(inti0;irow。getLastCellNum();i){循环表头每个单元格,并设置批注设置样式HSSFCellcellrow。getCell(i);CellStylestyleworkbook。createCellStyle();HSSFFontfontworkbook。createFont();font。setBold(true);字体加粗style。setFillForegroundColor(IndexedColors。BLUEGREY。getIndex());设置背景色style。setFillPattern(FillPatternType。FINEDOTS);设置图案样式style。setFillBackgroundColor(IndexedColors。GREY25PERCENT。getIndex());style。setAlignment(HorizontalAlignment。CENTER);水平居中style。setVerticalAlignment(VerticalAlignment。CENTER);垂直居中style。setLocked(true);单元格样式锁定不可编辑设置以下几列字体颜色if(cell。getStringCellValue()。equals(BuSuppPerformanceManage。materCode)cell。getStringCellValue()。equals(BuSuppPerformanceManage。ppmQuota)cell。getStringCellValue()。equals(BuSuppPerformanceManage。iptvQuota)cell。getStringCellValue()。equals(BuSuppPerformanceManage。startState)){font。setColor(Font。COLORRED);}设置批注if(cell。getStringCellValue()。equals(BuSuppPerformanceManage。ppmQuota)){HSSFCommentcommentpatr。createCellComment(newHSSFClientAnchor(0,0,0,0,(short)i,0,(short)(i1),4));批注位置comment。setString(newHSSFRichTextString(BuSuppPerformanceManage。enterNumber));设置批注内容cell。setCellComment(comment);font。setColor(Font。COLORRED);}if(cell。getStringCellValue()。equals(BuSuppPerformanceManage。iptvQuota)){HSSFCommentcommentpatr。createCellComment(newHSSFClientAnchor(0,0,0,0,(short)i,0,(short)(i1),4));批注位置comment。setString(newHSSFRichTextString(BuSuppPerformanceManage。enterNumber));设置批注内容cell。setCellComment(comment);font。setColor(Font。COLORRED);}if(cell。getStringCellValue()。equals(BuSuppPerformanceManage。startState)){HSSFCommentcommentpatr。createCellComment(newHSSFClientAnchor(0,0,0,0,(short)i,0,(short)(i1),4));批注位置comment。setString(newHSSFRichTextString(BuSuppPerformanceManage。onOff));设置批注内容cell。setCellComment(comment);font。setColor(Font。COLORRED);}cell。setCellStyle(style);style。setFont(font);}returnworkbook;} 效果: 结语:常规模板下载还是比较简单,下节记录一下导入功能,这个相对来说复杂点