private void insertItemToSheet(String table, HSSFSheet sheet, ArrayList<String> columns) { HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); Cursor cursor = database.rawQuery("select * from " + table, null); cursor.moveToFirst(); int n = 1; while (!cursor.isAfterLast()) { HSSFRow rowA = sheet.createRow(n); for (int j = 0; j < columns.size(); j++) { HSSFCell cellA = rowA.createCell(j); if (cursor.getType(j) == Cursor.FIELD_TYPE_BLOB) { HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) j, n, (short) (j + 1), n + 1); anchor.setAnchorType(3); patriarch.createPicture(anchor, workbook.addPicture(cursor.getBlob(j), HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { cellA.setCellValue(new HSSFRichTextString(cursor.getString(j))); } } n++; cursor.moveToNext(); } cursor.close(); }
private void createHeader(HSSFSheet sheet, List<ExpedientConsultaDissenyDto> expedientsConsultaDissenyDto) { int rowNum = 0; int colNum = 0; // Capçalera HSSFRow xlsRow = sheet.createRow(rowNum++); HSSFCell cell; cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize("Expedient"))); cell.setCellStyle(headerStyle); Iterator<Entry<String, DadaIndexadaDto>> it = expedientsConsultaDissenyDto.get(0).getDadesExpedient().entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, DadaIndexadaDto> e = (Map.Entry<String, DadaIndexadaDto>)it.next(); sheet.autoSizeColumn(colNum); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(e.getValue().getEtiqueta()))); cell.setCellStyle(headerStyle); } }
/** * Write the value to the cell. Override this method if you have complex data types that may need to be exported. * @param value the value of the cell * @param cell the cell to write it to */ protected void writeCell(Object value, HSSFCell cell, HSSFWorkbook wb) { if (value instanceof Number) { Number num = (Number) value; cell.setCellValue(num.doubleValue()); } else if (value instanceof Date) { HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat( wb.getCreationHelper().createDataFormat().getFormat("dd/MM/yyyy HH:mm")); cell.setCellStyle(cellStyle); cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue((Date) value); } else if (value instanceof Calendar) { cell.setCellValue((Calendar) value); } else { cell.setCellValue(new HSSFRichTextString(escapeColumnValue(value))); } }
public String getHtmlValue(Cell cell) { if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType() || Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { cell.setCellType(Cell.CELL_TYPE_STRING); return cell.getStringCellValue(); } else if (Cell.CELL_TYPE_STRING == cell.getCellType()) { if (cell.getRichStringCellValue().numFormattingRuns() == 0) { return XmlEscapers.xmlContentEscaper().escape(cell.getStringCellValue()); } else if (is07) { return getXSSFRichString((XSSFRichTextString) cell.getRichStringCellValue()); } else { return getHSSFRichString((HSSFRichTextString) cell.getRichStringCellValue()); } } return ""; }
/** * 03版本复杂数据 * @param rich * @return */ private String getHSSFRichString(HSSFRichTextString rich) { int nums = rich.numFormattingRuns(); StringBuilder sb = new StringBuilder(); String text = rich.toString(); int currentIndex = 0; sb.append(text.substring(0, rich.getIndexOfFormattingRun(0))); for (int i = 0; i < nums; i++) { sb.append("<span "); sb.append("class='font_" + rich.getFontOfFormattingRun(i)); sb.append("_"); sb.append(cssRandom); sb.append("'>"); currentIndex = rich.getIndexOfFormattingRun(i); if (i < nums - 1) { sb.append(XmlEscapers.xmlContentEscaper() .escape(text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1)))); } else { sb.append(XmlEscapers.xmlContentEscaper() .escape(text.substring(currentIndex, text.length()))); } sb.append("</span>"); } return sb.toString(); }
private boolean createHeader(WorkbookGeneratorContext context, HSSFSheet sheet, HSSFCellStyle style) { if (context.headerNames == null || context.headerNames.isEmpty()) { return false; } int headerRowIndex = 0; HSSFRow rowHeader = sheet.createRow(headerRowIndex); for (int i = 0; i < context.headerNames.size(); i++) { HSSFCell cell = rowHeader.createCell(i); sheet.autoSizeColumn((short) i); cell.setCellStyle(style); cell.setCellValue(new HSSFRichTextString(context.headerNames.get(i))); } return true; }
/** * 03版本复杂数据 * @param rich * @return */ private String getHSSFRichString(HSSFRichTextString rich) { int nums = rich.numFormattingRuns(); StringBuilder sb = new StringBuilder(); String text = rich.toString(); int currentIndex = 0; sb.append(text.substring(0, rich.getIndexOfFormattingRun(0))); for (int i = 0; i < nums; i++) { sb.append("<span "); sb.append("class='font_" + rich.getFontOfFormattingRun(i)); sb.append("_"); sb.append(cssRandom); sb.append("'>"); currentIndex = rich.getIndexOfFormattingRun(i); if (i < nums - 1) { sb.append(XmlEscapers.xmlContentEscaper().escape( text.substring(currentIndex, rich.getIndexOfFormattingRun(i + 1)))); } else { sb.append(XmlEscapers.xmlContentEscaper().escape( text.substring(currentIndex, text.length()))); } sb.append("</span>"); } return sb.toString(); }
protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont, Locale locale) { String text = styledText.getText(); HSSFRichTextString richTextStr = new HSSFRichTextString(text); int runLimit = 0; AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator(); while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) { Map<Attribute,Object> attributes = iterator.getAttributes(); JRFont runFont = attributes.isEmpty()? defaultFont : new JRBaseFont(attributes); short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null ? getWorkbookColor((Color)attributes.get(TextAttribute.FOREGROUND)).getIndex() : forecolor; HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale); richTextStr.applyFont(iterator.getIndex(), runLimit, font); iterator.setIndex(runLimit); } return richTextStr; }
public void createContent(WebInput wi, DocInfo di)throws ControllerException { HSSFWorkbook wb = di.getExcelDocument(); // 创建HSSFSheet对象 HSSFSheet sheet = wb.createSheet("sheet0"); // 创建HSSFRow对象 HSSFRow row = sheet.createRow((short) 0); // 创建HSSFCell对象 HSSFCell cell = row.createCell((short) 0); // 用来处理中文问题 // cell.setEncoding(HSSFCell.ENCODING_UTF_16); // 设置单元格的值 // cell.setCellValue("Hello World! 你好,中文世界"); String info = wi.getParameter("info"); HSSFRichTextString rts = new HSSFRichTextString(info); cell.setCellValue(rts); HSSFCell cell2 = row.createCell((short) 1); cell2.setCellValue(new HSSFRichTextString( "Beetle Web Framework 页面生成Excel文件演示!")); }
public static Integer findColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == Cell.CELL_TYPE_STRING) { final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
public static Integer findMatchColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != Cell.CELL_TYPE_STRING) { continue; } final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
public static CellLocation findCell(final HSSFSheet sheet, final String str, final int colNum) { for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet.getLastRowNum() + 1; rowNum++) { final HSSFRow row = sheet.getRow(rowNum); if (row == null) { continue; } final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (!Check.isEmpty(cellValue.getString())) { if (cellValue.getString().equals(str)) { return new CellLocation(rowNum, (short) colNum); } } } return null; }
public static String getCellValue(final HSSFSheet sheet, final int r, final int c) { final HSSFRow row = sheet.getRow(r); if (row == null) { return null; } final HSSFCell cell = row.getCell(c); if (cell == null) { return null; } final HSSFRichTextString cellValue = cell.getRichStringCellValue(); return cellValue.toString(); }
protected void setColumnData(final Map<String, String> keywordsValueMap, final ColumnTemplate columnTemplate, final HSSFRow row, final NormalColumn normalColumn, final TableView tableView, final int order) { for (final int columnNum : columnTemplate.columnTemplateMap.keySet()) { final HSSFCell cell = row.createCell(columnNum); final String template = columnTemplate.columnTemplateMap.get(columnNum); String value = null; if (KEYWORD_ORDER.equals(template)) { value = String.valueOf(order); } else { value = getColumnValue(keywordsValueMap, normalColumn, tableView, template); } try { final double num = Double.parseDouble(value); cell.setCellValue(num); } catch (final NumberFormatException e) { final HSSFRichTextString text = new HSSFRichTextString(value); cell.setCellValue(text); } } }
@Override protected void writeDirectTestData(final ERTable table, final Map<NormalColumn, String> data, final String database) { final HSSFRow row = sheet.createRow(rowNum++); int col = 0; for (final NormalColumn column : table.getExpandedColumns()) { final HSSFCell cell = row.createCell(col++); final String value = Format.null2blank(data.get(column)); if (value == null || "null".equals(value.toLowerCase())) { } else { cell.setCellValue(new HSSFRichTextString(value)); } } }
public static Integer findColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
public static Integer findMatchColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING) { continue; } HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
public static CellLocation findCell(HSSFSheet sheet, String str, int colNum) { for (int rowNum = sheet.getFirstRowNum(); rowNum < sheet .getLastRowNum() + 1; rowNum++) { HSSFRow row = sheet.getRow(rowNum); if (row == null) { continue; } HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (!Check.isEmpty(cellValue.getString())) { if (cellValue.getString().equals(str)) { return new CellLocation(rowNum, (short) colNum); } } } return null; }
public static String getCellValue(HSSFSheet sheet, int r, int c) { HSSFRow row = sheet.getRow(r); if (row == null) { return null; } HSSFCell cell = row.getCell(c); if (cell == null) { return null; } HSSFRichTextString cellValue = cell.getRichStringCellValue(); return cellValue.toString(); }
@Override protected void writeDirectTestData(ERTable table, Map<NormalColumn, String> data, String database) { HSSFRow row = this.sheet.createRow(this.rowNum++); int col = 0; for (NormalColumn column : table.getExpandedColumns()) { HSSFCell cell = row.createCell(col++); String value = Format.null2blank(data.get(column)); if (value == null || "null".equals(value.toLowerCase())) { } else { cell.setCellValue(new HSSFRichTextString(value)); } } }
private void writeTotal(String value, double total) { if (this.assertRequiredState()) { int rowNum = this.sheet.getLastRowNum(); this.currentRow = this.sheet.createRow(++rowNum); this.colNum = 0; prepareCell(); prepareCell(); prepareCell(); this.currentCell.setCellValue(new HSSFRichTextString("------------")); this.currentRow = this.sheet.createRow(++rowNum); this.colNum = 0; prepareCell(); prepareCell(); this.currentCell.setCellValue(new HSSFRichTextString(value + " Total:")); prepareCell(); this.currentCell.setCellValue(total); } }
/** * @see org.displaytag.render.TableWriterTemplate#writeCaption(org.displaytag.model.TableModel) */ @Override protected void writeCaption(TableModel model) throws Exception { HSSFCellStyle style = this.wb.createCellStyle(); HSSFFont bold = this.wb.createFont(); bold.setBoldweight(Font.BOLDWEIGHT_BOLD); bold.setFontHeightInPoints((short) 14); style.setFont(bold); style.setAlignment(CellStyle.ALIGN_CENTER); this.colNum = 0; this.currentRow = this.sheet.createRow(this.sheetRowNum++); this.currentCell = this.currentRow.createCell(this.colNum); this.currentCell.setCellStyle(style); String caption = model.getCaption(); this.currentCell.setCellValue(new HSSFRichTextString(caption)); this.rowSpanTable(model); }
/** * Gets the font index of the <code>Font</code> in use at the specified * position in the given <code>RichTextString</code>. * @param richTextString The <code>RichTextString</code>. * @param fmtIndex The 0-based index of the formatting run. * @return The font index: If HSSF, a <code>short</code>. If XSSF, an * <code>XSSFFont</code>. */ public static Object getFontAtIndex(RichTextString richTextString, int fmtIndex) { if (richTextString instanceof HSSFRichTextString) { // Returns a short. return ((HSSFRichTextString) richTextString).getFontAtIndex(fmtIndex); } else if (richTextString instanceof XSSFRichTextString) { try { // Instead of returning null, getFontAtIndex (eventually) throws a // NullPointerException. It extracts a "CTRElt" from an array, and // it extracts a "CTRPrElt" from the "CTRElt". The "CTRprElt" can // be null if there is no font at the formatting run. Then, when // creating a "CTFont", it calls a method on the null "CTRPrElt". // Return an XSSFFont. return ((XSSFRichTextString) richTextString).getFontAtIndex(fmtIndex); } catch (NullPointerException e) { // Detect this case and return null. if (DEBUG) System.err.println(" NullPointerException caught!"); return null; } } else throw new IllegalArgumentException("Unexpected RichTextString type: " + richTextString.getClass().getName() + ": " + richTextString.getString()); }
@SuppressWarnings("resource") @Test public void testAddRow() { Calendar cal = Calendar.getInstance(); Date date = new Date(); writer.addRow("def"); writer .addRow(null, true, cal, date, 1.1, new HSSFRichTextString("Hello!"), new XSSFRichTextString("World."), new HSSFWorkbook() .getCreationHelper().createHyperlink(HyperlinkType.URL), 123, "abc"); assertEquals("def", writer.getWorkbook().getSheetAt(0).rowIterator().next() .cellIterator().next().getStringCellValue()); WorkbookWriter.openXLSX().addRow(new HSSFRichTextString("Hello!"), new XSSFRichTextString("World.")); WorkbookWriter.openXLS().addRow(new HSSFRichTextString("Hello!"), new XSSFRichTextString("World.")); }
@Override protected void printHeader(List<String> header, ByteArrayOutputStream out) { wb = new HSSFWorkbook(); sheet = wb.createSheet("NextReports"); HSSFRow headerRow = sheet.createRow(0); int col = 0; if (header != null) { for (String s : header) { HSSFCell cell = headerRow.createCell(col); cell.setCellType(HSSFCell.CELL_TYPE_STRING); if (s == null) { s = ""; } cell.setCellValue(new HSSFRichTextString(s)); col++; } } }
private void createSheet(String table, HSSFSheet sheet) { HSSFRow rowA = sheet.createRow(0); ArrayList<String> columns = getColumns(table); for (int i = 0; i < columns.size(); i++) { HSSFCell cellA = rowA.createCell(i); cellA.setCellValue(new HSSFRichTextString("" + columns.get(i))); } insertItemToSheet(table, sheet, columns); }
private void createHeader(HSSFSheet sheet) { int rowNum = 0; int colNum = 0; // Capçalera HSSFRow xlsRow = sheet.createRow(rowNum++); HSSFCell cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.clau")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.darrera")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.minima")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.maxima")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.numMesures")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.mitja")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.periode")))); cell.setCellStyle(headerStyle); cell = xlsRow.createCell(colNum); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(getMessage("temps.pes")))); cell.setCellStyle(headerStyle); }
private void createHeader( HSSFWorkbook wb, HSSFSheet sheet, List<TascaDadaDto> informeCamps) { HSSFFont bold; bold = wb.createFont(); bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); bold.setColor(HSSFColor.WHITE.index); HSSFCellStyle headerStyle; headerStyle = wb.createCellStyle(); headerStyle.setFillPattern(HSSFCellStyle.FINE_DOTS); headerStyle.setFillBackgroundColor(HSSFColor.GREY_80_PERCENT.index); headerStyle.setFont(bold); int rowNum = 0; int colNum = 0; // Capçalera HSSFRow xlsRow = sheet.createRow(rowNum++); HSSFCell cell; cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize("Expedient"))); cell.setCellStyle(headerStyle); for (TascaDadaDto camp : informeCamps) { sheet.autoSizeColumn(colNum); cell = xlsRow.createCell(colNum++); cell.setCellValue(new HSSFRichTextString(StringUtils.capitalize(camp.getCampEtiqueta()))); cell.setCellStyle(headerStyle); } }
private HSSFCellStyle createCell( WorkbookGeneratorContext context, HSSFWorkbook workBook, HSSFSheet sheet, HSSFCellStyle style, Object object, HSSFRow rowData) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // 列作成 HSSFCellStyle dateStyle = null; for (int i = 0; i < context.outputFieldNames.size(); i++) { HSSFCell cell = rowData.createCell(i); sheet.autoSizeColumn((short) i); Object value = PropertyGetUtil.getValue(object, context.outputFieldNames.get(i)); if (value instanceof Date) { if (dateStyle == null) { dateStyle = initializeDateCellStyle(workBook, style); } setCellValue(cell, value); cell.setCellStyle(dateStyle); } else if (isNumber(value)) { if (value != null) { cell.setCellValue(Double.valueOf(String.valueOf(value))); } setCellValue(cell, value); setCellStyle(cell, style); } else { if (value != null) { cell.setCellValue(new HSSFRichTextString(String.valueOf(value))); } setCellValue(cell, value); setCellStyle(cell, style); } } return dateStyle; }
private void setCellValue(HSSFCell cell, Object value) { if (value == null) { return; } if (value instanceof Date) { cell.setCellValue((Date) value); } else if (NumberUtils.isNumber(String.valueOf(value))) { cell.setCellValue(Double.valueOf(String.valueOf(value))); } else { cell.setCellValue(new HSSFRichTextString(String.valueOf(value))); } }
/** * Writes the header column names */ @Override protected void writeReportHeader() throws JRException { row = sheet.createRow(0); for(int i = 0; i< columnNames.size(); i++) { String columnName = columnNames.get(i); cell = row.createCell(i); cell.setCellType(CellType.STRING); cell.setCellValue(new HSSFRichTextString(columnName)); } }
public static String getCellValue(final HSSFSheet sheet, final CellLocation location) { final HSSFRow row = sheet.getRow(location.r); final HSSFCell cell = row.getCell(location.c); final HSSFRichTextString cellValue = cell.getRichStringCellValue(); return cellValue.toString(); }