From 8a4d0347437750304d1920d0eeeda88c4371b676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=A3=E5=9C=A3=E5=8D=8E?= <752718920@qq.com> Date: Fri, 4 Jan 2019 22:48:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BA=93=E5=AD=98=E6=8A=A5?= =?UTF-8?q?=E8=A1=A8excel=E5=AF=BC=E5=87=BA=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pages/reports/in_out_stock_report.html | 2 +- pom.xml | 5 + .../erp/controller/DepotItemController.java | 66 +++++ .../java/com/jsh/erp/utils/ExcelUtils.java | 240 ++++++++++++++++++ .../com/jsh/erp/utils/ExportExecUtil.java | 28 ++ 5 files changed, 340 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/jsh/erp/utils/ExcelUtils.java create mode 100644 src/main/java/com/jsh/erp/utils/ExportExecUtil.java diff --git a/erp_web/pages/reports/in_out_stock_report.html b/erp_web/pages/reports/in_out_stock_report.html index 07d351cf..cd00cdb4 100644 --- a/erp_web/pages/reports/in_out_stock_report.html +++ b/erp_web/pages/reports/in_out_stock_report.html @@ -325,7 +325,7 @@ var mIds = res.data.mIds; if (mIds) { if (pageSize === 3000) { - window.location.href = "/depotItem/exportExcel.action?browserType=" + getOs() + "&pageNo=" + pageNo + "&pageSize=" + pageSize + "&ProjectId=" + $.trim($("#searchProjectId").val()) + "&MonthTime=" + $("#searchMonth").val() + "&HeadIds=" + HeadIds + "&MaterialIds=" + MIds; + window.location.href = "/depotItem/exportExcel?browserType=" + getOs() + "¤tPage=" + pageNo + "&pageSize=" + pageSize + "&projectId=" + $.trim($("#searchProjectId").val()) + "&monthTime=" + $("#searchMonth").val() + "&headIds=" + HeadIds + "&materialIds=" + mIds; } else { $.ajax({ diff --git a/pom.xml b/pom.xml index 67881160..d245e114 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,11 @@ commons-httpclient 3.1 + + net.sourceforge.jexcelapi + jxl + 2.6.3 + diff --git a/src/main/java/com/jsh/erp/controller/DepotItemController.java b/src/main/java/com/jsh/erp/controller/DepotItemController.java index e33c8280..2e937db8 100644 --- a/src/main/java/com/jsh/erp/controller/DepotItemController.java +++ b/src/main/java/com/jsh/erp/controller/DepotItemController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -841,6 +843,70 @@ public class DepotItemController { return res; } + /** + * 导出excel表格 + * @param request + * @return + */ + @GetMapping(value = "/exportExcel") + public BaseResponseInfo exportExcel(@RequestParam("currentPage") Integer currentPage, + @RequestParam("pageSize") Integer pageSize, + @RequestParam("projectId") Integer projectId, + @RequestParam("monthTime") String monthTime, + @RequestParam("headIds") String headIds, + @RequestParam("materialIds") String materialIds, + HttpServletRequest request, HttpServletResponse response) { + BaseResponseInfo res = new BaseResponseInfo(); + Map map = new HashMap(); + String message = "成功"; + try { + List dataList = depotItemService.findByAll(headIds, materialIds, currentPage, pageSize); + //存放数据json数组 + Integer pid = projectId; + String[] names = {"名称", "型号", "单位", "单价", "上月结存数量", "入库数量", "出库数量", "本月结存数量", "结存金额"}; + String title = "库存报表"; + List objects = new ArrayList(); + if (null != dataList) { + for (DepotItemVo4WithInfoEx diEx : dataList) { + String[] objs = new String[13]; + Double prevSum = sumNumber("入库", pid, diEx.getMaterialid(), monthTime, true) - sumNumber("出库", pid, diEx.getMaterialid(), monthTime, true); + Double InSum = sumNumber("入库", pid, diEx.getMaterialid(), monthTime, false); + Double OutSum = sumNumber("出库", pid, diEx.getMaterialid(), monthTime, false); + Double prevPrice = sumPrice("入库", pid, diEx.getMaterialid(), monthTime, true) - sumPrice("出库", pid, diEx.getMaterialid(), monthTime, true); + Double InPrice = sumPrice("入库", pid, diEx.getMaterialid(), monthTime, false); + Double OutPrice = sumPrice("出库", pid, diEx.getMaterialid(), monthTime, false); + Double unitPrice = 0.0; + if (prevSum + InSum - OutSum != 0.0) { + unitPrice = (prevPrice + InPrice - OutPrice) / (prevSum + InSum - OutSum); + } + Double thisSum = prevSum + InSum - OutSum; + Double thisAllPrice = prevPrice + InPrice - OutPrice; + objs[0] = diEx.getMName().toString(); + objs[1] = diEx.getMModel().toString(); + objs[2] = diEx.getMaterialUnit().toString(); + objs[3] = unitPrice.toString(); + objs[4] = prevSum.toString(); + objs[5] = InSum.toString(); + objs[6] = OutSum.toString(); + objs[7] = thisSum.toString(); + objs[8] = thisAllPrice.toString(); + objects.add(objs); + } + } + File file = ExcelUtils.exportObjectsWithoutTitle(title, names, title, objects); + ExportExecUtil.showExec(file, file.getName() + "-" + monthTime, response); + res.code = 200; + } catch (Exception e) { + e.printStackTrace(); + message = "导出失败"; + res.code = 500; + } finally { + map.put("message", message); + res.data = map; + } + return res; + } + /** * 数量合计 * diff --git a/src/main/java/com/jsh/erp/utils/ExcelUtils.java b/src/main/java/com/jsh/erp/utils/ExcelUtils.java new file mode 100644 index 00000000..c8e6849c --- /dev/null +++ b/src/main/java/com/jsh/erp/utils/ExcelUtils.java @@ -0,0 +1,240 @@ +package com.jsh.erp.utils; + +import java.io.File; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import com.alibaba.druid.util.StringUtils; +import jxl.Cell; +import jxl.Sheet; +import jxl.Workbook; +import jxl.format.*; +import jxl.write.Label; +import jxl.write.WritableCellFormat; +import jxl.write.WritableFont; +import jxl.write.WritableSheet; +import jxl.write.WritableWorkbook; + +public class ExcelUtils { + + public static WritableFont arial14font = null; + + public static File exportObjects(String fileName, String[] names, + String title, List objects) throws Exception { + File excelFile = new File("fileName.xls"); + WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); + WritableSheet sheet = wtwb.createSheet(title, 0); + sheet.getSettings().setDefaultColumnWidth(20); + WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15); + WritableCellFormat format = new WritableCellFormat(wfont); + WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20, + WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, + jxl.format.Colour.BLACK); + WritableCellFormat wcfFC = new WritableCellFormat(wfc); + wcfFC.setAlignment(Alignment.CENTRE); + wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); + // CellView cellView = new CellView(); + // cellView.setAutosize(true); //设置自动大小 + format.setAlignment(Alignment.LEFT); + format.setVerticalAlignment(VerticalAlignment.TOP); + sheet.mergeCells(0, 0, names.length - 1, 0); + sheet.addCell(new Label(0, 0, title, wcfFC)); + int rowNum = 2; + for (int i = 0; i < names.length; i++) { + sheet.addCell(new Label(i, 1, names[i], format)); + } + for (int j = 0; j < objects.size(); j++) { + String[] obj = objects.get(j); + for (int h = 0; h < obj.length; h++) { + sheet.addCell(new Label(h, rowNum, obj[h], format)); + } + rowNum = rowNum + 1; + + } + wtwb.write(); + wtwb.close(); + return excelFile; + } + + /** + * 导出excel,不需要第一行的title + * + * @param fileName + * @param names + * @param title + * @param objects + * @return + * @throws Exception + */ + public static File exportObjectsWithoutTitle(String fileName, + String[] names, String title, List objects) + throws Exception { + File excelFile = new File(fileName); + WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); + WritableSheet sheet = wtwb.createSheet(title, 0); + sheet.getSettings().setDefaultColumnWidth(20); + + // 第一行的格式 + WritableFont wfc = new WritableFont(WritableFont.ARIAL, 15, + WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, + jxl.format.Colour.BLACK); + WritableCellFormat wcfFC = new WritableCellFormat(wfc); + wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); + + // 设置字体以及单元格格式 + WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15); + WritableCellFormat format = new WritableCellFormat(wfont); + format.setAlignment(Alignment.LEFT); + format.setVerticalAlignment(VerticalAlignment.TOP); + + // 第一行写入标题 + for (int i = 0; i < names.length; i++) { + sheet.addCell(new Label(i, 0, names[i], wcfFC)); + } + + // 其余行依次写入数据 + int rowNum = 1; + for (int j = 0; j < objects.size(); j++) { + String[] obj = objects.get(j); + for (int h = 0; h < obj.length; h++) { + sheet.addCell(new Label(h, rowNum, obj[h], format)); + } + rowNum = rowNum + 1; + } + wtwb.write(); + wtwb.close(); + return excelFile; + } + + public static String createTempFile(String[] names, String title, List objects) throws Exception { + File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls"); + WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); + WritableSheet sheet = wtwb.createSheet(title, 0); + sheet.getSettings().setDefaultColumnWidth(20); + WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 15); + WritableCellFormat format = new WritableCellFormat(wfont); + WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20, + WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, + jxl.format.Colour.BLACK); + WritableCellFormat wcfFC = new WritableCellFormat(wfc); + wcfFC.setAlignment(Alignment.CENTRE); + wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); + // CellView cellView = new CellView(); + // cellView.setAutosize(true); //设置自动大小 + format.setAlignment(Alignment.LEFT); + format.setVerticalAlignment(VerticalAlignment.TOP); + sheet.mergeCells(0, 0, names.length - 1, 0); + sheet.addCell(new Label(0, 0, title, wcfFC)); + int rowNum = 2; + for (int i = 0; i < names.length; i++) { + sheet.addCell(new Label(i, 1, names[i], format)); + } + for (int j = 0; j < objects.size(); j++) { + String[] obj = objects.get(j); + for (int h = 0; h < obj.length; h++) { + sheet.addCell(new Label(h, rowNum, obj[h], format)); + } + rowNum = rowNum + 1; + } + wtwb.write(); + wtwb.close(); + return excelFile.getName(); + } + + public static String createCheckRandomTempFile(String[] names, String title, List objects,Map infoMap) throws Exception { + File excelFile = File.createTempFile(System.currentTimeMillis() + "", ".xls"); + WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); + WritableSheet sheet = wtwb.createSheet(title, 0); + sheet.getSettings().setDefaultColumnWidth(20); + WritableFont wfont = new WritableFont(WritableFont.createFont("楷书"), 14); + + WritableCellFormat format = new WritableCellFormat(wfont); + format.setBorder(Border.ALL, BorderLineStyle.THIN); + format.setAlignment(Alignment.CENTRE); + format.setVerticalAlignment(VerticalAlignment.CENTRE); + + WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20, + WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, + jxl.format.Colour.BLACK); + WritableCellFormat wcfFC = new WritableCellFormat(wfc); + wcfFC.setAlignment(Alignment.LEFT); + wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); + + WritableFont nameWfc = new WritableFont(WritableFont.ARIAL, 14, + WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, + jxl.format.Colour.BLACK); + WritableCellFormat nameFormat = new WritableCellFormat(nameWfc); + nameFormat.setBorder(Border.ALL, BorderLineStyle.THIN); + nameFormat.setAlignment(Alignment.CENTRE); + nameFormat.setVerticalAlignment(VerticalAlignment.CENTRE); + + WritableCellFormat infoFormat = new WritableCellFormat(wfont); + infoFormat.setAlignment(Alignment.LEFT); + infoFormat.setVerticalAlignment(VerticalAlignment.CENTRE); + + + sheet.mergeCells(0, 0, names.length - 1, 0); + sheet.addCell(new Label(0, 0, infoMap.get("title"), wcfFC)); + + sheet.addCell(new Label(0, 2, infoMap.get("info"), infoFormat)); + sheet.addCell(new Label(2, 2, infoMap.get("dvrnvr"), infoFormat)); + sheet.addCell(new Label(4, 2, infoMap.get("char"), infoFormat)); + sheet.addCell(new Label(0, 3, infoMap.get("infoPercent"), infoFormat)); + sheet.addCell(new Label(2, 3, infoMap.get("dvrnvrPercent"), infoFormat)); + sheet.addCell(new Label(4, 3, infoMap.get("charPercent"), infoFormat)); + + int rowNum = 5; + for (int i = 0; i < names.length; i++) { + sheet.addCell(new Label(i, 4, names[i], nameFormat)); + } + for (int j = 0; j < objects.size(); j++) { + String[] obj = objects.get(j); + for (int h = 0; h < obj.length; h++) { + sheet.addCell(new Label(h, rowNum, obj[h], format)); + } + rowNum = rowNum + 1; + } + wtwb.write(); + wtwb.close(); + return excelFile.getName(); + } + + + + public static String getContent(Sheet src, int rowNum, int colNum) { + return src.getRow(rowNum)[colNum].getContents().trim(); + } + + /** + * 从第i行开始到最后检测指定列的唯一性 + * + * @param src + * @param colNum + * @param fromRow + * 起始行 + * @return + */ + public static Boolean checkUnique(Sheet src, int colNum, int fromRow) { + Cell[] colCells = src.getColumn(colNum); + Set set = new HashSet(); + for (int i = fromRow; i < colCells.length; i++) { + if (!StringUtils.isEmpty(colCells[i].getContents()) + && !set.add(colCells[i].getContents())) { + return false; + } + } + return true; + } + + public static File getTempFile(String fileName) { + String dir = System.getProperty("java.io.tmpdir"); // 获取系统临时目录 + return new File(dir + File.separator + fileName); + } + + public static void main(String[] args) throws Exception { + String msg = "12345"; + System.out.println(msg.indexOf("@")); + } +} diff --git a/src/main/java/com/jsh/erp/utils/ExportExecUtil.java b/src/main/java/com/jsh/erp/utils/ExportExecUtil.java new file mode 100644 index 00000000..6ec7c371 --- /dev/null +++ b/src/main/java/com/jsh/erp/utils/ExportExecUtil.java @@ -0,0 +1,28 @@ +package com.jsh.erp.utils; + +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.OutputStream; + +public class ExportExecUtil { + + public static void showExec(File excelFile,String fileName,HttpServletResponse response) throws Exception{ + response.setContentType("application/octet-stream"); + fileName = new String(fileName.getBytes("gbk"),"ISO8859_1"); + response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\""); + FileInputStream fis = new FileInputStream(excelFile); + OutputStream out = response.getOutputStream(); + + int SIZE = 1024 * 1024; + byte[] bytes = new byte[SIZE]; + int LENGTH = -1; + while((LENGTH = fis.read(bytes)) != -1){ + out.write(bytes,0,LENGTH); + } + + out.flush(); + fis.close(); + } + +}