package org.apache.poi.hssf.usermodel.examples;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
-import org.apache.poi.hssf.usermodel.HSSFFont;
-import org.apache.poi.hssf.usermodel.HSSFPatriarch;
-import org.apache.poi.hssf.usermodel.HSSFRichTextString;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Drawing;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
/**
* Demonstrates how to work with excel cell comments.<p>
public class CellComments {
public static void main(String[] args) throws IOException {
- try (HSSFWorkbook wb = new HSSFWorkbook()) {
- HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
+ createWorkbook(false, ".xls");
+ createWorkbook(true, ".xlsx");
+ }
+
+ private static void createWorkbook(boolean xssf, String extension) throws IOException {
+ try (Workbook wb = WorkbookFactory.create(xssf)) {
+ Sheet sheet = wb.createSheet("Cell comments in POI " + extension);
+ CreationHelper creationHelper = wb.getCreationHelper();
// Create the drawing patriarch. This is the top level container for all shapes including cell comments.
- HSSFPatriarch patr = sheet.createDrawingPatriarch();
+ Drawing<?> patr = sheet.createDrawingPatriarch();
//create a cell in row 3
- HSSFCell cell1 = sheet.createRow(3).createCell(1);
- cell1.setCellValue(new HSSFRichTextString("Hello, World"));
+ Cell cell1 = sheet.createRow(3).createCell(1);
+ cell1.setCellValue(creationHelper.createRichTextString("Hello, World"));
//anchor defines size and position of the comment in worksheet
- HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
+ ClientAnchor clientAnchor = creationHelper.createClientAnchor();
+ clientAnchor.setCol1(4);
+ clientAnchor.setRow1(2);
+ clientAnchor.setCol2(6);
+ clientAnchor.setRow2(5);
+ Comment comment1 = patr.createCellComment(clientAnchor);
// set text in the comment
- comment1.setString(new HSSFRichTextString("We can set comments in POI"));
+ comment1.setString(creationHelper.createRichTextString("We can set comments in POI"));
//set comment author.
//you can see it in the status bar when moving mouse over the commented cell
comment1.setAuthor("Apache Software Foundation");
- // The first way to assign comment to a cell is via HSSFCell.setCellComment method
+ // The first way to assign comment to a cell is via Cell.setCellComment method
cell1.setCellComment(comment1);
//create another cell in row 6
- HSSFCell cell2 = sheet.createRow(6).createCell(1);
+ Cell cell2 = sheet.createRow(6).createCell(1);
cell2.setCellValue(36.6);
- HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 8, (short) 6, 11));
- //modify background color of the comment
- comment2.setFillColor(204, 236, 255);
+ clientAnchor = creationHelper.createClientAnchor();
+ clientAnchor.setCol1(4);
+ clientAnchor.setRow1(8);
+ clientAnchor.setCol2(6);
+ clientAnchor.setRow2(11);
+ Comment comment2 = patr.createCellComment(clientAnchor);
+ //modify background color of the comment, only available in HSSF currently
+ if (wb instanceof HSSFWorkbook) {
+ ((HSSFComment) comment2).setFillColor(204, 236, 255);
+ }
- HSSFRichTextString string = new HSSFRichTextString("Normal body temperature");
+ RichTextString string = creationHelper.createRichTextString("Normal body temperature");
//apply custom font to the text in the comment
- HSSFFont font = wb.createFont();
+ Font font = wb.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 10);
font.setBold(true);
- font.setColor(HSSFColorPredefined.RED.getIndex());
+ font.setColor(IndexedColors.RED.getIndex());
string.applyFont(font);
comment2.setString(string);
comment2.setRow(6);
comment2.setColumn(1);
- try (FileOutputStream out = new FileOutputStream("poi_comment.xls")) {
+ try (FileOutputStream out = new FileOutputStream("poi_comment" + extension)) {
wb.write(out);
}
}
@SuppressWarnings("unused")
@Internal
public class HSSFWorkbookFactory extends WorkbookFactory {
+ /**
+ * Create a new empty Workbook
+ *
+ * @return The created workbook
+ */
+ public static HSSFWorkbook createWorkbook() {
+ return new HSSFWorkbook();
+ }
+
/**
* Creates a HSSFWorkbook from the given NPOIFSFileSystem<p>
* Note that in order to properly release resources the
* by auto-detecting from the supplied input.
*/
public class WorkbookFactory {
+ /**
+ * Create a new empty Workbook, either XSSF or HSSF depending
+ * on the parameter
+ *
+ * @param xssf If an XSSFWorkbook or a HSSFWorkbook should be created
+ *
+ * @return The created workbook
+ *
+ * @throws IOException if an error occurs while reading the data
+ */
+ public static Workbook create(boolean xssf) throws IOException {
+ if(xssf) {
+ return createXSSFWorkbook();
+ } else {
+ return createHSSFWorkbook();
+ }
+ }
+
/**
* Creates a HSSFWorkbook from the given NPOIFSFileSystem<p>
*
package org.apache.poi.openxml4j.exceptions;
@SuppressWarnings("serial")
-public final class InvalidFormatException extends OpenXML4JException{
+public final class InvalidFormatException extends OpenXML4JException {
public InvalidFormatException(String message){
super(message);
* @param in
* The InputStream to read the package from
* @return A PackageBase object
+ *
+ * @throws InvalidFormatException
*/
public static OPCPackage open(InputStream in) throws InvalidFormatException,
IOException {
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class XSSFWorkbookFactory extends WorkbookFactory {
+ /**
+ * Create a new empty Workbook
+ *
+ * @return The created workbook
+ */
+ public static XSSFWorkbook createWorkbook() {
+ return new XSSFWorkbook();
+ }
/**
* Creates a XSSFWorkbook from the given OOXML Package.
* @return The created Workbook
*
* @throws IOException if an error occurs while reading the data
- * @throws InvalidFormatException
*/
public static XSSFWorkbook create(OPCPackage pkg) throws IOException {
return createWorkbook(pkg);
* @return The created Workbook
*
* @throws IOException if an error occurs while reading the data
- * @throws InvalidFormatException
*/
public static XSSFWorkbook createWorkbook(ZipPackage pkg) throws IOException {
return createWorkbook((OPCPackage)pkg);
* @return The created Workbook
*
* @throws IOException if an error occurs while reading the data
- * @throws InvalidFormatException
*/
public static XSSFWorkbook createWorkbook(OPCPackage pkg) throws IOException {
try {
* @return The created Workbook
*
* @throws IOException if an error occurs while reading the data
- * @throws InvalidFormatException
+ * @throws InvalidFormatException if the package is not valid.
*/
@SuppressWarnings("resource")
public static XSSFWorkbook createWorkbook(InputStream stream) throws IOException, InvalidFormatException {
OPCPackage pkg = OPCPackage.open(stream);
return createWorkbook(pkg);
}
-
-
}
public final class TestWorkbookFactory {
private static final String xls = "SampleSS.xls";
private static final String xlsx = "SampleSS.xlsx";
- private static final String[] xls_prot = new String[] {"password.xls", "password"};
- private static final String[] xlsx_prot = new String[]{"protected_passtika.xlsx", "tika"};
+ private static final String[] xls_protected = new String[] {"password.xls", "password"};
+ private static final String[] xlsx_protected = new String[]{"protected_passtika.xlsx", "tika"};
private static final String txt = "SampleSS.txt";
private static final POILogger LOGGER = POILogFactory.getLogger(TestWorkbookFactory.class);
public void testCreateWithPasswordFromStream() throws Exception {
Workbook wb;
-
// Unprotected, no password given, opens normally
wb = WorkbookFactory.create(
HSSFTestDataSamples.openSampleFileStream(xls), null
// Protected, correct password, opens fine
wb = WorkbookFactory.create(
- HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), xls_prot[1]
+ HSSFTestDataSamples.openSampleFileStream(xls_protected[0]), xls_protected[1]
);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
- assertCloseDoesNotModifyFile(xls_prot[0], wb);
+ assertCloseDoesNotModifyFile(xls_protected[0], wb);
wb = WorkbookFactory.create(
- HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), xlsx_prot[1]
+ HSSFTestDataSamples.openSampleFileStream(xlsx_protected[0]), xlsx_protected[1]
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
- assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
+ assertCloseDoesNotModifyFile(xlsx_protected[0], wb);
// Protected, wrong password, throws Exception
try {
wb = WorkbookFactory.create(
- HSSFTestDataSamples.openSampleFileStream(xls_prot[0]), "wrong"
+ HSSFTestDataSamples.openSampleFileStream(xls_protected[0]), "wrong"
);
- assertCloseDoesNotModifyFile(xls_prot[0], wb);
+ assertCloseDoesNotModifyFile(xls_protected[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
try {
wb = WorkbookFactory.create(
- HSSFTestDataSamples.openSampleFileStream(xlsx_prot[0]), "wrong"
+ HSSFTestDataSamples.openSampleFileStream(xlsx_protected[0]), "wrong"
);
- assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
+ assertCloseDoesNotModifyFile(xlsx_protected[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
// Protected, correct password, opens fine
wb = WorkbookFactory.create(
- HSSFTestDataSamples.getSampleFile(xls_prot[0]), xls_prot[1]
+ HSSFTestDataSamples.getSampleFile(xls_protected[0]), xls_protected[1]
);
assertNotNull(wb);
assertTrue(wb instanceof HSSFWorkbook);
- assertCloseDoesNotModifyFile(xls_prot[0], wb);
+ assertCloseDoesNotModifyFile(xls_protected[0], wb);
wb = WorkbookFactory.create(
- HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), xlsx_prot[1]
+ HSSFTestDataSamples.getSampleFile(xlsx_protected[0]), xlsx_protected[1]
);
assertNotNull(wb);
assertTrue(wb instanceof XSSFWorkbook);
assertTrue(wb.getNumberOfSheets() > 0);
assertNotNull(wb.getSheetAt(0));
assertNotNull(wb.getSheetAt(0).getRow(0));
- assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
+ assertCloseDoesNotModifyFile(xlsx_protected[0], wb);
// Protected, wrong password, throws Exception
try {
wb = WorkbookFactory.create(
- HSSFTestDataSamples.getSampleFile(xls_prot[0]), "wrong"
+ HSSFTestDataSamples.getSampleFile(xls_protected[0]), "wrong"
);
- assertCloseDoesNotModifyFile(xls_prot[0], wb);
+ assertCloseDoesNotModifyFile(xls_protected[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
try {
wb = WorkbookFactory.create(
- HSSFTestDataSamples.getSampleFile(xlsx_prot[0]), "wrong"
+ HSSFTestDataSamples.getSampleFile(xlsx_protected[0]), "wrong"
);
- assertCloseDoesNotModifyFile(xlsx_prot[0], wb);
+ assertCloseDoesNotModifyFile(xlsx_protected[0], wb);
fail("Shouldn't be able to open with the wrong password");
} catch (EncryptedDocumentException e) {
// expected here
*/
@Test
public void testFileSubclass() throws Exception {
- Workbook wb;
-
File normalXLS = HSSFTestDataSamples.getSampleFile(xls);
File normalXLSX = HSSFTestDataSamples.getSampleFile(xlsx);
File altXLS = new TestFile(normalXLS.getAbsolutePath());
File altXLSX = new TestFile(normalXLSX.getAbsolutePath());
assertTrue(altXLS.exists());
assertTrue(altXLSX.exists());
-
- wb = WorkbookFactory.create(altXLS);
- assertNotNull(wb);
- assertTrue(wb instanceof HSSFWorkbook);
-
- wb = WorkbookFactory.create(altXLSX);
- assertNotNull(wb);
- assertTrue(wb instanceof XSSFWorkbook);
+
+ try (Workbook wb = WorkbookFactory.create(altXLS)) {
+ assertNotNull(wb);
+ assertTrue(wb instanceof HSSFWorkbook);
+ }
+
+ try (Workbook wb = WorkbookFactory.create(altXLSX)) {
+ assertNotNull(wb);
+ assertTrue(wb instanceof XSSFWorkbook);
+ }
}
private static class TestFile extends File {
super(file);
}
}
+
+ /**
+ * Check that the overloaded file methods which take passwords work properly
+ */
+ @Test
+ public void testCreateEmpty() throws Exception {
+ try (Workbook wb = WorkbookFactory.create(false)) {
+ assertTrue(wb instanceof HSSFWorkbook);
+ }
+
+ try (Workbook wb = WorkbookFactory.create(true)) {
+ assertTrue(wb instanceof XSSFWorkbook);
+ }
+ }
}