List of usage examples for org.apache.poi.ss.usermodel CellStyle setWrapText
void setWrapText(boolean wrapped);
From source file:com.crimelab.service.ChemistryServiceImpl.java
@Override public Workbook createMonthlyReport(HttpSession session, String month) { Workbook wb = null;/*from w w w . j av a 2s . c o m*/ try { InputStream inp = session.getServletContext() .getResourceAsStream("/WEB-INF/templates/DrugMonthlyReport.xls"); wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); CellStyle cs1 = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); CellStyle bl = wb.createCellStyle(); CellStyle br = wb.createCellStyle(); CellStyle bt = wb.createCellStyle(); CellStyle bb = wb.createCellStyle(); CellStyle stf = wb.createCellStyle(); cs1.setWrapText(true); cs2.setAlignment(ALIGN_CENTER); bt.setBorderTop(BORDER_THIN); bb.setBorderBottom(BORDER_THIN); bl.setBorderLeft(BORDER_THIN); br.setBorderRight(BORDER_THIN); stf.setShrinkToFit(true); Row intro1 = sheet.createRow(7); Cell in1 = intro1.createCell(0);//.setCellValue("DRUG INVETORY COVERED PERIOD JANUARY-DECEMBER CY-" +month.split("-")[0]); in1.setCellValue("DRUG INVENTORY COVERED PERIOD JANUARY-DECEMBER CY-" + month.split("-")[0]); in1.setCellStyle(cs1); in1.setCellStyle(cs2); Row intro2 = sheet.createRow(9); Cell in2 = intro2.createCell(0);//.setCellValue("SUMMARY OF SEIZED/SURRENDERED/RECOVERED OF DRUG EVIDENCES FROM NEGATION OPERATIONS FROM LAW ENFORCEMENTS, PHARMACEUTICAL COMPANIES AND SIMILAR ESTABLISHMENTS FOR THE MONTH OF "+month); in2.setCellValue( "SUMMARY OF SEIZED/SURRENDERED/RECOVERED OF DRUG EVIDENCES FROM NEGATION OPERATIONS FROM LAW ENFORCEMENTS, PHARMACEUTICAL COMPANIES AND SIMILAR ESTABLISHMENTS FOR THE MONTH OF " + month); in2.setCellStyle(cs1); in2.setCellStyle(cs2); in2.setCellStyle(stf); int ctr = 12; //initial Row row = sheet.createRow(ctr); month = month.split("-")[1]; //System.out.println("GAC " + chemistryDAO.getAllChemistry(month).isEmpty()); for (Chemistry chemistry : chemistryDAO.getAllChemistry(month)) { //System.out.println("Test " + chemistry.getTimeDateReceived()); Cell cell0 = row.createCell(0); cell0.setCellValue(chemistry.getTimeDateReceived()); cell0.setCellStyle(bt); cell0.setCellStyle(bb); cell0.setCellStyle(bl); cell0.setCellStyle(br); Cell cell1 = row.createCell(1);//.setCellValue(chemistry.getReportNo()); cell1.setCellValue(chemistry.getReportNo()); cell1.setCellStyle(bt); cell1.setCellStyle(bb); cell1.setCellStyle(bl); cell1.setCellStyle(br); Cell cell2 = row.createCell(2);//.setCellValue(chemistry.getRequestingParty()); cell2.setCellValue(chemistry.getRequestingParty()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell2.setCellStyle(bl); cell2.setCellStyle(br); Cell cell3 = row.createCell(3);//.setCellValue(chemistry.getDescriptionOfEvidence()); cell3.setCellValue(chemistry.getDescriptionOfEvidence()); cell3.setCellStyle(bt); cell3.setCellStyle(bb); cell3.setCellStyle(bl); cell3.setCellStyle(br); Cell cell4 = row.createCell(4);//.setCellValue(chemistry.getSpecimenWeight()); cell4.setCellValue(chemistry.getSpecimenWeight()); cell4.setCellStyle(bt); cell4.setCellStyle(bb); cell4.setCellStyle(bl); cell4.setCellStyle(br); Cell cell5 = row.createCell(5);//.setCellValue(chemistry.getCustody()); cell5.setCellValue(chemistry.getCustody()); cell5.setCellStyle(bt); cell5.setCellStyle(bb); cell5.setCellStyle(bl); cell5.setCellStyle(br); Cell cell6 = row.createCell(6);//.setCellValue(chemistry.getSuspects()); cell6.setCellValue(chemistry.getSuspects()); cell6.setCellStyle(bt); cell6.setCellStyle(bb); cell6.setCellStyle(bl); cell6.setCellStyle(br); Cell cell7 = row.createCell(7);//.setCellValue(chemistry.getTypeOfOperation()); cell7.setCellValue(chemistry.getTypeOfOperation()); cell7.setCellStyle(bt); cell7.setCellStyle(bb); cell7.setCellStyle(bl); cell7.setCellStyle(br); Cell cell8 = row.createCell(8);//.setCellValue(chemistry.getPlaceOfOperation()); cell8.setCellValue(chemistry.getPlaceOfOperation()); cell8.setCellStyle(bt); cell8.setCellStyle(bb); cell8.setCellStyle(bl); cell8.setCellStyle(br); row = sheet.createRow(ctr += 1); } } catch (Exception e) { e.printStackTrace(); } return wb; }
From source file:com.crimelab.service.CriminalServiceImpl.java
@Override public Workbook createTenprintsCards(HttpSession session, String month) { Workbook wb = null;//from w ww. j ava 2 s .c o m try { InputStream inp = session.getServletContext() .getResourceAsStream("/WEB-INF/templates/TenprintsCards.xls"); wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); CellStyle cs1 = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); CellStyle bl = wb.createCellStyle(); CellStyle br = wb.createCellStyle(); CellStyle bt = wb.createCellStyle(); CellStyle bb = wb.createCellStyle(); cs1.setWrapText(true); cs2.setAlignment(ALIGN_CENTER); bt.setBorderTop(BORDER_THIN); bb.setBorderBottom(BORDER_THIN); bl.setBorderLeft(BORDER_THIN); br.setBorderRight(BORDER_THIN); Row intro1 = sheet.createRow(9); Cell in1 = intro1.createCell(0); in1.setCellValue("Submitted Tenprints Cards from " + month.split("-")[0]); in1.setCellStyle(cs1); in1.setCellStyle(cs2); int ctr = 11; //initial Row row = sheet.createRow(ctr); month = month.split("-")[1]; //JOptionPane.showMessageDialog(null, criminalDAO.getAllCriminal(month)); for (Criminal criminal : criminalDAO.getAllCriminal(month)) { //JOptionPane.showMessageDialog(null, criminal.getReportNo()); Cell cell2 = row.createCell(2);//.setCellValue(criminal.getRequestingParty()); cell2.setCellValue(criminal.getLastName()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell2.setCellStyle(bl); cell2.setCellStyle(br); Cell cell3 = row.createCell(3);//.setCellValue(criminal.getDescriptionOfEvidence()); cell3.setCellValue(criminal.getFirstName()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell3.setCellStyle(bl); cell3.setCellStyle(br); Cell cell4 = row.createCell(4);//.setCellValue(criminal.getSpecimenWeight()); cell4.setCellValue(criminal.getMiddleName()); cell4.setCellStyle(bt); cell4.setCellStyle(bb); cell4.setCellStyle(bl); cell4.setCellStyle(br); Cell cell5 = row.createCell(5);//.setCellValue(criminal.getCustody()); cell5.setCellValue(criminal.getTimeDateArrest()); cell5.setCellStyle(bt); cell5.setCellStyle(bb); cell5.setCellStyle(bl); cell5.setCellStyle(br); Cell cell6 = row.createCell(6);//.setCellValue(criminal.getSuspects()); cell6.setCellValue(criminal.getOffenseCharge()); cell6.setCellStyle(bt); cell6.setCellStyle(bb); cell6.setCellStyle(bl); cell6.setCellStyle(br); row = sheet.createRow(ctr += 1); return wb; } } catch (Exception e) { e.printStackTrace(); } return wb; }
From source file:com.crimelab.service.FirearmsServiceImpl.java
@Override public Workbook createFirearmsCases(HttpSession session, String month) { Workbook wb = null;// www . j ava2 s. c om try { InputStream inp = session.getServletContext() .getResourceAsStream("/WEB-INF/templates/FirearmsCases.xls"); wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); CellStyle cs1 = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); CellStyle bl = wb.createCellStyle(); CellStyle br = wb.createCellStyle(); CellStyle bt = wb.createCellStyle(); CellStyle bb = wb.createCellStyle(); cs1.setWrapText(true); cs2.setAlignment(ALIGN_CENTER); bt.setBorderTop(BORDER_THIN); bb.setBorderBottom(BORDER_THIN); bl.setBorderLeft(BORDER_THIN); br.setBorderRight(BORDER_THIN); Row intro1 = sheet.createRow(9); Cell in1 = intro1.createCell(0); in1.setCellValue("Period Covered:"); in1.setCellStyle(cs1); in1.setCellStyle(cs2); Row intro2 = sheet.createRow(10); Cell in2 = intro2.createCell(0); in1.setCellValue(month.split("-")[0]); in1.setCellStyle(cs1); in1.setCellStyle(cs2); int ctr = 12; //initial Row row = sheet.createRow(ctr); month = month.split("-")[1]; //JOptionPane.showMessageDialog(null, firearmsDAO.getAllFirearms(month)); for (Firearms firearms : firearmsDAO.getAllFirearms(month)) { //JOptionPane.showMessageDialog(null, firearms.getReportNo()); Cell cell0 = row.createCell(0); cell0.setCellValue(firearms.getReportNo()); cell0.setCellStyle(bt); cell0.setCellStyle(bb); cell0.setCellStyle(bl); cell0.setCellStyle(br); Cell cell1 = row.createCell(1);//.setCellValue(firearms.getReportNo()); cell1.setCellValue(firearms.getExaminerName()); cell1.setCellStyle(bt); cell1.setCellStyle(bb); cell1.setCellStyle(bl); cell1.setCellStyle(br); Cell cell2 = row.createCell(2);//.setCellValue(firearms.getRequestingParty()); cell2.setCellValue(firearms.getCaseType()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell2.setCellStyle(bl); cell2.setCellStyle(br); Cell cell3 = row.createCell(3);//.setCellValue(firearms.getDescriptionOfEvidence()); cell3.setCellValue(firearms.getVictim()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell3.setCellStyle(bl); cell3.setCellStyle(br); Cell cell4 = row.createCell(4);//.setCellValue(firearms.getSpecimenWeight()); cell4.setCellValue(firearms.getSuspect()); cell4.setCellStyle(bt); cell4.setCellStyle(bb); cell4.setCellStyle(bl); cell4.setCellStyle(br); Cell cell5 = row.createCell(5);//.setCellValue(firearms.getCustody()); cell5.setCellValue(firearms.getTimeDateIncident()); cell5.setCellStyle(bt); cell5.setCellStyle(bb); cell5.setCellStyle(bl); cell5.setCellStyle(br); Cell cell6 = row.createCell(6);//.setCellValue(firearms.getSuspects()); cell6.setCellValue(firearms.getPlaceOfIncident()); cell6.setCellStyle(bt); cell6.setCellStyle(bb); cell6.setCellStyle(bl); cell6.setCellStyle(br); Cell cell7 = row.createCell(7);//.setCellValue(firearms.getTypeOfOperation()); cell7.setCellValue(firearms.getRequestingParty()); cell7.setCellStyle(bt); cell7.setCellStyle(bb); cell7.setCellStyle(bl); cell7.setCellStyle(br); Cell cell8 = row.createCell(8);//.setCellValue(firearms.getPlaceOfOperation()); cell8.setCellValue(firearms.getInvestigator()); cell8.setCellStyle(bt); cell8.setCellStyle(bb); cell8.setCellStyle(bl); cell8.setCellStyle(br); Cell cell9 = row.createCell(9);//.setCellValue(firearms.getPlaceOfOperation()); cell9.setCellValue(firearms.getCaseStatus()); cell9.setCellStyle(bt); cell9.setCellStyle(bb); cell9.setCellStyle(bl); cell9.setCellStyle(br); row = sheet.createRow(ctr += 1); return wb; } } catch (Exception e) { e.printStackTrace(); } return wb; }
From source file:com.crimelab.service.MedicoLegalServiceImpl.java
@Override public Workbook createMedicoMonthly(HttpSession session, String month) { Workbook wb = null;/*from w w w . j a v a 2s .co m*/ try { InputStream inp = session.getServletContext() .getResourceAsStream("/WEB-INF/templates/MedicoMonthly.xls"); wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); CellStyle cs1 = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); CellStyle bl = wb.createCellStyle(); CellStyle br = wb.createCellStyle(); CellStyle bt = wb.createCellStyle(); CellStyle bb = wb.createCellStyle(); cs1.setWrapText(true); cs2.setAlignment(ALIGN_CENTER); bt.setBorderTop(BORDER_THIN); bb.setBorderBottom(BORDER_THIN); bl.setBorderLeft(BORDER_THIN); br.setBorderRight(BORDER_THIN); Row intro1 = sheet.createRow(9); Cell in1 = intro1.createCell(0); in1.setCellValue("Period Covered:"); in1.setCellStyle(cs1); in1.setCellStyle(cs2); Row intro2 = sheet.createRow(10); Cell in2 = intro2.createCell(0); in1.setCellValue(month.split("-")[0]); in1.setCellStyle(cs1); in1.setCellStyle(cs2); int ctr = 12; //initial Row row = sheet.createRow(ctr); month = month.split("-")[1]; //JOptionPane.showMessageDialog(null, medicoLegalDAO.getAllMedicoLegal(month)); for (MedicoLegal medicoLegal : medicoLegalDAO.getAllMedicoLegal(month)) { //JOptionPane.showMessageDialog(null, medicoLegal.getReportNo()); Cell cell0 = row.createCell(0); cell0.setCellValue(medicoLegal.getCaseNo()); cell0.setCellStyle(bt); cell0.setCellStyle(bb); cell0.setCellStyle(bl); cell0.setCellStyle(br); Cell cell1 = row.createCell(1);//.setCellValue(medicoLegal.getReportNo()); cell1.setCellValue(medicoLegal.getExaminerName()); cell1.setCellStyle(bt); cell1.setCellStyle(bb); cell1.setCellStyle(bl); cell1.setCellStyle(br); Cell cell2 = row.createCell(2);//.setCellValue(medicoLegal.getRequestingParty()); cell2.setCellValue(medicoLegal.getCaseType()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell2.setCellStyle(bl); cell2.setCellStyle(br); Cell cell3 = row.createCell(3);//.setCellValue(medicoLegal.getDescriptionOfEvidence()); cell3.setCellValue(medicoLegal.getVictim()); cell2.setCellStyle(bt); cell2.setCellStyle(bb); cell3.setCellStyle(bl); cell3.setCellStyle(br); Cell cell4 = row.createCell(4);//.setCellValue(medicoLegal.getSpecimenWeight()); cell4.setCellValue(medicoLegal.getSuspect()); cell4.setCellStyle(bt); cell4.setCellStyle(bb); cell4.setCellStyle(bl); cell4.setCellStyle(br); Cell cell5 = row.createCell(5);//.setCellValue(medicoLegal.getCustody()); cell5.setCellValue(medicoLegal.getTimeDateReceived()); cell5.setCellStyle(bt); cell5.setCellStyle(bb); cell5.setCellStyle(bl); cell5.setCellStyle(br); Cell cell6 = row.createCell(6);//.setCellValue(medicoLegal.getSuspects()); cell6.setCellValue(medicoLegal.getFindings()); cell6.setCellStyle(bt); cell6.setCellStyle(bb); cell6.setCellStyle(bl); cell6.setCellStyle(br); row = sheet.createRow(ctr += 1); return wb; } } catch (Exception e) { e.printStackTrace(); } return wb; }
From source file:com.dituiba.excel.BaseExcelService.java
License:Apache License
/** * ?//ww w. ja v a 2 s . co m * @param sheet * @param row * @param data * @return */ public static Row addRow(Sheet sheet, int row, String[] data) { Row sheetRow = sheet.createRow(row); CellStyle style = sheet.getWorkbook().createCellStyle(); // ? style.setWrapText(true); for (int i = 0; i < data.length; i++) { Cell cell = sheetRow.createCell(i); cell.setCellValue(data[i]); cell.setCellStyle(style); } return sheetRow; }
From source file:com.dituiba.excel.ExportTableService.java
License:Apache License
public void doExport() { Collection<CellBean> cellBeans = tableBean.getCellBeans(); if (ObjectHelper.isNotEmpty(cellBeans)) { for (CellBean cellBean : cellBeans) { if (cellBean.getXSize() > 1 || cellBean.getYSize() > 1) { log.debug("??{}", JsonUtil.toJSON(cellBean)); CellRangeAddress range = new CellRangeAddress(cellBean.getRowIndex(), cellBean.getRowIndex() + cellBean.getYSize() - 1, cellBean.getColumnIndex(), cellBean.getColumnIndex() + cellBean.getXSize() - 1); sheet.addMergedRegion(range); }// w w w . j av a2 s . co m log.debug("set row:{},column:{},content:{}", cellBean.getRowIndex(), cellBean.getColumnIndex(), cellBean.getContent()); Cell cell = sheet.getRow(cellBean.getRowIndex()).getCell(cellBean.getColumnIndex()); cell.setCellValue(cellBean.getContent()); CellStyle cellStyle = cell.getCellStyle(); if (cellStyle == null) { cellStyle = sheet.getWorkbook().createCellStyle(); } if (cellBean.isAlignCenter()) { cellStyle.setAlignment(CellStyle.ALIGN_CENTER);// } if (cellBean.isVerticalCenter()) { cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// } cellStyle.setWrapText(cellBean.isWrapText()); cell.setCellStyle(cellStyle); } } }
From source file:com.efficio.fieldbook.service.ExportExcelServiceImpl.java
License:Open Source License
public FileOutputStream exportFieldMapToExcel(String fileName, UserFieldmap userFieldMap) throws FieldbookException { Locale locale = LocaleContextHolder.getLocale(); boolean isTrial = userFieldMap.isTrial(); // Summary of Trial/Nursery, Field and Planting Details String summaryOfFieldbookFieldPlantingDetailsLabel = messageSource .getMessage("fieldmap.header.summary.for.trial", null, locale); //SUMMARY OF TRIAL, FIELD AND PLANTING DETAILS String selectedFieldbookLabel = messageSource.getMessage("fieldmap.trial.selected.trial", null, locale); //Selected Trial: if (!isTrial) { summaryOfFieldbookFieldPlantingDetailsLabel = messageSource .getMessage("fieldmap.header.summary.for.nursery", null, locale); //SUMMARY OF NURSERY, FIELD AND PLANTING DETAILS selectedFieldbookLabel = messageSource.getMessage("fieldmap.nursery.selected.nursery", null, locale); //Selected Nursery: }// ww w.j av a 2s . c o m String selectedFieldbookValue = userFieldMap.getBlockName(); String orderHeader = messageSource.getMessage("fieldmap.trial.order", null, locale); String studyHeader = messageSource.getMessage((isTrial ? "fieldmap.trial" : "fieldmap.nursery"), null, locale); String instanceHeader = messageSource.getMessage("fieldmap.trial.instance", null, locale); String entriesCountHeader = messageSource.getMessage("fieldmap.trial.entry.count", null, locale); String repsCountHeader = messageSource.getMessage("fieldmap.trial.reps.count", null, locale); String plotsNeededHeader = messageSource.getMessage("fieldmap.trial.plots.needed", null, locale); String totalPlotsHeader = messageSource.getMessage("fieldmap.trial.total.number.of.plots", null, locale); String datasetNameHeader = messageSource.getMessage("fieldmap.nursery.dataset", null, locale); // Field And Block Details String fieldAndBlockDetailsLabel = messageSource.getMessage("fieldmap.trial.field.and.block.details", null, locale); //FIELD AND BLOCK DETAILS String fieldLocationLabel = messageSource.getMessage("fieldmap.label.field.location", null, locale); //Field Location String fieldLocationValue = userFieldMap.getLocationName(); String fieldNameLabel = messageSource.getMessage("fieldmap.label.field.name", null, locale); //Field Name String fieldNameValue = userFieldMap.getFieldName(); String blockNameLabel = messageSource.getMessage("fieldmap.label.block.name", null, locale); //Block Name String blockNameValue = userFieldMap.getBlockName(); // Row, Range & Plot Details String rowRangePlotDetailsLabel = messageSource.getMessage("fieldmap.trial.row.and.range.and.plot.details", null, locale); //ROW, RANGE AND PLOT DETAILS String blockCapacityLabel = messageSource.getMessage("fieldmap.label.block.capacity", null, locale); //Block Capacity String blockCapacityValue = userFieldMap.getBlockCapacityString(messageSource); //e.g. "10 Columns, 10 Ranges" String rowsPerPlotLabel = messageSource.getMessage("fieldmap.label.rows.per.plot", null, locale); //Rows per Plot int rowsPerPlotValue = userFieldMap.getNumberOfRowsPerPlot(); String columnsLabel = messageSource.getMessage("fieldmap.label.columns", null, locale); //Columns Integer columnsValue = userFieldMap.getNumberOfColumnsInBlock(); // 10 String machineCapacityLabel = messageSource.getMessage("fieldmap.label.row.capacity.machine", null, locale); //machine row capacity Integer machineCapacityValue = userFieldMap.getMachineRowCapacity(); //Planting Details String plantingDetailsLabel = messageSource.getMessage("fieldmap.header.planting.details", null, locale); //PLANTING DETAILS String startingCoordinatesLabel = messageSource.getMessage("fieldmap.label.starting.coordinates", null, locale); //Starting Coordinates String startingCoordinatesValue = userFieldMap.getStartingCoordinateString(messageSource); // Column 1, Range 1 String plantingOrderLabel = messageSource.getMessage("fieldmap.label.planting.order", null, locale); //Planting Order String plantingOrderValue = userFieldMap.getPlantingOrderString(messageSource); //"Row/Column" or "Serpentine" // FieldMap String fieldMapLabel = messageSource.getMessage("fieldmap.header.fieldmap", null, locale); //FIELD MAP String rowsLabel = messageSource.getMessage("fieldmap.label.rows", null, locale); //Rows String columnLabel = messageSource.getMessage("fieldmap.label.capitalized.column", null, locale); //Column String rangeLabel = messageSource.getMessage("fieldmap.label.capitalized.range", null, locale); //Range try { //Create workbook HSSFWorkbook workbook = new HSSFWorkbook(); String summaryLabelSheet = messageSource.getMessage("fieldmap.header.excel.summary", null, locale); Sheet summarySheet = workbook.createSheet(summaryLabelSheet); Sheet fieldMapSheet = workbook.createSheet(fieldMapLabel); CellStyle labelStyle = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); labelStyle.setFont(font); CellStyle wrapStyle = workbook.createCellStyle(); wrapStyle.setWrapText(true); wrapStyle.setAlignment(CellStyle.ALIGN_CENTER); CellStyle mainHeaderStyle = workbook.createCellStyle(); HSSFPalette palette = workbook.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(179, 165, 165); // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here mainHeaderStyle.setFillForegroundColor(palIndex); mainHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); CellStyle mainSubHeaderStyle = workbook.createCellStyle(); HSSFPalette paletteSubHeader = workbook.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColorSubHeader = paletteSubHeader.findSimilarColor(190, 190, 186); // get the palette index of that color short palIndexSubHeader = myColorSubHeader.getIndex(); // code to get the style for the cell goes here mainSubHeaderStyle.setFillForegroundColor(palIndexSubHeader); mainSubHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); mainSubHeaderStyle.setAlignment(CellStyle.ALIGN_CENTER); int rowIndex = 0; int columnIndex = 0; // Create Header Information // Row 1: SUMMARY OF TRIAL, FIELD AND PLANTING DETAILS CellStyle headerLabelStyle = workbook.createCellStyle(); font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerLabelStyle.setFont(font); headerLabelStyle.setAlignment(CellStyle.ALIGN_CENTER); Row row = summarySheet.createRow(rowIndex++); Cell summaryCell = row.createCell(columnIndex); summaryCell.setCellValue(summaryOfFieldbookFieldPlantingDetailsLabel); summaryCell.setCellStyle(headerLabelStyle); summarySheet.addMergedRegion(new CellRangeAddress(rowIndex - 1, //first row (0-based) rowIndex - 1, //last row (0-based) columnIndex, //first column (0-based) columnIndex + 5 //last column (0-based) )); // Row 2: Space row = summarySheet.createRow(rowIndex++); // Row 3: Fieldbook Name, Entries, Reps, Plots row = summarySheet.createRow(rowIndex++); // Selected Trial : [Fieldbook Name] TABLE SECTION Cell labelCell = row.createCell(columnIndex++); labelCell.setCellValue(selectedFieldbookLabel); row = summarySheet.createRow(rowIndex++); columnIndex = 0; Cell headerCell = row.createCell(columnIndex++); headerCell.setCellValue(orderHeader); headerCell.setCellStyle(labelStyle); headerCell = row.createCell(columnIndex++); headerCell.setCellValue(studyHeader); headerCell.setCellStyle(labelStyle); if (isTrial) { headerCell = row.createCell(columnIndex++); headerCell.setCellValue(instanceHeader); headerCell.setCellStyle(labelStyle); headerCell = row.createCell(columnIndex++); headerCell.setCellValue(entriesCountHeader); headerCell.setCellStyle(labelStyle); headerCell = row.createCell(columnIndex++); headerCell.setCellValue(repsCountHeader); headerCell.setCellStyle(labelStyle); } else { headerCell = row.createCell(columnIndex++); headerCell.setCellValue(datasetNameHeader); headerCell.setCellStyle(labelStyle); } headerCell = row.createCell(columnIndex++); headerCell.setCellValue(plotsNeededHeader); headerCell.setCellStyle(labelStyle); for (SelectedFieldmapRow rec : userFieldMap.getSelectedFieldmapList().getRows()) { row = summarySheet.createRow(rowIndex++); columnIndex = 0; row.createCell(columnIndex++).setCellValue(rec.getOrder()); row.createCell(columnIndex++).setCellValue(rec.getStudyName()); if (isTrial) { row.createCell(columnIndex++).setCellValue(rec.getTrialInstanceNo()); row.createCell(columnIndex++).setCellValue(String.valueOf(rec.getEntryCount())); row.createCell(columnIndex++).setCellValue(String.valueOf(rec.getRepCount())); row.createCell(columnIndex++).setCellValue(String.valueOf(rec.getPlotCount())); } else { row.createCell(columnIndex++).setCellValue(rec.getDatasetName()); row.createCell(columnIndex++).setCellValue(String.valueOf(rec.getEntryCount())); } } row = summarySheet.createRow(rowIndex++); columnIndex = 0; headerCell = row.createCell(columnIndex++); headerCell.setCellValue(totalPlotsHeader); headerCell.setCellStyle(labelStyle); row.createCell(columnIndex++) .setCellValue(String.valueOf(userFieldMap.getSelectedFieldmapList().getTotalNumberOfPlots())); // Row 4: Space row = summarySheet.createRow(rowIndex++); // Row 5: Header - Details Heading row = summarySheet.createRow(rowIndex++); columnIndex = 0; labelCell = row.createCell(columnIndex++); labelCell.setCellValue(fieldAndBlockDetailsLabel); labelCell.setCellStyle(headerLabelStyle); summarySheet.addMergedRegion(new CellRangeAddress(rowIndex - 1, //first row (0-based) rowIndex - 1, //last row (0-based) columnIndex - 1, //first column (0-based) columnIndex //last column (0-based) )); row.createCell(columnIndex++); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(rowRangePlotDetailsLabel); labelCell.setCellStyle(headerLabelStyle); summarySheet.addMergedRegion(new CellRangeAddress(rowIndex - 1, //first row (0-based) rowIndex - 1, //last row (0-based) columnIndex - 1, //first column (0-based) columnIndex //last column (0-based) )); row.createCell(columnIndex++); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(plantingDetailsLabel); labelCell.setCellStyle(headerLabelStyle); summarySheet.addMergedRegion(new CellRangeAddress(rowIndex - 1, //first row (0-based) rowIndex - 1, //last row (0-based) columnIndex - 1, //first column (0-based) columnIndex //last column (0-based) )); row.createCell(columnIndex++); //Row 6: Field Location, Block Capacity, Starting Coordinates row = summarySheet.createRow(rowIndex++); columnIndex = 0; labelCell = row.createCell(columnIndex++); labelCell.setCellValue(fieldLocationLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(fieldLocationValue); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(blockCapacityLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(blockCapacityValue); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(startingCoordinatesLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(startingCoordinatesValue); // Row 7: Field Name, Rows Per Plot, Planting Order row = summarySheet.createRow(rowIndex++); columnIndex = 0; labelCell = row.createCell(columnIndex++); labelCell.setCellValue(fieldNameLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(fieldNameValue); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(rowsPerPlotLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(String.valueOf(rowsPerPlotValue)); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(plantingOrderLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(plantingOrderValue); // Row 8: Block Name, Columns row = summarySheet.createRow(rowIndex++); columnIndex = 0; labelCell = row.createCell(columnIndex++); labelCell.setCellValue(blockNameLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(blockNameValue); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(columnsLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(String.valueOf(columnsValue)); labelCell = row.createCell(columnIndex++); labelCell.setCellValue(machineCapacityLabel); labelCell.setCellStyle(labelStyle); row.createCell(columnIndex++).setCellValue(String.valueOf(machineCapacityValue)); // Row 9: Space row = summarySheet.createRow(rowIndex++); for (int columnsResize = 0; columnsResize < columnIndex; columnsResize++) { summarySheet.autoSizeColumn(columnsResize); } // Get FieldMap data //we reset the row index rowIndex = 0; // Row 10: FIELD MAP row = fieldMapSheet.createRow(rowIndex++); columnIndex = 0; labelCell = row.createCell(columnIndex++); labelCell.setCellValue(fieldMapLabel); labelCell.setCellStyle(labelStyle); // Row 11: Space row = fieldMapSheet.createRow(rowIndex++); Plot[][] plots = userFieldMap.getFieldmap(); int range = userFieldMap.getNumberOfRangesInBlock(); int col = userFieldMap.getNumberOfColumnsInBlock(); int rowsPerPlot = userFieldMap.getNumberOfRowsPerPlot(); int machineRowCapacity = userFieldMap.getMachineRowCapacity(); int rows = userFieldMap.getNumberOfRowsInBlock(); boolean isSerpentine = userFieldMap.getPlantingOrder() == 2; for (int j = range - 1; j >= 0; j--) { if (j == range - 1) { // TOP TABLE LABELS // Row 12: Rows Header rowIndex = printRowHeader(fieldMapSheet, userFieldMap.getNumberOfRowsInBlock(), rowIndex, rowsLabel, mainHeaderStyle, mainSubHeaderStyle); // Row 13: UP, DOWN Direction rowIndex = printDirectionHeader(fieldMapSheet, plots, j, rows, rowIndex, machineRowCapacity, mainHeaderStyle, mainSubHeaderStyle, isSerpentine); // Row 14: Column labels rowIndex = printColumnHeader(fieldMapSheet, col, rowIndex, columnLabel, rowsPerPlot, mainHeaderStyle, mainSubHeaderStyle); } // Rows 15 onwards: Ranges and Row Data row = fieldMapSheet.createRow(rowIndex); row.setHeightInPoints(45); columnIndex = 0; int rangeValue = j + 1; Cell cellRange = row.createCell(columnIndex++); cellRange.setCellValue(rangeLabel + " " + rangeValue); cellRange.setCellStyle(mainSubHeaderStyle); for (int i = 0; i < col; i++) { String displayString = plots[i][j].getDisplayString().replace("<br/>", "\n"); if (plots[i][j].isPlotDeleted()) { displayString = " X "; } Cell dataCell = row.createCell(columnIndex++); //dataCell.setCellValue(displayString); dataCell.setCellValue(new HSSFRichTextString(displayString)); dataCell.setCellStyle(wrapStyle); //row.createCell(columnIndex).setCellValue(""); for (int k = 0; k < rowsPerPlot - 1; k++) { row.createCell(columnIndex++).setCellValue(""); } fieldMapSheet.addMergedRegion(new CellRangeAddress(rowIndex, //first row (0-based) rowIndex, //last row (0-based) columnIndex - rowsPerPlot, //first column (0-based) columnIndex - 1 //last column (0-based) )); //columnIndex++; } rowIndex++; if (j == 0) { // BOTTOM TABLE LABELS rowIndex = printColumnHeader(fieldMapSheet, col, rowIndex, columnLabel, rowsPerPlot, mainHeaderStyle, mainSubHeaderStyle); rowIndex = printDirectionHeader(fieldMapSheet, plots, j, rows, rowIndex, machineRowCapacity, mainHeaderStyle, mainSubHeaderStyle, isSerpentine); rowIndex = printRowHeader(fieldMapSheet, userFieldMap.getNumberOfRowsInBlock(), rowIndex, rowsLabel, mainHeaderStyle, mainSubHeaderStyle); } } //Write the excel file FileOutputStream fileOutputStream = new FileOutputStream(fileName); workbook.write(fileOutputStream); fileOutputStream.close(); return fileOutputStream; } catch (FileNotFoundException e) { LOG.error(e.getMessage(), e); throw new FieldbookException("Error writing to file: " + fileName, e); } catch (IOException e) { LOG.error(e.getMessage(), e); throw new FieldbookException("Error writing to file: " + fileName, e); } }
From source file:com.efficio.fieldbook.service.LabelPrintingServiceImpl.java
License:Open Source License
@Override public String generateXlSLabels(List<StudyTrialInstanceInfo> trialInstances, UserLabelPrinting userLabelPrinting, ByteArrayOutputStream baos) throws MiddlewareQueryException { int pageSizeId = Integer.parseInt(userLabelPrinting.getSizeOfLabelSheet()); int numberOfLabelPerRow = Integer.parseInt(userLabelPrinting.getNumberOfLabelPerRow()); int numberofRowsPerPageOfLabel = Integer.parseInt(userLabelPrinting.getNumberOfRowsPerPageOfLabel()); int totalPerPage = numberOfLabelPerRow * numberofRowsPerPageOfLabel; String leftSelectedFields = userLabelPrinting.getLeftSelectedLabelFields(); String rightSelectedFields = userLabelPrinting.getRightSelectedLabelFields(); String barcodeNeeded = userLabelPrinting.getBarcodeNeeded(); String firstBarcodeField = userLabelPrinting.getFirstBarcodeField(); String secondBarcodeField = userLabelPrinting.getSecondBarcodeField(); String thirdBarcodeField = userLabelPrinting.getThirdBarcodeField(); String currentDate = DateUtil.getCurrentDate(); //String fileName = currentDate + ".xls"; String fileName = userLabelPrinting.getFilenameDLLocation(); try {/*from w w w . ja v a2 s.c o m*/ HSSFWorkbook workbook = new HSSFWorkbook(); String sheetName = cleanSheetName(userLabelPrinting.getName()); if (sheetName == null) sheetName = "Labels"; Sheet labelPrintingSheet = workbook.createSheet(sheetName); CellStyle labelStyle = workbook.createCellStyle(); HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); labelStyle.setFont(font); CellStyle wrapStyle = workbook.createCellStyle(); wrapStyle.setWrapText(true); wrapStyle.setAlignment(CellStyle.ALIGN_CENTER); CellStyle mainHeaderStyle = workbook.createCellStyle(); HSSFPalette palette = workbook.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(179, 165, 165); // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here mainHeaderStyle.setFillForegroundColor(palIndex); mainHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); CellStyle mainSubHeaderStyle = workbook.createCellStyle(); HSSFPalette paletteSubHeader = workbook.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColorSubHeader = paletteSubHeader.findSimilarColor(190, 190, 186); // get the palette index of that color short palIndexSubHeader = myColorSubHeader.getIndex(); // code to get the style for the cell goes here mainSubHeaderStyle.setFillForegroundColor(palIndexSubHeader); mainSubHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); mainSubHeaderStyle.setAlignment(CellStyle.ALIGN_CENTER); int rowIndex = 0; int columnIndex = 0; // Create Header Information // Row 1: SUMMARY OF TRIAL, FIELD AND PLANTING DETAILS Row row = labelPrintingSheet.createRow(rowIndex++); //we add all the selected fields header StringTokenizer token = new StringTokenizer(leftSelectedFields, ","); while (token.hasMoreTokens()) { String headerId = token.nextToken(); String headerName = getHeader(headerId); Cell summaryCell = row.createCell(columnIndex++); summaryCell.setCellValue(headerName); summaryCell.setCellStyle(labelStyle); } token = new StringTokenizer(rightSelectedFields, ","); while (token.hasMoreTokens()) { String headerId = token.nextToken(); String headerName = getHeader(headerId); Cell summaryCell = row.createCell(columnIndex++); summaryCell.setCellValue(headerName); summaryCell.setCellStyle(labelStyle); } //we populate the info now int i = 0; for (StudyTrialInstanceInfo trialInstance : trialInstances) { FieldMapTrialInstanceInfo fieldMapTrialInstanceInfo = trialInstance.getTrialInstance(); Map<String, String> moreFieldInfo = new HashMap<String, String>(); moreFieldInfo.put("locationName", fieldMapTrialInstanceInfo.getLocationName()); moreFieldInfo.put("blockName", fieldMapTrialInstanceInfo.getBlockName()); moreFieldInfo.put("selectedName", trialInstance.getFieldbookName()); moreFieldInfo.put("trialInstanceNumber", fieldMapTrialInstanceInfo.getTrialInstanceNo()); for (FieldMapLabel fieldMapLabel : fieldMapTrialInstanceInfo.getFieldMapLabels()) { row = labelPrintingSheet.createRow(rowIndex++); columnIndex = 0; i++; token = new StringTokenizer(leftSelectedFields, ","); while (token.hasMoreTokens()) { String headerId = token.nextToken(); String leftText = getSpecificInfo(moreFieldInfo, fieldMapLabel, headerId); Cell summaryCell = row.createCell(columnIndex++); summaryCell.setCellValue(leftText); //summaryCell.setCellStyle(labelStyle); } token = new StringTokenizer(rightSelectedFields, ","); while (token.hasMoreTokens()) { String headerId = token.nextToken(); String rightText = getSpecificInfo(moreFieldInfo, fieldMapLabel, headerId); Cell summaryCell = row.createCell(columnIndex++); summaryCell.setCellValue(rightText); //summaryCell.setCellStyle(labelStyle); } } } for (int columnPosition = 0; columnPosition < columnIndex; columnPosition++) { labelPrintingSheet.autoSizeColumn((short) (columnPosition)); } //Write the excel file FileOutputStream fileOutputStream = new FileOutputStream(fileName); //workbook.write(baos); workbook.write(fileOutputStream); fileOutputStream.close(); //return fileOutputStream; } catch (Exception e) { LOG.error(e.getMessage(), e); } return fileName; }
From source file:com.endro.belajar.controller.InvoiceProdukController.java
private void clickedbuttonExportDialog() { dialogExport.getButtonExport().addActionListener(new ActionListener() { @Override/*from w w w . j a v a 2s . c o m*/ public void actionPerformed(ActionEvent e) { try { LocalDate tanggalAwal = dialogExport.getTanggalAwalChooser().getDate().toInstant() .atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate tanggalAkhir = dialogExport.getTanggalAkhirChooser().getDate().toInstant() .atZone(ZoneId.systemDefault()).toLocalDate(); List<InvoiceOrder> daftar = invoiceDao.findAllByTanggal(tanggalAwal, tanggalAkhir); processConvertExcel(daftar); } catch (SQLException | IOException ex) { Logger.getLogger(InvoiceProdukController.class.getName()).log(Level.SEVERE, null, ex); } catch (NullPointerException ex) { JOptionPane.showMessageDialog(dialogExport, "Form tanggal diisi dengan lengkap!"); } finally { dialogExport.dispose(); dialogExport = null; } } private void processConvertExcel(List<InvoiceOrder> daftarInvoice) throws FileNotFoundException, IOException { Integer returnVal = dialogExport.getChooserSaveFile().showOpenDialog(dialogExport); if (returnVal == dialogExport.getChooserSaveFile().APPROVE_OPTION) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Just Example"); List<InvoiceOrder> list = daftarInvoice; Integer rowTable = 0; Integer cellTable = 0; CellStyle cellStyleTanggal = workbook.createCellStyle(); CellStyle cellStyleHeader = workbook.createCellStyle(); CellStyle cellStyleDouble = workbook.createCellStyle(); CreationHelper createHelper = workbook.getCreationHelper(); XSSFFont font = workbook.createFont(); cellStyleTanggal.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy")); cellStyleDouble.setDataFormat( createHelper.createDataFormat().getFormat("[$Rp-421]#,##0.0000;-[$Rp-421]#,##0.0000")); font.setBold(true); cellStyleHeader.setFont(font); cellStyleHeader.setWrapText(true); //cellStyleHeader.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); cellStyleHeader.setFillPattern(FillPatternType.DIAMONDS); for (InvoiceOrder order : list) { Row row = sheet.createRow(rowTable); if (rowTable == 0) { sheet.setColumnWidth(0, 2000); Cell cellHeader = row.createCell(0); cellHeader.setCellValue("ID"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(1, 5000); cellHeader = row.createCell(1); cellHeader.setCellValue("Nomor Transaksi"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(2, 4000); cellHeader = row.createCell(2); cellHeader.setCellValue("Tanggal"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(3, 6000 * 3); cellHeader = row.createCell(3); cellHeader.setCellValue("Informasi Posting"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(4, 4850); cellHeader = row.createCell(4); cellHeader.setCellValue("Total Sebelum Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(5, 5000); cellHeader = row.createCell(5); cellHeader.setCellValue("Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(6, 4500); cellHeader = row.createCell(6); cellHeader.setCellValue("Total Setelah Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(7, 5000 * 2); cellHeader = row.createCell(7); cellHeader.setCellValue("Alamat Pengiriman"); cellHeader.setCellStyle(cellStyleHeader); } else { row.createCell(0).setCellValue((Integer) order.getPk()); row.createCell(1).setCellValue((String) order.getNomortransaksi()); Cell cellTanggal = row.createCell(2); cellTanggal.setCellValue((Date) order.getTanggal()); cellTanggal.setCellStyle(cellStyleTanggal); row.createCell(3).setCellValue((String) order.getInformasiposting()); Cell cellDouble = row.createCell(4); cellDouble.setCellValue(order.getTotalbelumdiskon()); cellDouble.setCellStyle(cellStyleDouble); cellDouble = row.createCell(5); cellDouble.setCellValue(order.getDiskonfaktur()); cellDouble.setCellStyle(cellStyleDouble); cellDouble = row.createCell(6); cellDouble.setCellValue(order.getTotalsetelahdiskon()); cellDouble.setCellStyle(cellStyleDouble); row.createCell(7).setCellValue((String) order.getAlamatpengiriman() == null ? "Null" : order.getAlamatpengiriman()); } rowTable++; } File file = dialogExport.getChooserSaveFile().getSelectedFile(); FileOutputStream outputStream = new FileOutputStream(file + File.separator + "Penjualan.xlsx"); workbook.write(outputStream); int pesan = JOptionPane.showConfirmDialog(dialogExport, "Telah tersimpan di " + file + File.separator + "Penjualan.xlsx \n Apakah anda ingin membuka file tersebut?", "Notification", JOptionPane.OK_CANCEL_OPTION); if (pesan == JOptionPane.YES_OPTION) { if ("Linux".equals(System.getProperty("os.name"))) { String runPenjualan = "xdg-open " + file + File.separator + "Penjualan.xlsx"; Runtime.getRuntime().exec(runPenjualan); } else if ("Windows".equals(System.getProperty("os.name"))) { String runPenjualan = "excel.exe /r" + file + File.separator + "Penjualan.xlsx"; Runtime.getRuntime().exec(runPenjualan); } } } else { dialogExport.getChooserSaveFile().cancelSelection(); } } }); }
From source file:com.endro.belajar.controller.MainController.java
private void clickedExport() { exportPenjualan.getButtonExport().addActionListener(new ActionListener() { @Override/*from w w w. ja va 2s. com*/ public void actionPerformed(ActionEvent e) { try { LocalDate tanggalAwal = exportPenjualan.getTanggalAwalChooser().getDate().toInstant() .atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate tanggalAkhir = exportPenjualan.getTanggalAkhirChooser().getDate().toInstant() .atZone(ZoneId.systemDefault()).toLocalDate(); List<InvoiceOrder> daftar = invoiceDao.findAllByTanggal(tanggalAwal, tanggalAkhir); processConvertExcel(daftar); } catch (SQLException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); } catch (NullPointerException ex) { JOptionPane.showMessageDialog(exportPenjualan, "Form tanggal diisi dengan lengkap!"); } catch (IOException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); } finally { exportPenjualan.dispose(); exportPenjualan = null; } } private void processConvertExcel(List<InvoiceOrder> daftarInvoice) throws FileNotFoundException, IOException { Integer returnVal = exportPenjualan.getChooserSaveFile().showOpenDialog(exportPenjualan); if (returnVal == exportPenjualan.getChooserSaveFile().APPROVE_OPTION) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Just Example"); List<InvoiceOrder> list = daftarInvoice; Integer rowTable = 0; Integer cellTable = 0; CellStyle cellStyleTanggal = workbook.createCellStyle(); CellStyle cellStyleHeader = workbook.createCellStyle(); CellStyle cellStyleDouble = workbook.createCellStyle(); CreationHelper createHelper = workbook.getCreationHelper(); XSSFFont font = workbook.createFont(); cellStyleTanggal.setDataFormat(createHelper.createDataFormat().getFormat("dd/mm/yyyy")); cellStyleDouble.setDataFormat( createHelper.createDataFormat().getFormat("[$Rp-421]#,##0.0000;-[$Rp-421]#,##0.0000")); font.setBold(true); cellStyleHeader.setFont(font); cellStyleHeader.setWrapText(true); //cellStyleHeader.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); cellStyleHeader.setFillPattern(FillPatternType.DIAMONDS); for (InvoiceOrder order : list) { Row row = sheet.createRow(rowTable); if (rowTable == 0) { sheet.setColumnWidth(0, 2000); Cell cellHeader = row.createCell(0); cellHeader.setCellValue("ID"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(1, 5000); cellHeader = row.createCell(1); cellHeader.setCellValue("Nomor Transaksi"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(2, 4000); cellHeader = row.createCell(2); cellHeader.setCellValue("Tanggal"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(3, 6000 * 3); cellHeader = row.createCell(3); cellHeader.setCellValue("Informasi Posting"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(4, 4850); cellHeader = row.createCell(4); cellHeader.setCellValue("Total Sebelum Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(5, 5000); cellHeader = row.createCell(5); cellHeader.setCellValue("Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(6, 4500); cellHeader = row.createCell(6); cellHeader.setCellValue("Total Setelah Diskon"); cellHeader.setCellStyle(cellStyleHeader); sheet.setColumnWidth(7, 5000 * 2); cellHeader = row.createCell(7); cellHeader.setCellValue("Alamat Pengiriman"); cellHeader.setCellStyle(cellStyleHeader); } else { row.createCell(0).setCellValue((Integer) order.getPk()); row.createCell(1).setCellValue((String) order.getNomortransaksi()); Cell cellTanggal = row.createCell(2); cellTanggal.setCellValue((Date) order.getTanggal()); cellTanggal.setCellStyle(cellStyleTanggal); row.createCell(3).setCellValue((String) order.getInformasiposting()); Cell cellDouble = row.createCell(4); cellDouble.setCellValue(order.getTotalbelumdiskon()); cellDouble.setCellStyle(cellStyleDouble); cellDouble = row.createCell(5); cellDouble.setCellValue(order.getDiskonfaktur()); cellDouble.setCellStyle(cellStyleDouble); cellDouble = row.createCell(6); cellDouble.setCellValue(order.getTotalsetelahdiskon()); cellDouble.setCellStyle(cellStyleDouble); row.createCell(7).setCellValue((String) order.getAlamatpengiriman() == null ? "Null" : order.getAlamatpengiriman()); } rowTable++; } File file = exportPenjualan.getChooserSaveFile().getSelectedFile(); FileOutputStream outputStream = new FileOutputStream(file + File.separator + "Penjualan.xlsx"); workbook.write(outputStream); int pesan = JOptionPane.showConfirmDialog(exportPenjualan, "Telah tersimpan di " + file + File.separator + "Penjualan.xlsx \n Apakah anda ingin membuka file tersebut?", "Notification", JOptionPane.OK_CANCEL_OPTION); if (pesan == JOptionPane.YES_OPTION) { if ("Linux".equals(System.getProperty("os.name"))) { String runPenjualan = "xdg-open " + file + File.separator + "Penjualan.xlsx"; Runtime.getRuntime().exec(runPenjualan); } else if ("Windows".equals(System.getProperty("os.name"))) { String runPenjualan = "excel.exe /r" + file + File.separator + "Penjualan.xlsx"; Runtime.getRuntime().exec(runPenjualan); } } } else { exportPenjualan.getChooserSaveFile().cancelSelection(); } } }); }