Package org.apache.poi.ss.usermodel

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


        verifyResource("test2-workbook.xls");
    }

    private void verifyResource(String resource) throws IOException {
        final InputStream document = this.getClass().getResourceAsStream(resource);
        final Workbook wb;
        if(resource.endsWith(".xlsx")) {
            wb = new XSSFWorkbook(document);
        } else if(resource.endsWith("xls")) {
            wb = new HSSFWorkbook(document);
        } else {
            throw new IllegalArgumentException("Unsupported extension for resource " + resource);
        }
        Assert.assertEquals(2, wb.getNumberOfSheets());
        Sheet sheet;
        for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
            sheet = wb.getSheetAt(sheetIndex);
            int rowcount = 0;
            for (Row row : sheet) {
                rowcount++;
                int cellcount = 0;
                for (Cell cell : row) {
View Full Code Here


     * @return  the width in pixels
     */
    public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {

        Sheet sheet = cell.getSheet();
        Workbook wb = sheet.getWorkbook();
        Row row = cell.getRow();
        int column = cell.getColumnIndex();

        int colspan = 1;
        for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
            if (containsCell(region, row.getRowNum(), column)) {
                if (!useMergedCells) {
                    // If we're not using merged cells, skip this one and move on to the next.
                    return -1;
                }
                cell = row.getCell(region.getFirstColumn());
                colspan = 1 + region.getLastColumn() - region.getFirstColumn();
            }
        }

        CellStyle style = cell.getCellStyle();
        int cellType = cell.getCellType();

        // for formula cells we compute the cell width for the cached formula result
        if(cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();

        Font font = wb.getFontAt(style.getFontIndex());

        AttributedString str;
        TextLayout layout;

        double width = -1;
View Full Code Here

     */
    public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){
        AttributedString str;
        TextLayout layout;

        Workbook wb = sheet.getWorkbook();
        DataFormatter formatter = new DataFormatter();
        Font defaultFont = wb.getFontAt((short) 0);

        str = new AttributedString(String.valueOf(defaultChar));
        copyAttributes(defaultFont, str, 0, 1);
        layout = new TextLayout(str.getIterator(), fontRenderContext);
        int defaultCharWidth = (int)layout.getAdvance();
View Full Code Here

     */
    public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
        AttributedString str;
        TextLayout layout;

        Workbook wb = sheet.getWorkbook();
        DataFormatter formatter = new DataFormatter();
        Font defaultFont = wb.getFontAt((short) 0);

        str = new AttributedString(String.valueOf(defaultChar));
        copyAttributes(defaultFont, str, 0, 1);
        layout = new TextLayout(str.getIterator(), fontRenderContext);
        int defaultCharWidth = (int)layout.getAdvance();
View Full Code Here

import org.apache.poi.util.TempFile;

public final class TestDateFormatConverter extends TestCase {
    private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception {

        Workbook workbook = new HSSFWorkbook();
        String sheetName;
        if( dates ) {
            if( times ) {
                sheetName = "DateTimes";
            } else {
                sheetName = "Dates";
            }
        } else {
            sheetName = "Times";
        }
        Sheet sheet = workbook.createSheet(sheetName);
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("locale");
        header.createCell(1).setCellValue("DisplayName");
        header.createCell(2).setCellValue("Excel " + styleName);
        header.createCell(3).setCellValue("java.text.DateFormat");
        header.createCell(4).setCellValue("Equals");
        header.createCell(5).setCellValue("Java pattern");
        header.createCell(6).setCellValue("Excel pattern");

        int rowNum = 1;
        for( Locale locale : DateFormat.getAvailableLocales() ) {
            try {
                Row row = sheet.createRow(rowNum++);
   
                row.createCell(0).setCellValue(locale.toString());
                row.createCell(1).setCellValue(locale.getDisplayName());
   
                DateFormat dateFormat;
                if( dates ) {
                    if( times ) {
                        dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
                    } else {
                        dateFormat = DateFormat.getDateInstance(style, locale);
                    }
                } else {
                    dateFormat = DateFormat.getTimeInstance(style, locale);
                }
   
                Cell cell = row.createCell(2);
   
                cell.setCellValue(date);
                CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
   
                String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
                String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
   
                DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
                cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
                row.createCell(3).setCellValue(dateFormat.format(date));
   
                cell.setCellStyle(cellStyle);
   
                // the formula returns TRUE is the formatted date in column C equals to the string in column D
                row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum);
                row.createCell(5).setCellValue(javaDateFormatPattern);
                row.createCell(6).setCellValue(excelFormatPattern);
            } catch (Exception e) {
                throw new RuntimeException("Failed for locale: " + locale + ", having locales: " +
                        Arrays.toString(DateFormat.getAvailableLocales()), e);
            }
        }

        File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx");
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        try {
            workbook.write(outputStream);
        } finally {
            outputStream.close();
        }

        System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel");
View Full Code Here

    }
    log( versionBffr.toString(), Project.MSG_INFO ) ;
   
    log( "Using input file: " + excelFileName, Project.MSG_INFO ) ;
   
    Workbook targetWorkbook = loadWorkbook() ;
    if( targetWorkbook == null ) {
      log( "Unable to load " + excelFileName +
                      ".  Verify the file exists and can be read.",
                      Project.MSG_ERR ) ;
      return ;
View Full Code Here

  }
 
  public void testWorkbookConstructor() throws InvalidFormatException, IOException {
        File workbookFile = new File(mortgageCalculatorFileName);
        FileInputStream fis = new FileInputStream(workbookFile);
        Workbook workbook = WorkbookFactory.create(fis);

    fixture = new ExcelAntWorkbookUtilTestHelper( workbook ) ;
   
    assertNotNull( fixture ) ;
   
View Full Code Here

    fixture = new ExcelAntWorkbookUtilTestHelper(
                mortgageCalculatorFileName ) ;
   
    assertNotNull( fixture ) ;
   
    Workbook workbook = fixture.getWorkbook() ;
   
    assertNotNull( workbook ) ;
  }
View Full Code Here

            System.err.println("Use:");
            System.err.println("  CheckFunctionsSupported <filename>");
            return;
        }
       
        Workbook wb = WorkbookFactory.create(new File(args[0]));
        CheckFunctionsSupported check = new CheckFunctionsSupported(wb);
       
        // Fetch all the problems
        List<FormulaEvaluationProblems> problems = new ArrayList<CheckFunctionsSupported.FormulaEvaluationProblems>();
        for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
            problems.add(check.getEvaluationProblems(sn));
        }
       
        // Produce an overall summary
        Set<String> unsupportedFunctions = new TreeSet<String>();
        for (FormulaEvaluationProblems p : problems) {
            unsupportedFunctions.addAll(p.unsupportedFunctions);
        }
        if (unsupportedFunctions.isEmpty()) {
            System.out.println("There are no unsupported formula functions used");
        } else {
            System.out.println("Unsupported formula functions:");
            for (String function : unsupportedFunctions) {
                System.out.println("  " + function);
            }
            System.out.println("Total unsupported functions = " + unsupportedFunctions.size());
        }
       
        // Report sheet by sheet
        for (int sn=0; sn<wb.getNumberOfSheets(); sn++) {
            String sheetName = wb.getSheetName(sn);
            FormulaEvaluationProblems probs = problems.get(sn);
           
            System.out.println();
            System.out.println("Sheet = " + sheetName);
           
View Full Code Here

        assertNotNull(sh2.getDrawingPatriarch());
    }

    @Test
    public void bug53432(){
        Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();
        wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
        assertEquals(wb.getAllPictures().size(), 1);

        wb = new HSSFWorkbook();
        wb = writeOutAndReadBack((HSSFWorkbook) wb);
        assertEquals(wb.getAllPictures().size(), 0);
        wb.addPicture(new byte[]{123,22}, Workbook.PICTURE_TYPE_JPEG);
        assertEquals(wb.getAllPictures().size(), 1);

        wb = writeOutAndReadBack((HSSFWorkbook) wb);
        assertEquals(wb.getAllPictures().size(), 1);
    }
View Full Code Here

TOP

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

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.