/** * Write out this workbook to an OutputStream. * * @param stream - the java OutputStream you wish to write to * @exception IOException if anything can't be written. */ @Override public void write(OutputStream stream) throws IOException { flushSheets(); //Save the template File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx"); boolean deleted; try { try (FileOutputStream os = new FileOutputStream(tmplFile)) { _wb.write(os); } //Substitute the template entries with the generated sheet data files try (ZipSecureFile zf = new ZipSecureFile(tmplFile); ZipFileZipEntrySource source = new ZipFileZipEntrySource(zf)) { injectData(source, stream); } } finally { deleted = tmplFile.delete(); } if(!deleted) { throw new IOException("Could not delete temporary file after processing: " + tmplFile); } }
@Override public void write(OutputStream stream) throws IOException { flushSheets(); EncryptedTempData tempData = new EncryptedTempData(); ZipEntrySource source = null; try { try (OutputStream os = tempData.getOutputStream()) { getXSSFWorkbook().write(os); } // provide ZipEntrySource to poi which decrypts on the fly source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream()); injectData(source, stream); } finally { tempData.dispose(); IOUtils.closeQuietly(source); } }
/** * this method write the XSSFWorkbook object data into embedded excel file * * @param workbook * XSSFworkbook object * @throws IOException * @throws InvalidFormatException * @since POI 4.0.0 */ public void saveWorkbook(XSSFWorkbook workbook) throws IOException, InvalidFormatException { PackagePart worksheetPart = getWorksheetPart(); if (worksheetPart == null) { POIXMLRelation chartRelation = getChartRelation(); POIXMLRelation chartWorkbookRelation = getChartWorkbookRelation(); POIXMLFactory chartFactory = getChartFactory(); if (chartRelation != null && chartWorkbookRelation != null && chartFactory != null) { worksheetPart = createWorksheetPart(chartRelation, chartWorkbookRelation, chartFactory); } else { throw new InvalidFormatException("unable to determine chart relations"); } } try (OutputStream xlsOut = worksheetPart.getOutputStream()) { setWorksheetPartCommitted(); workbook.write(xlsOut); } }
workbook.write(out); out.close(); System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
public class SO{ public static void main(String[] args) { try { FileInputStream is = new FileInputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx")); XSSFWorkbook wb = new XSSFWorkbook(is); String header = "123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789"; Sheet sheet = wb.getSheet("Sheet1"); sheet.setColumnWidth(0, 18000); Row row = sheet.createRow(0); Cell cell = row.createCell(0); if(header.length() > 50){ //Length of String for my test sheet.setColumnWidth(0, 18000); //Set column width, you'll probably want to tweak the second int CellStyle style = wb.createCellStyle(); //Create new style style.setWrapText(true); //Set wordwrap cell.setCellStyle(style); //Apply style to cell cell.setCellValue(header); //Write header } wb.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\bob.xlsx"))); } catch (IOException e) { e.printStackTrace(); } } }
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); //Create some data to build the pivot table on setCellData(sheet); XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D4"), new CellReference("H5")); //Configure the pivot table //Use first column as row label pivotTable.addRowLabel(0); //Sum up the second column pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); //Set the third column as filter pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2); //Add filter on forth column pivotTable.addReportFilter(3); FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx"); wb.write(fileOut); fileOut.close(); }
public void write(OutputStream os) throws IOException { workbook.write(os); } }
@Override protected void write(XSSFWorkbook workbook, ByteArrayOutputStream output) throws IOException { workbook.write(output); }
for (XSSFWorkbook current : workbooks) { zipEntry = new ZipEntry(REPORT_PREFIX + workBookIndex + EXCEL_EXTENSION); zipOutputStream.putNextEntry(zipEntry); workbookByteArrayStream = new ByteArrayOutputStream(); current.write(workbookByteArrayStream); zipEntry.setSize(workbookByteArrayStream.size()); zipOutputStream.write(workbookByteArrayStream.toByteArray()); zipOutputStream.closeEntry(); } zipOutputStream.close(); return byteOutputStream;
XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(); FileOutputStream fileOut = new FileOutputStream("Example.xlsx"); wb.write(fileOut); fileOut.close();
public ExcelWorkbook save(String fileName) throws IOException { try (FileOutputStream out = new FileOutputStream(fileName)) { if (sxssfWorkbook != null) { sxssfWorkbook.write(out); } else { xssfWorkbook.write(out); } } return this; }
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sample.xlsx"); //Create a new row in current sheet Row row = sheet.createRow(0); //Create a new cell in current row Cell cell = row.createCell(0); //Set value to new value cell.setCellValue("Blahblah"); //finally write data to excel file FileOutputStream out = new FileOutputStream(new File("C:\\test.xls")); workbook.write(out); out.close();
public void setData(String adr,String sht,int rn,int cn,String val) throws Exception{ FileInputStream fsIP= new FileInputStream(new File(adr)); //Read the spreadsheet that needs to be updated XSSFWorkbook wb = new XSSFWorkbook(fsIP); //Access the workbook XSSFSheet worksheet = wb.getSheet(sht); //Access the worksheet, so that we can update / modify it. Cell cell = null; // declare a Cell object cell = worksheet.getRow(rn).getCell(cn); // Access the second cell in second row to update the value cell.setCellValue(val); // Get current cell value value and overwrite the value fsIP.close(); //Close the InputStream FileOutputStream output_file =new FileOutputStream(new File(adr)); //Open FileOutputStream to write updates wb.write(output_file); //write changes output_file.close(); //close the stream }
public static void main(String[] args) throws Exception { for (int i = 0; i < args.length; i++) { OPCPackage pkg = OPCPackage.open(args[i]); XSSFWorkbook wb = new XSSFWorkbook(pkg); int sep = args[i].lastIndexOf('.'); String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x"); FileOutputStream out = new FileOutputStream(outfile); wb.write(out); out.close(); pkg.close(); } }
//out is the file after jasper report export XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(out)); int ns = workbook.getNumberOfSheets(); for (int i = 0; i < ns; i++) { XSSFSheet sheet = workbook.getSheetAt(i); sheet.setRightToLeft(true); } FileOutputStream outStream = new FileOutputStream(out); workbook.write(outStream); outStream.close();
fis = new FileInputStream(new File(fileName)); XSSFWorkbook workbook = new XSSFWorkbook (fis); XSSFSheet sheet = workbook.getSheetAt(0); XSSFRow row1 = sheet.createRow(0); XSSFCell r1c1 = row1.createCell(0); r1c1.setCellValue("Demo"); fis.close(); FileOutputStream fos = new FileOutputStream(new File(outputFile)); workbook.write(fos); fos.close();
public static void main(String[] args) throws InvalidFormatException, IOException{ FileInputStream fis=new FileInputStream("D://Data.xlsx"); XSSFWorkbook wb= new XSSFWorkbook(fis); //XSSFSheet sh= wb.getSheetAt(0); Or XSSFSheet sh = wb.createSheet("Test"); XSSFRow row=sh.createRow(0); XSSFCell cell= row.createCell(0); //cell.setCellType(cell.CELL_TYPE_STRING); cell.setCellValue("Ish Mishra"); FileOutputStream fos=new FileOutputStream("D:\\Data.xlsx"); wb.write(fos); fos.close(); System.out.println("Excel File Written successfully");
/** * 写Excel文件 * @param wb * @param pathName */ public void writeExcel2007(XSSFWorkbook wb,String pathName) { try { wb.write(FileUtil.getFileOutStream(pathName)); } catch (IOException e) { log.error("写入Excel文件失败:"+pathName); log.error(e.getMessage()); } }
String srcPath = "C:\\projects\\source.xlsx"; String destPath = "C:\\projects\\destSheet.xlsx"; XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new File(srcPath)); XSSFSheet sheet = workbook.getSheetAt(0); XSSFRow row = sheet.getRow(1); if(row == null) { row = sheet.createRow(1); } XSSFCell cell = row.getCell(0); if(cell == null) { cell = row.createCell(0); } cell.setCellValue("Testing123"); FileOutputStream fileOut = new FileOutputStream(new File(destPath)); workbook.write(fileOut); fileOut.flush(); fileOut.close();
@Override protected void endSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception { int columns = sheet.getRow( 0 ).getPhysicalNumberOfCells(); for ( int i = 0; i < columns; i++ ) { sheet.autoSizeColumn( i ); } workbook.write( outputStream ); }