diff --git a/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java b/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java index 573acfc8..4c0d3087 100644 --- a/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java +++ b/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java @@ -34,7 +34,10 @@ import com.interplug.qcast.util.InterfaceQsp; import com.interplug.qcast.util.PdfUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -44,6 +47,8 @@ import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.springframework.beans.factory.annotation.Autowired; @@ -977,28 +982,30 @@ public class EstimateService { // SchDrawingFlg (1 : 견적서,2 : 발전시뮬레이션, 3 : 도면, 4 : 가대) // ex) 1|2|3|4 - if (estimateRequest.getSchDrawingFlg().indexOf("1") > -1) { - arrSection[iSection] = "div.section1"; - iSection++; - arrSection[iSection] = "div.section2"; - iSection++; - } + if (!StringUtils.isEmpty(estimateRequest.getSchDrawingFlg())) { + if (estimateRequest.getSchDrawingFlg().indexOf("1") > -1) { + arrSection[iSection] = "div.section1"; + iSection++; + arrSection[iSection] = "div.section2"; + iSection++; + } - if (estimateRequest.getSchDrawingFlg().indexOf("2") > -1) { - arrSection[iSection] = "div.section3"; // 발전시뮬레이션 - iSection++; - } + if (estimateRequest.getSchDrawingFlg().indexOf("2") > -1) { + arrSection[iSection] = "div.section3"; // 발전시뮬레이션 + iSection++; + } - if (estimateRequest.getSchDrawingFlg().indexOf("3") > -1) { - arrSection[iSection] = "div.section4"; // 도면 - iSection++; - arrSection[iSection] = "div.section5"; // 도면 - iSection++; - } + if (estimateRequest.getSchDrawingFlg().indexOf("3") > -1) { + arrSection[iSection] = "div.section4"; // 도면 + iSection++; + arrSection[iSection] = "div.section5"; // 도면 + iSection++; + } - if (estimateRequest.getSchDrawingFlg().indexOf("4") > -1) { - arrSection[iSection] = "div.section6"; - iSection++; + if (estimateRequest.getSchDrawingFlg().indexOf("4") > -1) { + arrSection[iSection] = "div.section6"; + iSection++; + } } // pdf 다운로드 @@ -1006,16 +1013,52 @@ public class EstimateService { } else { + Workbook workbook = null; + String excelTemplateNam = "excel_download_quotation_detail_template.xlsx"; ExcelUtil excelUtil = new ExcelUtil(); - excelUtil.download( - request, - response, - excelUtil.convertVoToMap(estimateResponse), - excelUtil.convertListToMap(estimateItemList), - estimateRequest.getFileName(), - excelTemplateNam); + byte[] excelBytes = + excelUtil.download( + request, + response, + excelUtil.convertVoToMap(estimateResponse), + excelUtil.convertListToMap(estimateItemList), + excelTemplateNam); + + InputStream in = new ByteArrayInputStream(excelBytes); + workbook = WorkbookFactory.create(in); // JXLS POI 엑셀로 재변환 + + // SchDrawingFlg (1 : 견적서,2 : 발전시뮬레이션, 3 : 도면, 4 : 가대) + // ex) 1|2|3|4 + if (!StringUtils.isEmpty(estimateRequest.getSchDrawingFlg())) { + if (estimateRequest.getSchDrawingFlg().indexOf("1") < 0) { + workbook.removeSheetAt(workbook.getSheetIndex("見積書")); + workbook.removeSheetAt(workbook.getSheetIndex("特異事項")); + } + + if (estimateRequest.getSchDrawingFlg().indexOf("2") < 0) { + workbook.removeSheetAt(workbook.getSheetIndex("発電シミュレーション")); + } + + if (estimateRequest.getSchDrawingFlg().indexOf("3") < 0) { + workbook.removeSheetAt(workbook.getSheetIndex("割付図・系統図")); + workbook.removeSheetAt(workbook.getSheetIndex("架台図")); + } + } + + // 추후 개발 (가대중량표) + if (estimateRequest.getSchDrawingFlg().indexOf("4") < 0) {} + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + workbook.write(byteArrayOutputStream); + excelBytes = byteArrayOutputStream.toByteArray(); + + response.setHeader( + "Content-Disposition", + "attachment; filename=\"" + estimateRequest.getFileName() + ".xlsx\""); + response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); + response.getOutputStream().write(excelBytes); } } catch (Exception e) { diff --git a/src/main/java/com/interplug/qcast/util/ExcelUtil.java b/src/main/java/com/interplug/qcast/util/ExcelUtil.java index 9d1a543b..3f75ead1 100644 --- a/src/main/java/com/interplug/qcast/util/ExcelUtil.java +++ b/src/main/java/com/interplug/qcast/util/ExcelUtil.java @@ -29,40 +29,43 @@ public class ExcelUtil { * @param response HttpServletResponse * @param map 엑셀 출력데이터 * @param list 엑셀 출력 목록 데이터 - * @param fileName 다운로드 파일명 * @param templateFileName * @throws ParsePropertyException * @throws InvalidFormatException */ - public void download( + public byte[] download( HttpServletRequest request, HttpServletResponse response, Map map, List> list, - String fileName, String templateFileName) throws ParsePropertyException, InvalidFormatException { + + byte[] excelBytes = null; + try { String templateFilePath = "template/excel/" + templateFileName; InputStream templateStream = PdfUtil.class.getClassLoader().getResourceAsStream(templateFilePath); - InputStream is = new BufferedInputStream(templateStream); + // InputStream is = new BufferedInputStream(templateStream); - response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xlsx\""); - response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); - - try (OutputStream os = response.getOutputStream()) { + try (InputStream is = new BufferedInputStream(templateStream)) { Context context = new Context(); context.putVar("data", map); context.putVar("list", list); - JxlsHelper.getInstance().processTemplate(is, os, context); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + JxlsHelper.getInstance().processTemplate(is, byteArrayOutputStream, context); + + excelBytes = byteArrayOutputStream.toByteArray(); } } catch (Exception e) { log.debug(e.getMessage()); } + + return excelBytes; } /** diff --git a/src/main/resources/template/excel/excel_download_quotation_detail_template.xlsx b/src/main/resources/template/excel/excel_download_quotation_detail_template.xlsx index e7d0fac3..325d7ff7 100644 Binary files a/src/main/resources/template/excel/excel_download_quotation_detail_template.xlsx and b/src/main/resources/template/excel/excel_download_quotation_detail_template.xlsx differ