Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.kcs.action; import com.kcs.core.actions.BaseAction; import com.kcs.core.utilities.DateUtil; import com.kcs.core.utilities.DmsConstant; import com.kcs.dto.ComboBoxDto; import com.kcs.model.ReportCompareUtilizeFwd; import com.kcs.service.ReportCompareUtilizeFWDService; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellUtil; import org.springframework.beans.factory.annotation.Autowired; /** * * @author chawachote */ public class ReportCompareUtilizeFwdAction extends BaseAction { Logger logger = Logger.getLogger(this.getClass()); private String processDate; private String reportType; private List<ComboBoxDto> listReportType; private List<ReportCompareUtilizeFwd> list; private String fileType; private InputStream excelStream; private String fileName; private String fileReportName; private String hidDate; @Autowired ReportCompareUtilizeFWDService reportCompareUtilizeFWDService; private void initialCombo() throws Exception { setListReportType(reportCompareUtilizeFWDService.findAllReportTypeService()); } @Override public String success() throws Exception { initialCombo(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); hidDate = dateFormat.format(new Date()); return SUCCESS; } @Override public String search() throws Exception { logger.debug("= = = = = SEARCH = = = = ="); logger.debug("PALM : processDate >>> " + DateUtil.convertDateFromJsp(processDate) + " reportType >>> " + reportType); list = reportCompareUtilizeFWDService.findWithCriteriaService(reportType, DateUtil.convertDateFromJsp(processDate)); fileType = "PDF"; if ("RPT_TYP1_41".equals(reportType)) { fileReportName = "utilizeFwd1"; } else if ("RPT_TYP2_41".equals(reportType)) { fileReportName = "utilizeFwd2"; } return REPORT; //To change body of generated methods, choose Tools | Templates. } public String export() throws Exception { logger.debug("= = = = = EXPORT = = = = ="); logger.debug("PALM : processDate >>> " + DateUtil.convertDateFromJsp(processDate) + " reportType >>> " + reportType); list = reportCompareUtilizeFWDService.findWithCriteriaService(reportType, DateUtil.convertDateFromJsp(processDate)); fileType = "XLS"; HSSFWorkbook myWorkBook = new HSSFWorkbook(); HSSFSheet mySheet = myWorkBook.createSheet("Utilize_FWD"); if ("RPT_TYP1_41".equals(reportType)) { fileName = "Report_FWD_not_found_EES_(Trade_Finance)_CBS_(Remittance)_" + hidDate + ".xls"; Row rowHeader1 = mySheet.createRow(0); rowHeader1.createCell(0).setCellValue( "? FWD ? EES (Trade Finance) & CBS (Remittance)"); } else if ("RPT_TYP2_41".equals(reportType)) { fileName = "Report_EES_(Trade_Finance)_CBS_(Remittance)_not_found_FWD_" + hidDate + ".xls"; Row rowHeader1 = mySheet.createRow(0); rowHeader1.createCell(0).setCellValue( "? EES (Trade Finance) & CBS (Remittance)? FWD"); } // Cell cell1 = CellUtil.createCell(rowHeader1, 0,"? FWD ? EES (Trade Finance) & CBS (Remittance)"); // CellUtil.setAlignment(cell1, myWorkBook, CellStyle.ALIGN_CENTER); Row rowHeader2 = mySheet.createRow(1); rowHeader2.createCell(0).setCellValue(processDate); // Cell cell2 = CellUtil.createCell(rowHeader2, 1,processDate); // CellUtil.setAlignment(cell2, myWorkBook, CellStyle.ALIGN_CENTER); mySheet.addMergedRegion(new CellRangeAddress(0, //first row (0-based) 0, //last row (0-based) 0, //first column (0-based) 2 //last column (0-based) )); mySheet.addMergedRegion(new CellRangeAddress(1, //first row (0-based) 1, //last row (0-based) 0, //first column (0-based) 2 //last column (0-based) )); if (null != list && list.size() > 0) { int rownum = 2; Row row = mySheet.createRow(rownum++); int cellnum = 0; row.createCell(cellnum++).setCellValue("Curr"); row.createCell(cellnum++).setCellValue("Fi Arrg No"); row.createCell(cellnum++).setCellValue("Amount"); for (ReportCompareUtilizeFwd obj : list) { Row rowData = mySheet.createRow(rownum++); cellnum = 0; rowData.createCell(cellnum++).setCellValue(obj.getCURR() + ""); rowData.createCell(cellnum++).setCellValue(obj.getFI_ARRG_NO() + ""); rowData.createCell(cellnum++).setCellValue(obj.getAMT() + ""); } for (int j = 0; j < cellnum; j++) { mySheet.autoSizeColumn(j); } } ByteArrayOutputStream boas = new ByteArrayOutputStream(); myWorkBook.write(boas); setExcelStream(new ByteArrayInputStream(boas.toByteArray())); logger.debug("exportExcel : end..."); return "excel"; // return REPORT; } /** * @return the processDate */ public String getProcessDate() { return processDate; } /** * @param processDate the processDate to set */ public void setProcessDate(String processDate) { this.processDate = processDate; } /** * @return the reportType */ public String getReportType() { return reportType; } /** * @param reportType the reportType to set */ public void setReportType(String reportType) { this.reportType = reportType; } /** * @return the listReportType */ public List<ComboBoxDto> getListReportType() { return listReportType; } /** * @param listReportType the listReportType to set */ public void setListReportType(List<ComboBoxDto> listReportType) { this.listReportType = listReportType; } /** * @return the list */ public List<ReportCompareUtilizeFwd> getList() { return list; } /** * @param list the list to set */ public void setList(List<ReportCompareUtilizeFwd> list) { this.list = list; } /** * @return the fileType */ public String getFileType() { return fileType; } /** * @param fileType the fileType to set */ public void setFileType(String fileType) { this.fileType = fileType; } /** * @return the excelStream */ public InputStream getExcelStream() { return excelStream; } /** * @param excelStream the excelStream to set */ public void setExcelStream(InputStream excelStream) { this.excelStream = excelStream; } /** * @return the fileName */ public String getFileName() { return fileName; } /** * @param fileName the fileName to set */ public void setFileName(String fileName) { this.fileName = fileName; } /** * @return the fileReportName */ public String getFileReportName() { return fileReportName; } /** * @param fileReportName the fileReportName to set */ public void setFileReportName(String fileReportName) { this.fileReportName = fileReportName; } /** * @return the hidDate */ public String getHidDate() { return hidDate; } /** * @param hidDate the hidDate to set */ public void setHidDate(String hidDate) { this.hidDate = hidDate; } }