Package org.apache.poi.ss.usermodel

Examples of org.apache.poi.ss.usermodel.CreationHelper


    protected void write(FeatureCollectionResponse featureCollection, OutputStream output,
        Operation getFeature) throws IOException ,ServiceException {
   
        // Create the workbook
        Workbook wb = getNewWorkbook();
        CreationHelper helper = wb.getCreationHelper();
        ExcelCellStyles styles = new ExcelCellStyles(wb);

        for (Iterator it = featureCollection.getFeature().iterator(); it.hasNext();) {
            SimpleFeatureCollection fc = (SimpleFeatureCollection) it.next();

            // create the sheet for this feature collection
            Sheet sheet = wb.createSheet(fc.getSchema().getTypeName());

            // write out the header
            Row header = sheet.createRow(0);

            SimpleFeatureType ft = fc.getSchema();
            Cell cell;

            cell = header.createCell(0);
            cell.setCellValue(helper.createRichTextString("FID"));
            for (int i = 0; i < ft.getAttributeCount() && i < colLimit; i++) {
                AttributeDescriptor ad = ft.getDescriptor(i);
                cell = header.createCell(i + 1);
                cell.setCellValue(helper.createRichTextString(ad.getLocalName()));
                cell.setCellStyle(styles.getHeaderStyle());
            }

            // write out the features
            SimpleFeatureIterator i = fc.features();
            int r = 0; // row index
            try {
                Row row;
                while (i.hasNext()) {
                    r++; // start at 1, since header is at 0

                    row = sheet.createRow(r);
                    cell = row.createCell(0);

                    if (r == (rowLimit - 1) && i.hasNext()) {
                        // there are more features than rows available in this
                        // Excel format. write out a warning line and break
                        RichTextString rowWarning = helper.createRichTextString(TRUNCATE_WARNING
                                + ": ROWS " + r + " - " + fc.size() + " NOT SHOWN");
                        cell.setCellValue(rowWarning);
                        cell.setCellStyle(styles.getWarningStyle());
                        break;
                    }

                    SimpleFeature f = i.next();
                    cell.setCellValue(helper.createRichTextString(f.getID()));
                    for (int j = 0; j < f.getAttributeCount() && j < colLimit; j++) {
                        Object att = f.getAttribute(j);
                        if (att != null) {
                            cell = row.createCell(j + 1);
                            if (att instanceof Number) {
                                cell.setCellValue(((Number) att).doubleValue());
                            } else if (att instanceof Date) {
                                cell.setCellValue((Date) att);
                                cell.setCellStyle(styles.getDateStyle());
                            } else if (att instanceof Calendar) {
                                cell.setCellValue((Calendar) att);
                                cell.setCellStyle(styles.getDateStyle());
                            } else if (att instanceof Boolean) {
                                cell.setCellValue((Boolean) att);
                            } else {
                                // ok, it seems we have no better way than dump it as a string
                                String stringVal = att.toString();

                                // if string length > excel cell limit, truncate it and warn the
                                // user, otherwise excel workbook will be corrupted
                                if (stringVal.length() > CELL_CHAR_LIMIT) {
                                    stringVal = TRUNCATE_WARNING
                                            + " "
                                            + stringVal.substring(0, CELL_CHAR_LIMIT
                                                    - TRUNCATE_WARNING.length() - 1);
                                    cell.setCellStyle(styles.getWarningStyle());
                                }
                                cell.setCellValue(helper.createRichTextString(stringVal));

                            }
                        }
                    }
                }
View Full Code Here


     * Tests that cell formatting stuff works as expected
     */
    public void testCellFormatting() {
      Workbook workbook = new XSSFWorkbook();
      Sheet sheet = workbook.createSheet();
      CreationHelper creationHelper = workbook.getCreationHelper();
     
      CellStyle cs = workbook.createCellStyle();
      assertNotNull(cs);
     
      assertNotNull(creationHelper);
      assertNotNull(creationHelper.createDataFormat());
     
      cs.setDataFormat(
          creationHelper.createDataFormat().getFormat("yyyy/mm/dd")
      );
      Cell cell = sheet.createRow(0).createCell((short)0);
      cell.setCellValue(new Date(654321));
     
      assertNull(cell.getCellStyle());
View Full Code Here

     * XSSFSheet autoSizeColumn() on empty RichTextString fails
     */
    public void test48325() {
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("Test");
        CreationHelper factory = wb.getCreationHelper();

        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);

        XSSFFont font = wb.createFont();
        RichTextString rts = factory.createRichTextString("");
        rts.applyFont(font);
        cell.setCellValue(rts);

        sheet.autoSizeColumn(0);
    }
View Full Code Here

{
  public static void main(String[] args) throws Exception
  {
    Workbook wb = new HSSFWorkbook();
   
    CreationHelper createHelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");
   
    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short)0);
    // Create a cell and put a value in it.
    Cell cell = row.createCell(0);
    cell.setCellValue(1);
   
    // Or do it on one line.
    row.createCell(1).setCellValue(1.2);
    row.createCell(2).setCellValue(
    createHelper.createRichTextString("This is a string"));
    row.createCell(3).setCellValue(true);
   
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("data/DataInCells_Apache.xls");
    wb.write(fileOut);
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.usermodel.CreationHelper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.