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.report; import com.kcs.core.actions.BaseAction; import com.kcs.core.utilities.DateUtil; import com.kcs.model.Datasetfrf; import com.kcs.model.Journal; import com.kcs.service.CTBWithFTSByCurrService; import com.kcs.service.JournalService; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; 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.Row; import org.springframework.beans.factory.annotation.Autowired; public class ReportJournalSummaryAction extends BaseAction { private Logger logger = Logger.getLogger(this.getClass()); private String dataSetDate; private List<Journal> list; private File toBeUploaded; private String fileName; private InputStream excelStream; @Autowired private JournalService service; @Override public String success() throws Exception { setDataSetDate(getService().getBotDate()); return SUCCESS; } public String report() throws Exception { try { getLogger().debug("Report : begin..."); getLogger().debug("Criteria : dataSetDate >>> " + getDataSetDate() + " Convert >>> " + DateUtil.convertDateFromJsp(getDataSetDate())); setList(getService().findJournalSummaryByCriteria(DateUtil.convertDateFromJsp(getDataSetDate()))); getLogger().debug("Report : end..."); } catch (Exception e) { e.printStackTrace(); } return REPORT; } public String print() throws Exception { try { getLogger().debug("Print : begin..."); getLogger().debug("Criteria : dataSetDate >>> " + getDataSetDate() + " Convert >>> " + DateUtil.convertDateFromJsp(getDataSetDate())); setList(getService().findJournalSummaryByCriteria(DateUtil.convertDateFromJsp(getDataSetDate()))); getLogger().debug("Print : end..."); } catch (Exception e) { e.printStackTrace(); } return "print"; } @Override public String export() throws Exception { list = service.findJournalSummaryByCriteria(DateUtil.convertDateFromJsp(dataSetDate)); getLogger().debug("exportExcel : begin..."); getLogger().debug("exportExcel : list >>> " + list); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); HSSFWorkbook myWorkBook = new HSSFWorkbook(); HSSFSheet mySheet = myWorkBook.createSheet("Journal"); setFileName("Export Data Journal (Summary)" + dateFormat.format(new Date()) + "excel".concat(".xls")); if (null != list && list.size() > 0) { int rownum = 0; Row row = mySheet.createRow(rownum++); int cellnum = 0; row.createCell(cellnum++).setCellValue("JournalGroup"); row.createCell(cellnum++).setCellValue("Ccy"); row.createCell(cellnum++).setCellValue("BuyAmt"); row.createCell(cellnum++).setCellValue("SellAmt"); row.createCell(cellnum++).setCellValue("TranType"); for (Journal obj : list) { Row rowData = mySheet.createRow(rownum++); cellnum = 0; rowData.createCell(cellnum++).setCellValue(obj.getJOURNAL_GROUP() + ""); rowData.createCell(cellnum++).setCellValue(obj.getCCY() + ""); rowData.createCell(cellnum++).setCellValue(obj.getBUY_AMT() + ""); rowData.createCell(cellnum++).setCellValue(obj.getSELL_AMT() + ""); rowData.createCell(cellnum++).setCellValue(obj.getTRANS_TYPE() + ""); } for (int j = 0; j < cellnum; j++) { mySheet.autoSizeColumn(j); } } ByteArrayOutputStream boas = new ByteArrayOutputStream(); myWorkBook.write(boas); setExcelStream(new ByteArrayInputStream(boas.toByteArray())); getLogger().debug("exportExcel : end..."); return "excel"; } /** * @return the logger */ public Logger getLogger() { return logger; } /** * @param logger the logger to set */ public void setLogger(Logger logger) { this.logger = logger; } /** * @return the dataSetDate */ public String getDataSetDate() { return dataSetDate; } /** * @param dataSetDate the dataSetDate to set */ public void setDataSetDate(String dataSetDate) { this.dataSetDate = dataSetDate; } /** * @return the list */ public List<Journal> getList() { return list; } /** * @param list the list to set */ public void setList(List<Journal> list) { this.list = list; } /** * @return the service */ public JournalService getService() { return service; } /** * @param service the service to set */ public void setService(JournalService service) { this.service = service; } /** * @return the toBeUploaded */ public File getToBeUploaded() { return toBeUploaded; } /** * @param toBeUploaded the toBeUploaded to set */ public void setToBeUploaded(File toBeUploaded) { this.toBeUploaded = toBeUploaded; } /** * @return the fileName */ public String getFileName() { return fileName; } /** * @param fileName the fileName to set */ public void setFileName(String fileName) { this.fileName = fileName; } /** * @return the excelStream */ public InputStream getExcelStream() { return excelStream; } /** * @param excelStream the excelStream to set */ public void setExcelStream(InputStream excelStream) { this.excelStream = excelStream; } }