Examples of SXSSFWorkbook


Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

    @Ignore("Shifting rows is not yet implemented in SXSSFSheet")
    @Test
    public void testBug53798XLSXStream() throws IOException {
        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53798_shiftNegative_TMPL.xlsx");
        File xlsOutput = TempFile.createTempFile("testBug53798", ".xlsx");
        bug53798Work(new SXSSFWorkbook(wb), xlsOutput);
    }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

                if(wb instanceof XSSFWorkbook) {
                    newWB = new XSSFWorkbook(is);
                } else if(wb instanceof HSSFWorkbook) {
                    newWB = new HSSFWorkbook(is);
                } else if(wb instanceof SXSSFWorkbook) {
                    newWB = new SXSSFWorkbook(new XSSFWorkbook(is));
                } else {
                    throw new IllegalStateException("Unknown workbook: " + wb);
                }
                assertNotNull(newWB.getSheet("test"));
            } finally {
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

        if (tableModel == null) throw new IllegalArgumentException("Invalid tabla data model!");
        int columnCount = tableModel.getColumnCount();
        int rowCount = tableModel.getRowCount() + 1; //Include header row
        int row = 0;

        SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
        Map<String, CellStyle> styles = createStyles(wb);
        Sheet sh = wb.createSheet("Sheet 1");

        // General setup
        sh.setDisplayGridlines(true);
        sh.setPrintGridlines(false);
        sh.setFitToPage(true);
        sh.setHorizontallyCenter(true);
        PrintSetup printSetup = sh.getPrintSetup();
        printSetup.setLandscape(true);

        // Create header
        Row header = sh.createRow(row++);
        header.setHeightInPoints(20f);
        for (int i = 0; i < columnCount; i++) {
            Cell cell = header.createCell(i);
            cell.setCellStyle(styles.get("header"));
            cell.setCellValue(tableModel.getColumnName(i));
        }

        // Create data rows
        for(; row < rowCount; row++){
            Row _row = sh.createRow(row);
            for(int cellnum = 0; cellnum < columnCount; cellnum++){
                Cell cell = _row.createCell(cellnum);
                Object value = tableModel.getValueAt(row-1, cellnum);
                if (value instanceof Short || value instanceof Long || value instanceof Integer || value instanceof BigInteger) {
                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(styles.get("integer_number_cell"));
                    if (value!= null) cell.setCellValue( ((Number)value).doubleValue());
                } else if (value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(styles.get("decimal_number_cell"));
                    if (value!= null) cell.setCellValue( ((Number)value).doubleValue());
                } else if (value instanceof Date) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellStyle(styles.get("date_cell"));
                    if (value!= null) cell.setCellValue( (Date)value );
                } else if (value instanceof Interval) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellStyle(styles.get("text_cell"));
                    if (value!= null) cell.setCellValue( ((Interval)value).getDescription(LocaleManager.currentLocale()) );
                } else {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellStyle(styles.get("text_cell"));
                    if (value!= null) cell.setCellValue( value.toString() );
                }
            }

        }

        // Adjust column size
        for (int i = 0; i < columnCount; i++) {
            sh.autoSizeColumn(i);
        }

        // Put border around table data
        CellRangeAddress range = new CellRangeAddress(0, rowCount-1, 0, columnCount-1);
        RegionUtil.setBorderRight(BorderFormatting.BORDER_THIN, range, sh, wb);
        RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
        RegionUtil.setBorderLeft(BorderFormatting.BORDER_THIN, range, sh, wb);
        RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
        RegionUtil.setBorderTop(BorderFormatting.BORDER_THIN, range, sh, wb);
        RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
        RegionUtil.setBorderBottom(BorderFormatting.BORDER_THIN, range, sh, wb);
        RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);

        ByteArrayInputStream bis = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            wb.write(bos);
            bis = new ByteArrayInputStream(bos.toByteArray());
            bos.close();
        } catch (IOException e) {
            log.error("Data export error: ", e);
        }

        // Dispose of temporary files backing this workbook on disk
        if (!wb.dispose()) log.warn("Could not dispose of temporary file associated to data export!");

        return bis;
    }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

        assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
      }

      // for XSSF also test with SXSSF
      if(isXSSF) {
      Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
        for (int si = 0; si < swb.getNumberOfSheets(); si++) {
          Sheet sh = swb.getSheetAt(si);
          assertNotNull(sh.getSheetName());
          assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow());
        }
      }
  }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

        assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol());
      }

      // for XSSF also test with SXSSF
      if(isXSSF) {
      Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb);
        for (int si = 0; si < swb.getNumberOfSheets(); si++) {
          Sheet sh = swb.getSheetAt(si);
          assertNotNull(sh.getSheetName());
          assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol());
        }
      }
  }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

 
 
  public Excel(String sheetName, short xmlFormat, int rows){
    if(FORMAT_XSSF==xmlFormat) workbook=new XSSFWorkbook();
    else if(FORMAT_HSSF==xmlFormat) workbook=new HSSFWorkbook();
    else if(FORMAT_SXSSF==xmlFormat) workbook=new SXSSFWorkbook();
    this.xmlFormat=xmlFormat;
        Sheet sheet = workbook.createSheet();
        drawing = sheet.createDrawingPatriarch();
        workbook.setSheetName(0, sheetName);
  }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

        Long maxId = 0L;//当前查询的数据中最大的id 优化分页的

        String fileName = generateFilename(user, contextRootPath, "xlsx");
        File file = new File(fileName);
        BufferedOutputStream out = null;
        SXSSFWorkbook wb = null;
        try {
            long beginTime = System.currentTimeMillis();

            wb = new SXSSFWorkbook(rowAccessWindowSize);
            wb.setCompressTempFiles(true);//生成的临时文件将进行gzip压缩

            while (true) {

                Sheet sheet = wb.createSheet();
                Row headerRow = sheet.createRow(0);
                Cell idHeaderCell = headerRow.createCell(0);
                idHeaderCell.setCellValue("编号");
                Cell contentHeaderCell = headerRow.createCell(1);
                contentHeaderCell.setCellValue("内容");

                totalRows = 1;

                Page<ExcelData> page = null;

                do {
                    searchable.setPage(0, pageSize);
                    //优化分页性能
                    if(!searchable.containsSearchKey("id_in")) {
                        searchable.addSearchFilter("id", SearchOperator.gt, maxId);
                    }
                    page = findAll(searchable);

                    for (ExcelData data : page.getContent()) {
                        Row row = sheet.createRow(totalRows);
                        Cell idCell = row.createCell(0);
                        idCell.setCellValue(data.getId());
                        Cell contentCell = row.createCell(1);
                        contentCell.setCellValue(data.getContent());
                        maxId = Math.max(maxId, data.getId());
                        totalRows++;
                    }
                    //clear entity manager
                    RepositoryHelper.clear();
                } while (page.hasNextPage() && totalRows <= perSheetRows);

                if (!page.hasNextPage()) {
                    break;
                }
            }
            out = new BufferedOutputStream(new FileOutputStream(file));
            wb.write(out);

            IOUtils.closeQuietly(out);

            if (needCompress(file)) {
                fileName = compressAndDeleteOriginal(fileName);
            }

            long endTime = System.currentTimeMillis();

            Map<String, Object> context = Maps.newHashMap();
            context.put("seconds", (endTime - beginTime) / 1000);
            context.put("url", fileName.replace(contextRootPath, ""));
            notificationApi.notify(user.getId(), "excelExportSuccess", context);
        } catch (Exception e) {
            IOUtils.closeQuietly(out);
            log.error("excel export error", e);
            Map<String, Object> context = Maps.newHashMap();
            context.put("error", e.getMessage());
            notificationApi.notify(user.getId(), "excelExportError", context);
        } finally {
            // 清除本工作簿备份在磁盘上的临时文件
            wb.dispose();
        }
    }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

    Model model = FileManager.get().loadModel("data/data.ttl");
    Query query = QueryFactory.create("SELECT * WHERE { ?s ?p ?o }");
    QueryExecution qexec = QueryExecutionFactory.create(query, model);

    FileOutputStream out = new FileOutputStream("target/sxssf.xlsx");
    Workbook wb = new SXSSFWorkbook(100);
    Sheet sh = wb.createSheet();

    int rows = 0;
    int columns = 0;
    try {
      ResultSet resultSet = qexec.execSelect();
      List<String> varNames = resultSet.getResultVars();
      List<Var> vars = new ArrayList<Var>(varNames.size());

      // first row with var names
      Row row = sh.createRow(rows++);
      for (String varName : varNames) {
        Var var = Var.alloc(varName);
        Cell cell = row.createCell(columns++);
        cell.setCellValue(var.toString());
        vars.add(var);
      }

      // other rows with bindings
      while (resultSet.hasNext()) {
        Binding bindings = resultSet.nextBinding();
        row = sh.createRow(rows++);
        columns = 0;
        for (Var var : vars) {
          Node n = bindings.get(var);
          if (n != null) {
            Cell cell = row.createCell(columns++);
            String value = FmtUtils.stringForNode(n, (SerializationContext) null);
            cell.setCellValue(value);
          }
        }
      }
    } finally {
      qexec.close();
    }

    wb.write(out);
    out.close();
  }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

    private SXSSFITestDataProvider() {
        // enforce singleton
    }
    public Workbook openSampleWorkbook(String sampleFileName) {
      XSSFWorkbook xssfWorkbook = XSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
        SXSSFWorkbook swb = new SXSSFWorkbook(xssfWorkbook);
        instances.add(swb);
      return swb;
    }
View Full Code Here

Examples of org.apache.poi.xssf.streaming.SXSSFWorkbook

            throw new RuntimeException(e);
        }
        return result;
    }
    public SXSSFWorkbook createWorkbook(){
        SXSSFWorkbook wb = new SXSSFWorkbook();
        instances.add(wb);
        return wb;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.