From: Nick Burch Date: Tue, 8 Apr 2008 10:34:43 +0000 (+0000) Subject: Example of using xssf usermodel to create a new file X-Git-Tag: REL_3_5_BETA2~115 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a9fda92b5fb7f8b7232b3c828b9b14463e98c97d;p=poi.git Example of using xssf usermodel to create a new file git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@645832 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java new file mode 100644 index 0000000000..f653d560f2 --- /dev/null +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java @@ -0,0 +1,56 @@ +package org.apache.poi.xssf.usermodel.examples; + +import java.io.FileOutputStream; + +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; +import org.apache.poi.hssf.usermodel.HSSFPatriarch; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.*; + +public class CreateNewSpreadsheet { + public static void main(String[] args) throws Exception { + Workbook wb = new XSSFWorkbook(); + CreationHelper createHelper = wb.getCreationHelper(); + + Sheet s1 = wb.createSheet("Sheet One"); + Sheet s2 = wb.createSheet("2nd Sheet"); + + // Create a few cells + s1.createRow(0); + s1.createRow(1); + s1.createRow(2); + s1.createRow(3); + s2.createRow(2); + + s1.getRow(0).createCell(0).setCellValue(1.2); + s1.getRow(0).createCell(1).setCellValue(createHelper.createRichTextString("Sheet 1 text")); + s1.getRow(1).createCell(0).setCellValue(4.22); + s1.getRow(2).createCell(0).setCellValue(5.44); + s1.getRow(3).createCell(0).setCellFormula("SUM(A1:A3)"); + + s2.getRow(2).createCell(1).setCellValue(createHelper.createRichTextString("Sheet 2")); + + + // Comment + Comment comment = ((XSSFSheet)s1).createComment(); +// HSSFPatriarch patriach = (HSSFPatriarch)s1.createDrawingPatriarch(); +// Comment comment = patriach.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5)); + + comment.setAuthor("Apache POI"); + comment.setString(createHelper.createRichTextString("I am a comment")); + s1.getRow(0).getCell(0).setCellComment(comment); + + // Hyperlink + Hyperlink hyperlink = createHelper.createHyperlink(Hyperlink.LINK_URL); + hyperlink.setAddress("http://poi.apache.org/"); + hyperlink.setLabel("Link to POI"); + s1.getRow(1).createCell(1).setHyperlink(hyperlink); + s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI")); + + // Save + FileOutputStream fout = new FileOutputStream("/tmp/NewFile.xlsx"); + wb.write(fout); + fout.close(); + System.out.println("Done"); + } +}