用apache poi进行excel模板下载

本文介绍了如何使用Apache POI库来创建和下载Excel模板。主要涉及了所需的jar包引用,Controller层的代码实现,以及前端交互和最终展示的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值