1.需要的jar包
2.Controller层
/** * 批量导出模板控制器 */ @Controller public class GoodsImportedAction { @RequestMapping("/exceportExcel.htm") public void export( HttpServletRequest request, HttpServletResponse response){ /* Collection<ImportedGoods> list = new ArrayList<ImportedGoods>(); ExportParams params = new ExportParams("商品批量导出模板", "商品批量导出模板"); Workbook workbook = ExcelExportUtil.exportExcel(params, ImportedGoods.class, list); response.setHeader("content-Type","application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+".xls"); response.setCharacterEncoding("UTF-8"); try { workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); }*/ //创建工作簿对象 HSSFWorkbook wb=new HSSFWorkbook(); //创建工作表对象 HSSFSheet sheet=wb.createSheet("商品批量导出模板"); //创建绘图对象 HSSFPatriarch p=sheet.createDrawingPatriarch(); HSSFRow row = sheet.createRow(0); //设置背景色 CellStyle style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); //插入单元格内容 HSSFCell cell3 = row.createCell(0); cell3.setCellValue(new HSSFRichTextString("宝贝名称")); cell3.setCellStyle(style); HSSFCell cell4 = row.createCell(1); cell4.setCellValue(new HSSFRichTextString("宝贝价格")); cell4.setCellStyle(style); HSSFCell cell5 = row.createCell(2); cell5.setCellValue(new HSSFRichTextString("宝贝数量")); cell5.setCellStyle(style); HSSFCell cell1 = row.createCell(3); cell1.setCellValue(new HSSFRichTextString("运费承担")); cell1.setCellStyle(style); //获取批注对象 //(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) //前四个参数是坐标点,后四个参数是编辑和显示批注时的大小. HSSFComment comment1=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6)); comment1.setString(new HSSFRichTextString("0=买家承担;1=卖家承担")); cell1.setCellComment(comment1); HSSFCell cell = row.createCell(4); cell.setCellValue(new HSSFRichTextString("放入仓库")); cell.setCellStyle(style); HSSFComment comment2=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6)); comment2.setString(new HSSFRichTextString("1=立即发布;2=放入仓库")); cell.setCellComment(comment2); HSSFCell cell2 = row.createCell(5); cell2.setCellValue(new HSSFRichTextString("橱窗推荐")); cell2.setCellStyle(style); HSSFComment comment3=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6)); comment3.setString(new HSSFRichTextString("TRUE=推荐;FALSE=不推荐")); cell2.setCellComment(comment3); HSSFCell cell6 = row.createCell(6); cell6.setCellValue(new HSSFRichTextString("宝贝描述")); cell6.setCellStyle(style); HSSFCell cell7 = row.createCell(7); cell7.setCellValue(new HSSFRichTextString("图片1")); cell7.setCellStyle(style); HSSFCell cell8 = row.createCell(8); cell8.setCellValue(new HSSFRichTextString("图片2")); cell8.setCellStyle(style); HSSFCell cell9 = row.createCell(9); cell9.setCellValue(new HSSFRichTextString("图片3")); cell9.setCellStyle(style); HSSFCell cell10 = row.createCell(10); cell10.setCellValue(new HSSFRichTextString("图片4")); cell10.setCellStyle(style); HSSFCell cell11 = row.createCell(11); cell11.setCellValue(new HSSFRichTextString("图片5")); cell11.setCellStyle(style); response.setHeader("content-Type","application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+".xls"); response.setCharacterEncoding("UTF-8"); try { wb.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
3.前端
<a href="/exceportExcel.htm">模板下载</a>
4.效果