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 static com.kcs.action.FrfImpExpAction.logger; import com.kcs.core.actions.BaseAction; import com.kcs.core.utilities.DateUtil; import com.kcs.core.utilities.PaggingList; import com.kcs.core.utilities.Utility; import com.kcs.dto.DarDto; import com.kcs.model.Datasetfrf; import com.kcs.model.Datasetisi; import com.kcs.model.PositionReport; import com.kcs.service.DatasetIsiService; import com.kcs.service.PositionReportService; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; 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; /** * * @author LENOVO */ public class PositionReportAction extends BaseAction { Logger logger = Logger.getLogger(this.getClass()); private String dataSetDate; private PaggingList paging; private List<PositionReport> positionReportList; private String fileName; private InputStream excelStream; @Autowired private PositionReportService service; @Override public String success() throws Exception { dataSetDate = service.getDataSetDate(); if (Utility.isNull(getPaging())) { setPaging(new PaggingList()); } search(); return SUCCESS; } @Override public String search() throws Exception { paging = service.findMainListPaging(createPaginate(), DateUtil.getDateFromString(dataSetDate, DateUtil.DEFAULT_DATE_FORMAT)); return SEARCH; } public String print() throws Exception { positionReportList = service .findMainList(DateUtil.getDateFromString(dataSetDate, DateUtil.DEFAULT_DATE_FORMAT)); return "print"; } @Override public String export() throws Exception { positionReportList = service .findMainList(DateUtil.getDateFromString(dataSetDate, DateUtil.DEFAULT_DATE_FORMAT)); logger.debug("exportExcel : begin..."); logger.debug("exportExcel : list >>> " + positionReportList.size()); DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); HSSFWorkbook myWorkBook = new HSSFWorkbook(); HSSFSheet mySheet = myWorkBook.createSheet("Position Report"); setFileName("Position Report_" + dateFormat.format(new Date()) + "excel".concat(".xls")); if (null != positionReportList && positionReportList.size() > 0) { int rownum = 0; Row row = mySheet.createRow(rownum++); int cellnum = 0; row.createCell(cellnum++).setCellValue("dataSetDate"); row.createCell(cellnum++).setCellValue("curr"); row.createCell(cellnum++).setCellValue("bankNoteAmt"); row.createCell(cellnum++).setCellValue("nostroAmt"); row.createCell(cellnum++).setCellValue("plcmentAmt"); row.createCell(cellnum++).setCellValue("fibAmt"); row.createCell(cellnum++).setCellValue("exportAmt"); row.createCell(cellnum++).setCellValue("loanAmt"); row.createCell(cellnum++).setCellValue("otherAmt"); row.createCell(cellnum++).setCellValue("fwdbAmt"); row.createCell(cellnum++).setCellValue("vostroAmt"); row.createCell(cellnum++).setCellValue("brAmt"); row.createCell(cellnum++).setCellValue("firAmt"); row.createCell(cellnum++).setCellValue("fwdsAmt"); for (PositionReport obj : positionReportList) { Row rowData = mySheet.createRow(rownum++); cellnum = 0; rowData.createCell(cellnum++).setCellValue(obj.getDataSetDate() + ""); rowData.createCell(cellnum++).setCellValue(obj.getCurr() + ""); rowData.createCell(cellnum++).setCellValue(obj.getBankNoteAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getNostroAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getPlcmentAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getFibAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getExportAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getLoanAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getOtherAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getFwdbAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getVostroAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getBrAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getFirAmt() + ""); rowData.createCell(cellnum++).setCellValue(obj.getFwdsAmt() + ""); } 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 the dataSetDate */ public String getDataSetDate() { return dataSetDate; } /** * @param dataSetDate the dataSetDate to set */ public void setDataSetDate(String dataSetDate) { this.dataSetDate = dataSetDate; } /** * @return the paging */ public PaggingList getPaging() { return paging; } /** * @param paging the paging to set */ public void setPaging(PaggingList paging) { this.paging = paging; } /** * @return the service */ public PositionReportService getService() { return service; } /** * @param service the service to set */ public void setService(PositionReportService service) { this.service = service; } /** * @return the positionReportList */ public List<PositionReport> getPositionReportList() { return positionReportList; } /** * @param positionReportList the positionReportList to set */ public void setPositionReportList(List<PositionReport> positionReportList) { this.positionReportList = positionReportList; } /** * @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; } }