List of usage examples for org.apache.poi.ss.usermodel CreationHelper createRichTextString
RichTextString createRichTextString(String text);
From source file:rpt.GUI.ProgramStrategist.CyclePlans.CompareDialogController.java
private int writeRow(Workbook wb, Sheet sheet, Row row, TableVariant variant, Map<String, Map<String, String>> diffList, Boolean colorChanges, Boolean addOldSOP) { //Used for placing comment at the right position CreationHelper factory = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); //Create new style XSSFCellStyle styleRed = (XSSFCellStyle) wb.createCellStyle(); XSSFCellStyle styleBlack = (XSSFCellStyle) wb.createCellStyle(); XSSFFont fontRed = (XSSFFont) wb.createFont(); fontRed.setColor(new XSSFColor(new java.awt.Color(255, 0, 0))); XSSFFont fontBlack = (XSSFFont) wb.createFont(); fontBlack.setColor(new XSSFColor(new java.awt.Color(0, 0, 0))); styleRed.setFont(fontRed);/*from ww w .ja v a2s . c o m*/ styleBlack.setFont(fontBlack); //xEtract differences to highlight Map<String, String> differences; if (diffList != null) { differences = diffList.get(variant.getVariantID()); } else { differences = new HashMap<String, String>(); } //Start with column 0 int cols = 0; //Create string with columns to print String[] columns = { "Plant", "Platform", "Vehicle", "Propulsion", "Denomination", "Fuel", "EngineFamily", "Generation", "EngineCode", "Displacement", "EnginePower", "ElMotorPower", "Torque", "TorqueOverBoost", "GearboxType", "Gears", "Gearbox", "Driveline", "TransmissionCode", "CertGroup", "EmissionClass", "StartOfProd", "EndOfProd" }; Cell cell; for (int i = 0; i < columns.length; i++) { cell = row.createCell(i); if (differences.containsKey(columns[i])) { cell.setCellStyle(styleRed); // position the comment anchor.setCol1(cell.getColumnIndex()); anchor.setCol2(cell.getColumnIndex() + 1); anchor.setRow1(row.getRowNum()); anchor.setRow2(row.getRowNum() + 3); // Create the comment and set the text+author Comment comment = drawing.createCellComment(anchor); RichTextString str = factory.createRichTextString(differences.get(columns[i])); comment.setString(str); comment.setAuthor("RPT"); // Assign the comment to the cell cell.setCellComment(comment); } else { cell.setCellStyle(styleBlack); } cell.setCellValue(variant.getValue(columns[i])); cols++; } if (addOldSOP) { cell = row.createCell(23); cell.setCellValue(variant.getOldSOP()); cols++; } if (addOldSOP) { cell = row.createCell(24); cell.setCellValue(variant.getOldEOP()); cols++; } return cols; }
From source file:uk.gov.ofwat.RefTest.java
License:Open Source License
public void writeXLS() throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); CreationHelper creationHelper = wb.getCreationHelper(); // create a new sheet Sheet s = wb.createSheet();// w ww .jav a 2 s .com // declare a row object reference Row r = null; // declare a cell object reference Cell c = null; // create 2 cell styles XSSFCellStyle cs = wb.createCellStyle(); XSSFCellStyle cs2 = wb.createCellStyle(); DataFormat df = wb.createDataFormat(); // create 2 fonts objects Font f = wb.createFont(); Font f2 = wb.createFont(); // Set font 1 to 12 point type, blue and bold f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.RED.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // Set font 2 to 10 point type, red and bold f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.RED.getIndex()); f2.setBoldweight(Font.BOLDWEIGHT_BOLD); // Set cell style and formatting cs.setFont(f); cs.setDataFormat(df.getFormat("#,##0.0")); // Set the other cell style and formatting cs2.setBorderBottom(cs2.BORDER_THIN); cs2.setDataFormat((short) BuiltinFormats.getBuiltinFormat("text")); cs2.setFont(f2); // Define a few rows for (int rownum = 0; rownum < 30; rownum++) { r = s.createRow(rownum); for (int cellnum = 0; cellnum < 10; cellnum += 2) { c = r.createCell(cellnum); Cell c2 = r.createCell(cellnum + 1); c.setCellValue((double) rownum + (cellnum / 10)); c2.setCellValue(creationHelper.createRichTextString("Hello! " + cellnum)); } } File file = new File("d:\\out.xls"); FileOutputStream fos = new FileOutputStream(file); wb.write(fos); // fos.write(wb.getBytes()); // fos.flush(); // fos.close(); }
From source file:vista.ui.VistaGenerarEstadisticas.java
License:Open Source License
/** * Este mtodo sirve para crear una nueva hoja de Excel en el libro de * trabajo seleccionado, no es trabajo de este mtodo guardar los cambios en * el sistema de archivos, slo agrega la hoja de Excel al libro de trabajo * en memoria./*from w w w .j av a 2 s.co m*/ * * @param workbook el objeto Workbook al que se le adjuntar la nueva hoja * de Excel * @param nombre un String con el nombre que tendr la nueva hoja de Excel * @param datos un objeto TablaEstadisticas con los datos y los nombres de * columnas que sern agregados a la hoja */ private void crearHoja(Workbook workbook, String nombre, TablaEstadisticas datos) { //Crear la hoja CreationHelper createHelper = workbook.getCreationHelper(); Sheet nuevaHoja = workbook.createSheet(WorkbookUtil.createSafeSheetName(nombre)); //Escribir los nombres de las columnas en la hoja Row headers = nuevaHoja.createRow(0); for (int i = 0; i < datos.getColumnCount(); i++) { //Crear headers con estilo Cell cell = headers.createCell(i); CellStyle style = crearEstiloCelda(workbook, IndexedColors.BLACK, CellStyle.BORDER_THICK, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER); cell.setCellValue(createHelper.createRichTextString(datos.getColumnName(i))); cell.setCellStyle(style); //Autoajustar nuevaHoja.autoSizeColumn(i); } //Escribir los datos de la tabla en la hoja for (int i = 0; i < datos.getRowCount(); i++) { Row fila = nuevaHoja.createRow(i + 1); CellStyle style = crearEstiloCelda(workbook, IndexedColors.GREEN, CellStyle.BORDER_THIN, (short) -18, (short) -18); for (int j = 0; j < datos.getColumnCount(); j++) { Cell cell = fila.createCell(j); //Escribir de acuerdo al tipo de dato if (datos.getColumnClass(j).equals(String.class)) { cell.setCellValue(createHelper.createRichTextString(datos.getValueAt(i, j).toString())); } else if (datos.getColumnClass(j).equals(Integer.class)) { cell.setCellValue((int) datos.getValueAt(i, j)); } else if (datos.getColumnClass(j).equals(Double.class)) { cell.setCellValue((double) datos.getValueAt(i, j)); } else if (datos.getColumnClass(j).equals(Turno.class)) { cell.setCellValue(((Turno) datos.getValueAt(i, j)).toString()); } //Agregar el estilo cell.setCellStyle(style); } } }