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"); }
execl导出功能
最新推荐文章于 2023-11-03 16:56:26 发布