execl导出功能

public ApiResult<Integer> outExport(HttpServletRequest request, HttpServletResponse response) throws IOException{
    //获取前台传过来的ids
    Integer houseId=0;
    String str="";
    if (!StringUtils.isEmpty(request.getParameter("house"))) {
        houseId = Integer.parseInt(request.getParameter("house"));
    }
    if (!StringUtils.isEmpty(request.getParameter("str"))) {
        str = request.getParameter("str");
    }

    List<ErpGoodsSpecStockVo> list = stockService.goodsStockInfoSearch(str, houseId);// null;//orderService.getOrderSendExportList(sender, senderMobile, senderTel);
    //文件名称,客户端传来的参数,防止中文文件名乱码参数编码因此这里需要解码
    String fileName = URLDecoder.decode(request.getParameter("fileName"),"UTF-8");
    //创建Excel工作薄对象
    HSSFWorkbook workbook = new HSSFWorkbook();
    //创建Excel工作表对象
    HSSFSheet sheet = workbook.createSheet();
    sheet.setColumnWidth(0, 3000);
    sheet.setColumnWidth(1, 5000);
    sheet.setColumnWidth(2, 4000);
    sheet.setColumnWidth(3, 2500);
    sheet.setColumnWidth(4, 3000);
    sheet.setColumnWidth(5, 6000);
    sheet.setColumnWidth(6, 6000);

    // 设置表头字体样式
    HSSFFont columnHeadFont = workbook.createFont();
    columnHeadFont.setFontName("宋体");
    columnHeadFont.setFontHeightInPoints((short) 10);

    // 列头的样式
    HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
    columnHeadStyle.setFont(columnHeadFont);
    // 上下居中
    columnHeadStyle.setLocked(true);
    columnHeadStyle.setWrapText(true);
    // 设置普通单元格字体样式
    HSSFFont font = workbook.createFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) 10);

    //创建Excel工作表第一行
    HSSFRow row0 = sheet.createRow(0);
    // 设置行高
    row0.setHeight((short) 450);

    HSSFCell cell = row0.createCell(0);
    //设置单元格内容
    cell.setCellValue(new HSSFRichTextString("id"));
    //设置单元格字体样式
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(1);
    cell.setCellValue(new HSSFRichTextString("商品编号"));
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(2);
    cell.setCellValue(new HSSFRichTextString("商品名称"));
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(3);
    cell.setCellValue(new HSSFRichTextString("sku编码"));
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(4);
    cell.setCellValue(new HSSFRichTextString("商品规格"));
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(5);
    cell.setCellValue(new HSSFRichTextString("所在仓位"));
    cell.setCellStyle(columnHeadStyle);
    cell = row0.createCell(6);
    cell.setCellValue(new HSSFRichTextString("当前库存"));
    cell.setCellStyle(columnHeadStyle);

    // 循环写入数据
    for (int i = 0; i < list.size(); i++) {
        ErpGoodsSpecStockVo item = list.get(i);
        HSSFRow row = sheet.createRow(i + 1);
        cell = row.createCell(0);
        cell.setCellValue(new HSSFRichTextString(String.valueOf(item.getId())));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(1);
        if (item.getNumber()==null) item.setNumber("");
        cell.setCellValue(new HSSFRichTextString(item.getNumber()));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(2);
        if (item.getGoodsName()==null) item.setGoodsName("");
        cell.setCellValue(new HSSFRichTextString(item.getGoodsName()));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(3);
        if (item.getSpecNumber()==null) item.setSpecNumber("");
        cell.setCellValue(new HSSFRichTextString(item.getSpecNumber()));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(4);
        if (item.getColorValue()==null) item.setColorValue("");
        if (item.getSizeValue()==null) item.setSizeValue("");
        if (item.getStyleValue()==null) item.setStyleValue("");
        cell.setCellValue(new HSSFRichTextString(item.getColorValue()+" "+item.getSizeValue()+"  "+item.getStyleValue()));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(5);
        if (item.getLocationName()==null) item.setLocationName("");
        cell.setCellValue(new HSSFRichTextString(item.getLocationName()));
        cell.setCellStyle(columnHeadStyle);
        cell = row.createCell(6);
        cell.setCellValue(new HSSFRichTextString(item.getCurrentQty().toString()));
        cell.setCellStyle(columnHeadStyle);
    }
    // 获取输出流
    OutputStream os = new FileOutputStream("C:\\test.xls",true);//加上true,可重复
    // 重置输出流
    response.reset();
    // 设定输出文件头
    response.setHeader("Content-disposition",
            "attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
    // 定义输出类型
    response.setContentType("application/msexcel");
    workbook.write(os);
    os.close();
    return new ApiResult<>(0, "SUCCESS");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值