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 bean; import com.liferay.faces.portal.context.LiferayFacesContext; import com.liferay.portal.theme.ThemeDisplay; import java.io.File; import java.io.FileOutputStream; import java.io.Serializable; import java.util.Arrays; import java.util.Date; import java.util.List; import java.math.*; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.RequestScoped; import javax.faces.context.FacesContext; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Font; /** * * @author misteam */ @ManagedBean @RequestScoped public class TrialBalanceExport implements Serializable { /** * Creates a new instance of TrialBalanceExport */ public TrialBalanceExport() { } /* * properties */ @ManagedProperty(value = "#{exportData}") private ExportData exportData; @ManagedProperty(value = "#{financialReportData}") private FinancialReportData financialReportData; @ManagedProperty(value = "#{trialBalanceData}") private TrialBalanceData trialBalanceData; @ManagedProperty(value = "#{dataConvert}") private DataConvert dataConvert; /* * getter setter */ public ExportData getExportData() { return exportData == null ? exportData = new ExportData() : exportData; } public void setExportData(ExportData exportData) { this.exportData = exportData; } public FinancialReportData getFinancialReportData() { return financialReportData == null ? financialReportData = new FinancialReportData() : financialReportData; } public void setFinancialReportData(FinancialReportData financialReportData) { this.financialReportData = financialReportData; } public TrialBalanceData getTrialBalanceData() { return trialBalanceData == null ? trialBalanceData = new TrialBalanceData() : trialBalanceData; } public void setTrialBalanceData(TrialBalanceData trialBalanceData) { this.trialBalanceData = trialBalanceData; } public DataConvert getDataConvert() { return dataConvert == null ? dataConvert = new DataConvert() : dataConvert; } public void setDataConvert(DataConvert dataConvert) { this.dataConvert = dataConvert; } /* * methods */ public void export() { Integer columnNo; HSSFWorkbook workbook; HSSFSheet sheet; HSSFRow dataRow; HSSFCell cell; HSSFCellStyle cellStyle, boldStyle; HSSFFont font; ThemeDisplay themeDisplay = LiferayFacesContext.getInstance().getThemeDisplay(); getExportData().createFolder(null, themeDisplay, "Trial Balance Report", "DESCRIPTION"); if (getExportData().getFilename() == null || getExportData().getFilename().length() == 0) { getExportData().setFilename("Default(" + new Date() + ")"); } getExportData().setFilename(getExportData().getFilename().replace(":", "")); try { getExportData().setFilename(getExportData().getFilename().concat(".xls")); workbook = new HSSFWorkbook(); cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00")); font = workbook.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); boldStyle = workbook.createCellStyle(); boldStyle.setFont(font); sheet = workbook .createSheet("Trial Balance Report " + getFinancialReportData().getCurrentMonth() != null ? getDataConvert().monthConvert(getFinancialReportData().getCurrentMonth()) + " " + getFinancialReportData().getCurrentYear().toString() : getFinancialReportData().getCurrentYear().toString()); dataRow = sheet.createRow((short) 0); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("TRIAL BALANCE"); cell.setCellStyle(boldStyle); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue(getFinancialReportData().getCurrentMonth() != null ? getDataConvert().monthConvert(getFinancialReportData().getCurrentMonth()) + " " + getFinancialReportData().getCurrentYear().toString() : getFinancialReportData().getCurrentYear().toString()); cell.setCellStyle(boldStyle); dataRow = sheet.createRow(dataRow.getRowNum() + 1); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("Account Title"); cell.setCellStyle(boldStyle); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellValue("Debit"); cell.setCellStyle(boldStyle); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellValue("Credit"); cell.setCellStyle(boldStyle); if (getTrialBalanceData().getType() == 1) { for (int i = 0; i < getTrialBalanceData().getCombotb().size(); i++) { for (int ii = 0; ii < getTrialBalanceData().getCombotb().get(i).size(); ii++) { dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue(getDataConvert() .accountCodeConvert((String) getTrialBalanceData().getCombotb().get(i).get(ii)[0])); cell.setCellStyle(cellStyle); // dataRow.createCell(columnNo++).setCellValue(""); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue( ((BigDecimal) getTrialBalanceData().getCombotb().get(i).get(ii)[1]).doubleValue()); cell.setCellStyle(cellStyle); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue( ((BigDecimal) getTrialBalanceData().getCombotb().get(i).get(ii)[2]).doubleValue()); cell.setCellStyle(cellStyle); } if (i == getTrialBalanceData().getCombotb().size() - 1) { dataRow = sheet.createRow(dataRow.getRowNum() + 1); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("TOTAL:"); cell.setCellStyle(boldStyle); dataRow.createCell(columnNo++).setCellValue(""); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((getTrialBalanceData().getComboDebitTotal()).doubleValue())); cell.setCellStyle(boldStyle); System.out.println("debit total " + getDataConvert().convertAmount(getTrialBalanceData().getComboDebitTotal())); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((getTrialBalanceData().getComboCreditTotal()).doubleValue())); cell.setCellStyle(boldStyle); System.out.println("credit total " + getDataConvert().convertAmount(getTrialBalanceData().getComboCreditTotal())); } } } else if (getTrialBalanceData().getType() == 2) { for (int i = 0; i < getTrialBalanceData().getTrialBalance().size(); i++) { dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue((String) getTrialBalanceData().getTrialBalance().get(i).getAcctTitle()); cell.setCellStyle(cellStyle); // dataRow.createCell(columnNo++).setCellValue(""); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(getDataConvert() .convertAmount((BigDecimal) getTrialBalanceData().getTrialBalance().get(i).getDebit())); cell.setCellStyle(cellStyle); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(getDataConvert().convertAmount( (BigDecimal) getTrialBalanceData().getTrialBalance().get(i).getCredit())); cell.setCellStyle(cellStyle); if (i == getTrialBalanceData().getTrialBalance().size() - 1) { dataRow = sheet.createRow(dataRow.getRowNum() + 1); dataRow = sheet.createRow(dataRow.getRowNum() + 1); columnNo = 0; cell = dataRow.createCell(columnNo++); cell.setCellValue("TOTAL:"); cell.setCellStyle(boldStyle); dataRow.createCell(columnNo++).setCellValue(""); columnNo = 1; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue( getDataConvert().convertAmount(getTrialBalanceData().getTrialBalanceDebitTotal())); cell.setCellStyle(boldStyle); columnNo = 2; cell = dataRow.createCell(columnNo++); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue( getDataConvert().convertAmount(getTrialBalanceData().getTrialBalanceCreditTotal())); cell.setCellStyle(boldStyle); } } } FileOutputStream fileOutputStream = new FileOutputStream(getExportData().getFilename()); workbook.write(fileOutputStream); fileOutputStream.close(); getExportData().fileUploadByDL(getExportData().getFilename(), "Trial Balance Report", themeDisplay, null); File file = new File(getExportData().getFilename()); if (file.exists()) { file.delete(); } } catch (Exception e) { System.out.print("trialBalanceExport().export() " + e); FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "An error occurred while generating excel file."); FacesContext.getCurrentInstance().addMessage(null, message); } } }