List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet setColumnWidth
@Override public void setColumnWidth(int columnIndex, int width)
The maximum column width for an individual cell is 255 characters.
From source file:org.sigmah.server.endpoint.export.Export.java
License:Open Source License
private void createHeaders(ActivityDTO activity, HSSFSheet sheet) { /// The HEADER rows Row headerRow1 = sheet.createRow(0); Row headerRow2 = sheet.createRow(1); headerRow2.setHeightInPoints(75);//w ww. j a v a 2 s . co m int column = 0; createHeaderCell(headerRow2, column++, "Date1", CellStyle.ALIGN_RIGHT); createHeaderCell(headerRow2, column++, "Date2", CellStyle.ALIGN_RIGHT); createHeaderCell(headerRow2, column, "Partner"); sheet.setColumnWidth(column, 16 * 256); column++; createHeaderCell(headerRow2, column, activity.getLocationType().getName()); sheet.setColumnWidth(column, 20 * 256); column++; createHeaderCell(headerRow2, column++, "Axe"); indicators = new ArrayList<Integer>(activity.getIndicators().size()); if (activity.getReportingFrequency() == ActivityDTO.REPORT_ONCE) { for (IndicatorGroup group : activity.groupIndicators()) { if (group.getName() != null) { // create a merged cell on the top row spanning all members of the group createHeaderCell(headerRow1, column, group.getName()); sheet.addMergedRegion( new CellRangeAddress(0, 0, column, column + group.getIndicators().size() - 1)); } for (IndicatorDTO indicator : group.getIndicators()) { indicators.add(indicator.getId()); createHeaderCell(headerRow2, column, indicator.getName(), indicatorHeaderStyle); sheet.setColumnWidth(column, 16 * 256); column++; } } } attributes = new ArrayList<Integer>(); int firstAttributeColumn = column; for (AttributeGroupDTO group : activity.getAttributeGroups()) { if (group.getAttributes().size() != 0) { createHeaderCell(headerRow1, column, group.getName(), CellStyle.ALIGN_CENTER); sheet.addMergedRegion( new CellRangeAddress(0, 0, column, column + group.getAttributes().size() - 1)); for (AttributeDTO attrib : group.getAttributes()) { attributes.add(attrib.getId()); createHeaderCell(headerRow2, column, attrib.getName(), attribHeaderStyle); sheet.setColumnWidth(column, 5 * 256); column++; } } } // sheet.getSheetConditionalFormatting().addConditionalFormatting( // new CellRangeAddress[] { new CellRangeAddress(2, 65535, firstAttributeColumn, column-1) }, // new HSSFConditionalFormattingRule[] { attribTrueRule, attribFalseRule }); levels = new ArrayList<Integer>(); for (AdminLevelDTO level : activity.getAdminLevels()) { createHeaderCell(headerRow2, column++, "Code " + level.getName()); createHeaderCell(headerRow2, column++, level.getName()); levels.add(level.getId()); } createHeaderCell(headerRow2, column, "Longitude", CellStyle.ALIGN_RIGHT); createHeaderCell(headerRow2, column + 1, "Latitude", CellStyle.ALIGN_RIGHT); sheet.setColumnWidth(column, 12 * 256); sheet.setColumnWidth(column + 1, 12 * 256); }
From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.template.BaseSynthesisExcelTemplate.java
License:Open Source License
public BaseSynthesisExcelTemplate(final BaseSynthesisData data, final HSSFWorkbook wb, final Class clazz) throws Throwable { this.wb = wb; this.data = data; this.clazz = clazz; String title = data.getLocalizedVersion("projectSynthesis"); if (clazz.equals(OrgUnit.class)) title = data.getLocalizedVersion("orgUnitSynthesis"); final HSSFSheet sheet = wb.createSheet(title); utils = new ExcelUtils(wb); int rowIndex = -1; // empty row//from w w w .j ava 2 s . com utils.putEmptyRow(sheet, ++rowIndex, 8.65f); // title utils.putMainTitle(sheet, ++rowIndex, title.toUpperCase(), data.getNumbOfCols()); // empty row utils.putEmptyRow(sheet, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); // column headers row = sheet.createRow(++rowIndex); utils.putHeader(row, 2, data.getLocalizedVersion("adminFlexibleName")); utils.putHeader(row, 3, data.getLocalizedVersion("value")); // empty row utils.putEmptyRow(sheet, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); // freeze pane sheet.createFreezePane(0, rowIndex); // detail row = sheet.createRow(++rowIndex); utils.putHeader(row, 1, data.getLocalizedVersion("projectDetails")); // Project synthesis if (clazz.equals(Project.class)) { rowIndex = putLayout(sheet, data.getProject().getProjectModel().getProjectDetails().getLayout(), rowIndex); // empty row utils.putEmptyRow(sheet, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); // run through project phases to get synthesis data for (final PhaseModel phaseModel : data.getProject().getProjectModel().getPhases()) { // phase name row = sheet.createRow(++rowIndex); utils.putHeader(row, 1, phaseModel.getName()); rowIndex = putLayout(sheet, phaseModel.getLayout(), rowIndex); } } else { // Org Unit synthesis rowIndex = putLayout(sheet, data.getOrgUnit().getOrgUnitModel().getDetails().getLayout(), rowIndex); } sheet.setColumnWidth(0, 256 * 2); sheet.setColumnWidth(1, 256 * 25); sheet.setColumnWidth(2, 256 * labelColWidth); sheet.setColumnWidth(3, 256 * valueColWidth); }
From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.template.GlobalExportExcelTemplate.java
License:Open Source License
public GlobalExportExcelTemplate(final GlobalExportData data) { this.wb = new HSSFWorkbook(); final ExcelUtils utils = new ExcelUtils(wb); final float defHeight = ExportConstants.TITLE_ROW_HEIGHT; int rowIndex = -1; HSSFSheet sheet; HSSFRow row;//www . j a v a 2s .c o m int defaultWidth = 20; final Map<Integer, Integer> headerWidthMap = new HashMap<Integer, Integer>(); final Map<Integer, Integer> contentWidthMap = new HashMap<Integer, Integer>(); for (final String pModelName : data.getExportData().keySet()) { List<String[]> dataList = data.getExportData().get(pModelName); sheet = wb.createSheet(pModelName); rowIndex = -1; headerWidthMap.clear(); contentWidthMap.clear(); //titles final String[] header = dataList.get(0); row = sheet.createRow(++rowIndex); for (int i = 0; i < header.length; i++) { utils.putGlobalExportHeader(row, i, header[i]); if (header[i] != null) { headerWidthMap.put(i, header[i].length() / 2); } } row.setHeightInPoints(2 * defHeight); //values for (int j = 1; j < dataList.size(); j++) { row = sheet.createRow(++rowIndex); final String[] values = dataList.get(j); int devider = 2; for (int i = 0; i < header.length; i++) { utils.putBorderedBasicCell(sheet, rowIndex, i, values[i]); if (values[i] != null) { String parts[] = values[i].split("\n"); if (parts.length > devider) { devider = parts.length; } int currentWidth = values[i].length() / devider; Integer oldWidth = contentWidthMap.get(i); if (oldWidth != null) { currentWidth = Math.max(oldWidth, currentWidth); } contentWidthMap.put(i, currentWidth); } } row.setHeightInPoints(devider * defHeight); } // set width for (Integer i : headerWidthMap.keySet()) { Integer width = defaultWidth; if (headerWidthMap.get(i) != null) { width = headerWidthMap.get(i); } if (contentWidthMap.get(i) != null) { width = Math.max(contentWidthMap.get(i), width); } sheet.setColumnWidth(i, 256 * (width + 15)); } } }
From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.template.IndicatorEntryExcelTemplate.java
License:Open Source License
public IndicatorEntryExcelTemplate(final IndicatorEntryData data, final HSSFWorkbook wb) { this.data = data; this.wb = wb; final HSSFSheet sheet = wb.createSheet(data.getLocalizedVersion("flexibleElementIndicatorsList")); utils = new ExcelUtils(wb); int rowIndex = -1; int cellIndex = 0; // empty row//from w ww . j av a2 s. c o m utils.putEmptyRow(sheet, ++rowIndex, 8.65f); //title utils.putMainTitle(sheet, ++rowIndex, data.getLocalizedVersion("flexibleElementIndicatorsList").toUpperCase(), data.getNumbOfCols()); // empty row utils.putEmptyRow(sheet, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); // column headers row = sheet.createRow(++rowIndex); row.setHeightInPoints(ExportConstants.TITLE_ROW_HEIGHT); cellIndex = 0; utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("name")); utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("code")); utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("targetValue")); utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("value")); // empty row utils.putEmptyRow(sheet, ++rowIndex, 8.65f); // freeze pane sheet.createFreezePane(0, rowIndex); for (final IndicatorGroup group : data.getIndicators().getGroups()) { row = sheet.createRow(++rowIndex); row.setHeightInPoints(ExportConstants.TITLE_ROW_HEIGHT); putGroupCell(sheet, rowIndex, group.getName()); for (final IndicatorDTO indicator : group.getIndicators()) { //indicator's detail sheet createDetailSheet(indicator); row = sheet.createRow(++rowIndex); row.setHeightInPoints(ExportConstants.TITLE_ROW_HEIGHT); //ind name utils.createLinkCell(row.createCell(1), indicator.getName(), ExportConstants.INDICATOR_SHEET_PREFIX + indicator.getName(), true); //code utils.putBorderedBasicCell(sheet, rowIndex, 2, indicator.getCode()); //target putRightAlignedCell(sheet, rowIndex, 3, indicator.getObjective()); //current value putRightAlignedCell(sheet, rowIndex, 4, data.getFormattedValue(indicator)); } } sheet.setColumnWidth(0, 256 * 2); sheet.setColumnWidth(1, 256 * 45); sheet.setColumnWidth(2, 256 * 27); sheet.setColumnWidth(3, 256 * 27); sheet.setColumnWidth(4, 256 * 27); }
From source file:org.sigmah.server.endpoint.export.sigmah.spreadsheet.template.IndicatorEntryExcelTemplate.java
License:Open Source License
private void createDetailSheet(final IndicatorDTO indicator) { final boolean isQualitative = indicator.getAggregation() == IndicatorDTO.AGGREGATE_MULTINOMIAL; final HSSFSheet sheetEx = wb .createSheet(utils.normalizeAsLink(ExportConstants.INDICATOR_SHEET_PREFIX + indicator.getName())); int rowIndex = -1; final List<PivotTableData.Axis> leaves = data.getEntryMap().get(indicator.getId()).getRootColumn() .getLeaves();// w ww . j a va2 s . c om int numbOfLeaves = leaves.size(); int numbOfCols = 4; //back to list link row = sheetEx.createRow(++rowIndex); utils.createLinkCell(row.createCell(1), data.getLocalizedVersion("goToIndicatorsList"), data.getLocalizedVersion("flexibleElementIndicatorsList"), false); sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, numbOfCols)); //title utils.putMainTitle(sheetEx, ++rowIndex, indicator.getName(), numbOfCols); //empty row utils.putEmptyRow(sheetEx, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, numbOfCols)); //put details putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("code"), indicator.getCode(), numbOfCols); putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("group"), data.getGroupMap().get(indicator.getGroupId()), numbOfCols); //type String type = null; ; if (isQualitative) { //qualitative type = data.getLocalizedVersion("qualitative"); } else { //quantitative type = data.getLocalizedVersion("quantitative"); } putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("type"), type, numbOfCols); //conditional if (isQualitative) { //qualitative //possible values row = sheetEx.createRow(++rowIndex); //key cell = utils.putHeader(row, 1, data.getLocalizedVersion("possibleValues")); cell.getCellStyle().setAlignment(CellStyle.ALIGN_RIGHT); //value final MultiItemText itemText = data.formatPossibleValues(indicator.getLabels()); utils.putBorderedBasicCell(sheetEx, rowIndex, 2, itemText.text); row.setHeightInPoints(itemText.lineCount * defHeight); region = new CellRangeAddress(rowIndex, rowIndex, 2, numbOfCols); sheetEx.addMergedRegion(utils.getBorderedRegion(region, sheetEx, wb)); } else { //quantitative //aggregation method String aggrMethod = null; if (indicator.getAggregation() == IndicatorDTO.AGGREGATE_AVG) aggrMethod = data.getLocalizedVersion("average"); else aggrMethod = data.getLocalizedVersion("sum"); putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("aggregationMethod"), aggrMethod, numbOfCols); //units putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("units"), indicator.getUnits(), numbOfCols); //target value putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("targetValue"), indicator.getObjective(), numbOfCols); } //source of ver putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("sourceOfVerification"), indicator.getSourceOfVerification(), numbOfCols); row.setHeightInPoints(utils.calculateLineCount(indicator.getSourceOfVerification(), 3 * 18) * defHeight); //comment putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("indicatorComments"), indicator.getDescription(), numbOfCols); row.setHeightInPoints(utils.calculateLineCount(indicator.getDescription(), 3 * 18) * defHeight); //value putBasicInfo(sheetEx, ++rowIndex, data.getLocalizedVersion("value"), data.getFormattedValue(indicator), numbOfCols); //empty row utils.putEmptyRow(sheetEx, ++rowIndex, ExportConstants.EMPTY_ROW_HEIGHT); sheetEx.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, numbOfCols)); //data entry //header row = sheetEx.createRow(++rowIndex); row.setHeightInPoints(defHeight); int cellIndex = 0; utils.putHeader(row, ++cellIndex, data.getLocalizedVersion("sideAndMonth")); Map<String, Integer> columnIndexMap = new HashMap<String, Integer>(); for (PivotTableData.Axis axis : leaves) { utils.putHeader(row, ++cellIndex, axis.getLabel()); columnIndexMap.put(axis.getLabel(), cellIndex); } //rows for (PivotTableData.Axis axis : data.getEntryMap().get(indicator.getId()).getRootRow().getChildren()) { row = sheetEx.createRow(++rowIndex); row.setHeightInPoints(defHeight); utils.putHeader(row, 1, axis.getLabel()); //populate empty cells for (int i = 0; i < numbOfLeaves; i++) { cell = utils.putBorderedBasicCell(sheetEx, rowIndex, i + 2, ""); } //insert values for (Map.Entry<PivotTableData.Axis, PivotTableData.Cell> entry : axis.getCells().entrySet()) { cellIndex = columnIndexMap.get(entry.getKey().getLabel()); Object value = null; boolean rightAligned = false; if (isQualitative) { value = data.getLabelByIndex(indicator.getLabels(), entry.getValue().getValue()); } else { value = new Long(Math.round(entry.getValue().getValue())); rightAligned = true; } putValueCell(sheetEx, rowIndex, cellIndex, value, rightAligned); } } //col width sheetEx.setColumnWidth(0, 256 * 2); sheetEx.autoSizeColumn(1); for (int i = 2; i < 2 + numbOfLeaves; i++) { sheetEx.setColumnWidth(i, 256 * 16); } }
From source file:org.sourcecodemetrics.report.generators.ReportGeneratorImpl.java
License:Open Source License
@Override public void generateReport(IProject project, String filePath) { try {// ww w .j a v a2 s . c o m FileOutputStream fileOut = new FileOutputStream(filePath); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet worksheet = workbook.createSheet("Source Code Metrics"); // creation of the header row whiteStyle = workbook.createCellStyle(); whiteStyle.setFillForegroundColor(HSSFColor.WHITE.index); whiteStyle.setFillBackgroundColor(HSSFColor.WHITE.index); whiteStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); dottedBlueStyle = workbook.createCellStyle(); dottedBlueStyle.setFillForegroundColor(HSSFColor.BLACK.index); dottedBlueStyle.setFillBackgroundColor(HSSFColor.AQUA.index); dottedBlueStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS); blueStyle = workbook.createCellStyle(); blueStyle.setFillForegroundColor(HSSFColor.AQUA.index); blueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.RED.index); redBlueStyle = workbook.createCellStyle(); redBlueStyle.setFillForegroundColor(HSSFColor.AQUA.index); redBlueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); redBlueStyle.setFont(font); dottedYellowStyle = workbook.createCellStyle(); dottedYellowStyle.setFillForegroundColor(HSSFColor.BLACK.index); dottedYellowStyle.setFillBackgroundColor(HSSFColor.YELLOW.index); dottedYellowStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS); yellowStyle = workbook.createCellStyle(); yellowStyle.setFillForegroundColor(HSSFColor.YELLOW.index); yellowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); redYellowStyle = workbook.createCellStyle(); redYellowStyle.setFillForegroundColor(HSSFColor.YELLOW.index); redYellowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); redYellowStyle.setFont(font); dottedStyle = workbook.createCellStyle(); dottedStyle.setFillForegroundColor(HSSFColor.BLACK.index); dottedStyle.setFillBackgroundColor(HSSFColor.WHITE.index); dottedStyle.setFillPattern(HSSFCellStyle.SPARSE_DOTS); redWhiteStyle = workbook.createCellStyle(); redWhiteStyle.setFillForegroundColor(HSSFColor.WHITE.index); redWhiteStyle.setFillBackgroundColor(HSSFColor.WHITE.index); redWhiteStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); redWhiteStyle.setFont(font); HSSFRow headerRow = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); for (int i = 0; i < headings.size(); i++) { String title = headings.get(i); HSSFCell cell = headerRow.createCell(i); cell.setCellValue(title); cell.setCellStyle(whiteStyle); } for (IPackage ip : project.getPackages()) { if (!ip.isTests() && !ip.getSourceFiles().isEmpty()) { PackageGenerator.generate(ip, worksheet, workbook); } } // adjusting first three columns try { for (int i = 0; i < 3; i++) { worksheet.autoSizeColumn(i); } } catch (ArrayIndexOutOfBoundsException e) { logger.log(Level.SEVERE, "Exception when trying to adjust columns", e); } // setting constant width to other columns for (int i = 3; i < headings.size(); i++) { worksheet.setColumnWidth(i, 2000); } // writing out the statistics to the report StatisticsGenerator.generateStatistics(project, workbook); // writing the raw data to the report RawDataGenerator.generateRawData(project, workbook); worksheet.createFreezePane(3, 1); workbook.write(fileOut); fileOut.flush(); fileOut.close(); } catch (FileNotFoundException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } catch (Exception ex) { Exceptions.printStackTrace(ex); } }
From source file:org.sourcecodemetrics.report.generators.StatisticsGenerator.java
License:Open Source License
public static void generateStatistics(IProject project, HSSFWorkbook workbook) { if (blueStyle == null) { blueStyle = workbook.createCellStyle(); blueStyle.setFillForegroundColor(HSSFColor.AQUA.index); blueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); }/*from w ww .j av a 2s . co m*/ HSSFSheet worksheet = workbook.createSheet("Statistics"); // creation of the header row HSSFRow headerRow = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.WHITE.index); for (int i = 0; i < headings.size(); i++) { String title = headings.get(i); HSSFCell cell = headerRow.createCell(i); cell.setCellValue(title); cell.setCellStyle(blueStyle); } // writing out all of the values of the metrics HSSFRow row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); HSSFCell cell = row.createCell(0); cell.setCellValue("A"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Abstractness"); cell = row.createCell(3); if (project.getPackageAMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageAMin()); } cell = row.createCell(4); if (project.getPackageAMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageAMax()); } cell = row.createCell(5); if (project.getPackageAAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageAAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("AC"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Afferent Coupling"); cell = row.createCell(3); if (project.getPackageACMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageACMin()); } cell = row.createCell(4); if (project.getPackageACMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageACMax()); } cell = row.createCell(5); if (project.getPackageACAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageACAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("C"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Coverage"); cell = row.createCell(3); if (project.getPackageCoverageMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageCoverageMin()); } cell = row.createCell(4); if (project.getPackageCoverageMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageCoverageMax()); } cell = row.createCell(5); if (project.getPackageCoverageAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageCoverageAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("D"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Distance"); cell = row.createCell(3); if (project.getPackageDMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageDMin()); } cell = row.createCell(4); if (project.getPackageDMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageDMax()); } cell = row.createCell(5); if (project.getPackageDAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageDAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("EC"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Efferent Coupling"); cell = row.createCell(3); if (project.getPackageECMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageECMin()); } cell = row.createCell(4); if (project.getPackageECMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageECMax()); } cell = row.createCell(5); if (project.getPackageECAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageECAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("I"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Instability"); cell = row.createCell(3); if (project.getPackageIMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageIMin()); } cell = row.createCell(4); if (project.getPackageIMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageIMax()); } cell = row.createCell(5); if (project.getPackageIAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageIAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOC"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Lines Of Code"); cell = row.createCell(3); if (project.getPackageLocMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageLocMin()); } cell = row.createCell(4); if (project.getPackageLocMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageLocMax()); } cell = row.createCell(6); if (project.getLOC() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getLOC()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOCm"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Lines Of Comments"); cell = row.createCell(3); if (project.getPackageLocmMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageLocmMin()); } cell = row.createCell(4); if (project.getPackageLocmMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageLocmMax()); } cell = row.createCell(6); if (project.getLOCm() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getLOCm()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NCP"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Number Of Classes in Package"); cell = row.createCell(3); if (project.getPackageNCPMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNCPMin()); } cell = row.createCell(4); if (project.getPackageNCPMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNCPMax()); } cell = row.createCell(6); if (project.getPackageNCPSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNCPSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NIP"); cell = row.createCell(1); cell.setCellValue("package"); cell = row.createCell(2); cell.setCellValue("Number Of Interfaces in Package"); cell = row.createCell(3); if (project.getPackageNIPMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNIPMin()); } cell = row.createCell(4); if (project.getPackageNIPMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNIPMax()); } cell = row.createCell(6); if (project.getPackageNIPSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getPackageNIPSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCC"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Loose Class Coupling"); cell = row.createCell(3); if (project.getClassLCCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCCMax()); } cell = row.createCell(4); if (project.getClassLCCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCCMin()); } cell = row.createCell(5); if (project.getClassLCCAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCCAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCOM1"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lack Of Cohesion in Methods 1"); cell = row.createCell(3); if (project.getClassLCOM1Min() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM1Min()); } cell = row.createCell(4); if (project.getClassLCOM1Max() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM1Max()); } cell = row.createCell(5); if (project.getClassLCOM1Avg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM1Avg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCOM2"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lack Of Cohesion in Methods 2"); cell = row.createCell(3); if (project.getClassLCOM2Min() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM2Min()); } cell = row.createCell(4); if (project.getClassLCOM2Max() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM2Max()); } cell = row.createCell(5); if (project.getClassLCOM2Avg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM2Avg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCOM3"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lack Of Cohesion in Methods 3"); cell = row.createCell(3); if (project.getClassLCOM3Min() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM3Min()); } cell = row.createCell(4); if (project.getClassLCOM3Max() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM3Max()); } cell = row.createCell(5); if (project.getClassLCOM3Avg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM3Avg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCOM4"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lack Of Cohesion in Methods 4"); cell = row.createCell(3); if (project.getClassLCOM4Min() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM4Min()); } cell = row.createCell(4); if (project.getClassLCOM4Max() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM4Max()); } cell = row.createCell(5); if (project.getClassLCOM4Avg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM4Avg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LCOM5"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lack Of Cohesion in Methods 5"); cell = row.createCell(3); if (project.getClassLCOM5Min() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM5Min()); } cell = row.createCell(4); if (project.getClassLCOM5Max() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM5Max()); } cell = row.createCell(5); if (project.getClassLCOM5Avg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLCOM5Avg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOC"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lines Of Code"); cell = row.createCell(3); if (project.getClassLOCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLOCMin()); } cell = row.createCell(4); if (project.getClassLOCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLOCMax()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOCm"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Lines Of Comments"); cell = row.createCell(3); if (project.getClassLOCmMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLOCmMin()); } cell = row.createCell(4); if (project.getClassLOCmMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassLOCmMax()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NAK"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number of Assertions per KLOC"); cell = row.createCell(3); if (project.getClassNAKMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNAKMin()); } cell = row.createCell(4); if (project.getClassNAKMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNAKMax()); } cell = row.createCell(5); if (project.getClassNAKAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNAKAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOC"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number Of Children"); cell = row.createCell(3); if (project.getClassNOCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOCMin()); } cell = row.createCell(4); if (project.getClassNOCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOCMax()); } cell = row.createCell(6); if (project.getClassNOCSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOCSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOF"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number Of Fields"); cell = row.createCell(3); if (project.getClassNOFMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOFMin()); } cell = row.createCell(4); if (project.getClassNOFMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOFMax()); } cell = row.createCell(6); if (project.getClassNOFSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOFSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOM"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number Of Methods"); cell = row.createCell(3); if (project.getClassNOMMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOMMin()); } cell = row.createCell(4); if (project.getClassNOMMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOMMax()); } cell = row.createCell(6); if (project.getClassNOMSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOMSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOSF"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number Of Static Fields"); cell = row.createCell(3); if (project.getClassNOSFMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSFMin()); } cell = row.createCell(4); if (project.getClassNOSFMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSFMax()); } cell = row.createCell(6); if (project.getClassNOSFSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSFSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOSM"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number Of Static Methods"); cell = row.createCell(3); if (project.getClassNOSMMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSMMin()); } cell = row.createCell(4); if (project.getClassNOSMMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSMMax()); } cell = row.createCell(6); if (project.getClassNOSMSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNOSMSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NTM"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Number of Test Methods"); cell = row.createCell(3); if (project.getClassNTMMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNTMMin()); } cell = row.createCell(4); if (project.getClassNTMMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNTMMax()); } cell = row.createCell(6); if (project.getClassNTMSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassNTMSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("TCC"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Tight Class Coupling"); cell = row.createCell(3); if (project.getClassTCCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassTCCMin()); } cell = row.createCell(4); if (project.getClassTCCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassTCCMax()); } cell = row.createCell(5); if (project.getClassTCCAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassTCCAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("WMC"); cell = row.createCell(1); cell.setCellValue("class"); cell = row.createCell(2); cell.setCellValue("Weighted Method Count"); cell = row.createCell(3); if (project.getClassWMCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassWMCMin()); } cell = row.createCell(4); if (project.getClassWMCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassWMCMax()); } cell = row.createCell(6); if (project.getClassWMCSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getClassWMCSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOC"); cell = row.createCell(1); cell.setCellValue("method"); cell = row.createCell(2); cell.setCellValue("Lines Of Code"); cell = row.createCell(3); if (project.getMethodLOCMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodLOCMin()); } cell = row.createCell(4); if (project.getMethodLOCMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodLOCMax()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("LOCm"); cell = row.createCell(1); cell.setCellValue("method"); cell = row.createCell(2); cell.setCellValue("Lines Of Comments"); cell = row.createCell(3); if (project.getMethodLOCmMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodLOCmMin()); } cell = row.createCell(4); if (project.getMethodLOCmMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodLOCmMax()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NBD"); cell = row.createCell(1); cell.setCellValue("method"); cell = row.createCell(2); cell.setCellValue("Nested Block Depth"); cell = row.createCell(3); if (project.getMethodNBDMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNBDMin()); } cell = row.createCell(4); if (project.getMethodNBDMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNBDMax()); } cell = row.createCell(5); if (project.getMethodNBDAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNBDAvg()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("NOP"); cell = row.createCell(1); cell.setCellValue("method"); cell = row.createCell(2); cell.setCellValue("Number Of Parameters"); cell = row.createCell(3); if (project.getMethodNOPMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNOPMin()); } cell = row.createCell(4); if (project.getMethodNOPMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNOPMax()); } cell = row.createCell(6); if (project.getMethodNOPSum() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodNOPSum()); } row = worksheet.createRow(worksheet.getPhysicalNumberOfRows()); cell = row.createCell(0); cell.setCellValue("VG"); cell = row.createCell(1); cell.setCellValue("method"); cell = row.createCell(2); cell.setCellValue("McGabe's Cyclomatic Complexity"); cell = row.createCell(3); if (project.getMethodVGMin() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodVGMin()); } cell = row.createCell(4); if (project.getMethodVGMax() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodVGMax()); } cell = row.createCell(5); if (project.getMethodVGAvg() == null) { cell.setCellValue("-"); } else { cell.setCellValue(project.getMethodVGAvg()); } // adjusting first three columns try { for (int i = 0; i < 7; i++) { worksheet.autoSizeColumn(i); } } catch (ArrayIndexOutOfBoundsException e) { logger.log(Level.SEVERE, "Exception when trying to adjust columns", e); } // setting constant width to other columns for (int i = 3; i < 7; i++) { worksheet.setColumnWidth(i, 2700); } }
From source file:org.tentackle.ui.FormTableUtilityPopup.java
License:Open Source License
/** * Converts the table to an excel spreadsheet. * @param file the output file/*from w w w. jav a 2 s . c o m*/ * @param onlySelected true if export only selected rows * @throws IOException if export failed */ public void excel(File file, boolean onlySelected) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); TableModel model = table.getModel(); TableColumnModel columnModel = table.getColumnModel(); int[] selectedRows = onlySelected ? table.getSelectedRows() : new int[] {}; int rows = onlySelected ? selectedRows.length : model.getRowCount(); // number of data rows int cols = columnModel.getColumnCount(); // number of data columns short srow = 0; // current spreadsheet row // local copies cause might be changed String xTitle = this.title; String xIntro = this.intro; if (xTitle == null) { // get default from window title Window parent = FormHelper.getParentWindow(table); try { // paint page-title xTitle = ((FormWindow) parent).getTitle(); } catch (Exception e) { xTitle = null; } } if (xTitle != null) { HSSFRow row = sheet.createRow(srow); HSSFFont font = wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle cs = wb.createCellStyle(); cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); cs.setFont(font); HSSFCell cell = row.createCell(0); cell.setCellStyle(cs); cell.setCellValue(new HSSFRichTextString(xTitle)); // region rowFrom, colFrom, rowTo, colTo sheet.addMergedRegion(new CellRangeAddress(0, srow, 0, cols - 1)); srow++; } if (xIntro != null || onlySelected) { HSSFRow row = sheet.createRow(srow); HSSFCell cell = row.createCell(0); HSSFCellStyle cs = wb.createCellStyle(); cs.setAlignment(HSSFCellStyle.ALIGN_LEFT); cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cs.setWrapText(true); cell.setCellStyle(cs); if (onlySelected) { if (xIntro == null) { xIntro = ""; } else { xIntro += ", "; } xIntro += Locales.bundle.getString("<nur_selektierte_Zeilen>"); } cell.setCellValue(new HSSFRichTextString(xIntro)); sheet.addMergedRegion(new CellRangeAddress(srow, srow + 2, 0, cols - 1)); srow += 3; } // column headers boolean isAbstractFormTableModel = model instanceof AbstractFormTableModel; srow++; // always skip one line HSSFRow row = sheet.createRow(srow); HSSFFont font = wb.createFont(); font.setItalic(true); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle cs = wb.createCellStyle(); cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); cs.setFont(font); for (int c = 0; c < cols; c++) { HSSFCell cell = row.createCell(c); cell.setCellValue(new HSSFRichTextString(isAbstractFormTableModel ? ((AbstractFormTableModel) model) .getDisplayedColumnName(columnModel.getColumn(c).getModelIndex()) : model.getColumnName(columnModel.getColumn(c).getModelIndex()))); cell.setCellStyle(cs); } srow++; // default cell-style for date HSSFCellStyle dateStyle = wb.createCellStyle(); dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); // cellstyles for numbers List<HSSFCellStyle> numberStyles = new ArrayList<HSSFCellStyle>(); HSSFDataFormat format = wb.createDataFormat(); for (int r = 0; r < rows; r++) { int modelRow = onlySelected ? selectedRows[r] : r; row = sheet.createRow(srow + (short) r); for (int i = 0; i < cols; i++) { int c = columnModel.getColumn(i).getModelIndex(); Object value = model.getValueAt(modelRow, c); HSSFCell cell = row.createCell(i); if (value instanceof Boolean) { cell.setCellValue(((Boolean) value).booleanValue()); } else if (value instanceof BMoney) { BMoney money = (BMoney) value; cell.setCellValue(money.doubleValue()); String fmt = "#,##0"; if (money.scale() > 0) { fmt += "."; for (int j = 0; j < money.scale(); j++) { fmt += "0"; } } // create format short fmtIndex = format.getFormat(fmt); // check if there is already a cellstyle with this scale Iterator<HSSFCellStyle> iter = numberStyles.iterator(); boolean found = false; while (iter.hasNext()) { cs = iter.next(); if (cs.getDataFormat() == fmtIndex) { // reuse that found = true; break; } } if (!found) { // create a new style cs = wb.createCellStyle(); cs.setDataFormat(fmtIndex); numberStyles.add(cs); } cell.setCellStyle(cs); } else if (value instanceof Number) { cell.setCellValue(((Number) value).doubleValue()); } else if (value instanceof Date) { cell.setCellValue((Date) value); cell.setCellStyle(dateStyle); } else if (value instanceof GregorianCalendar) { cell.setCellValue((GregorianCalendar) value); cell.setCellStyle(dateStyle); } else if (value != null) { cell.setCellValue(new HSSFRichTextString(value.toString())); } } } // set the width for each column for (int c = 0; c < cols; c++) { short width = (short) (columnModel.getColumn(c).getWidth() * 45); // is a reasonable value sheet.setColumnWidth(c, width); } // Write the output to a file FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); // open Excel URLHelper.openURL(file.getPath()); }
From source file:org.unitime.timetable.export.XLSPrinter.java
License:Apache License
private ClientAnchorDetail fitImageToColumns(HSSFSheet sheet, int colNumber, double reqImageWidthMM, int resizeBehaviour) { double colWidthMM; double colCoordinatesPerMM; int pictureWidthCoordinates; ClientAnchorDetail colClientAnchorDetail = null; colWidthMM = ConvertImageUnits.widthUnits2Millimetres((short) sheet.getColumnWidth(colNumber)); if (colWidthMM < reqImageWidthMM) { if (resizeBehaviour == EXPAND_COLUMN || resizeBehaviour == EXPAND_ROW_AND_COLUMN) { sheet.setColumnWidth(colNumber, ConvertImageUnits.millimetres2WidthUnits(reqImageWidthMM)); colWidthMM = reqImageWidthMM; colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; pictureWidthCoordinates = (int) (reqImageWidthMM * colCoordinatesPerMM); colClientAnchorDetail = new ClientAnchorDetail(colNumber, colNumber, pictureWidthCoordinates); } else if (resizeBehaviour == OVERLAY_ROW_AND_COLUMN || resizeBehaviour == EXPAND_ROW) { colClientAnchorDetail = calculateColumnLocation(sheet, colNumber, reqImageWidthMM); }//ww w . j a v a2 s . c o m } else { colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS / colWidthMM; pictureWidthCoordinates = (int) (reqImageWidthMM * colCoordinatesPerMM); colClientAnchorDetail = new ClientAnchorDetail(colNumber, colNumber, pictureWidthCoordinates); } return (colClientAnchorDetail); }
From source file:org.waterforpeople.mapping.dataexport.SurveyFormExporter.java
License:Open Source License
/** * Writes the survey as an XLS document//from w w w.ja va2 s.c om */ private void writeSurvey(String title, File fileName, List<QuestionGroupDto> groupList, Map<QuestionGroupDto, List<QuestionDto>> questions) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); sheet.setColumnWidth(0, COL_WIDTH); sheet.setColumnWidth(1, COL_WIDTH); HSSFCellStyle headerStyle = wb.createCellStyle(); headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont headerFont = wb.createFont(); headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setFont(headerFont); HSSFCellStyle questionStyle = wb.createCellStyle(); questionStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); questionStyle.setWrapText(true); HSSFCellStyle depStyle = wb.createCellStyle(); depStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont depFont = wb.createFont(); depFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); depFont.setItalic(true); depStyle.setFont(depFont); int curRow = 0; HSSFRow row = sheet.createRow(curRow++); sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1)); createCell(row, 0, title, headerStyle); row = sheet.createRow(curRow++); createCell(row, 0, QUESTION_HEADER, headerStyle); createCell(row, 1, RESPONSE_HEADER, headerStyle); Long count = 1L; if (questions != null) { for (int i = 0; i < groupList.size(); i++) { HSSFRow groupHeaderRow = sheet.createRow(curRow++); sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1)); createCell(groupHeaderRow, 0, groupList.get(i).getDisplayName(), headerStyle); for (QuestionDto q : questions.get(groupList.get(i))) { int questionStartRow = curRow; HSSFRow tempRow = sheet.createRow(curRow++); if (q.getQuestionDependency() != null) { // if there is a dependency, add a row about not // answering unless the dependency is satisfied sheet.addMergedRegion(new CellRangeAddress(curRow - 1, curRow - 1, 0, 1)); Long qNum = idToNumberMap.get(q.getQuestionDependency().getQuestionId()); createCell(tempRow, 0, DEP_HEAD + q.getQuestionDependency().getAnswerValue() + DEP_HEAD_TO + "Q" + qNum, depStyle); tempRow = sheet.createRow(curRow++); questionStartRow = curRow; } createCell(tempRow, 0, (count++) + ". " + formText(q.getText(), q.getTranslationMap()), questionStyle); if (q.getOptionContainerDto() != null && q.getOptionContainerDto().getOptionsList() != null) { for (QuestionOptionDto opt : q.getOptionContainerDto().getOptionsList()) { tempRow = sheet.createRow(curRow++); createCell(tempRow, 1, formText(opt.getText(), opt.getTranslationMap()) + SMALL_BLANK, null); } sheet.addMergedRegion(new CellRangeAddress(questionStartRow, curRow - 1, 0, 0)); } else { createCell(tempRow, 1, BLANK, null); } } } } FileOutputStream fileOut = new FileOutputStream(fileName); wb.write(fileOut); fileOut.close(); }