use of org.apache.poi.ss.usermodel.CreationHelper in project poi by apache.
the class CellComments method main.
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
CreationHelper factory = wb.getCreationHelper();
Sheet sheet = wb.createSheet();
Cell cell1 = sheet.createRow(3).createCell(5);
cell1.setCellValue("F4");
Drawing<?> drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = factory.createClientAnchor();
Comment comment1 = drawing.createCellComment(anchor);
RichTextString str1 = factory.createRichTextString("Hello, World!");
comment1.setString(str1);
comment1.setAuthor("Apache POI");
cell1.setCellComment(comment1);
Cell cell2 = sheet.createRow(2).createCell(2);
cell2.setCellValue("C3");
Comment comment2 = drawing.createCellComment(anchor);
RichTextString str2 = factory.createRichTextString("XSSF can set cell comments");
//apply custom font to the text in the comment
Font font = wb.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short) 14);
font.setBold(true);
font.setColor(IndexedColors.RED.getIndex());
str2.applyFont(font);
comment2.setString(str2);
comment2.setAuthor("Apache POI");
comment2.setAddress(new CellAddress("C3"));
String fname = "comments.xlsx";
FileOutputStream out = new FileOutputStream(fname);
wb.write(out);
out.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.CreationHelper in project poi by apache.
the class CreateCell method main.
public static void main(String[] args) throws IOException {
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
CreationHelper creationHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 0);
// Create a cell and put a value in it.
Cell cell = row.createCell((short) 0);
cell.setCellValue(1);
//numeric value
row.createCell(1).setCellValue(1.2);
//plain string value
row.createCell(2).setCellValue("This is a string cell");
//rich text string
RichTextString str = creationHelper.createRichTextString("Apache");
Font font = wb.createFont();
font.setItalic(true);
font.setUnderline(Font.U_SINGLE);
str.applyFont(font);
row.createCell(3).setCellValue(str);
//boolean value
row.createCell(4).setCellValue(true);
//formula
row.createCell(5).setCellFormula("SUM(A1:B1)");
//date
CellStyle style = wb.createCellStyle();
style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(6);
cell.setCellValue(new Date());
cell.setCellStyle(style);
//hyperlink
row.createCell(7).setCellFormula("SUM(A1:B1)");
cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.CreationHelper in project poi by apache.
the class HyperlinkExample method main.
public static void main(String[] args) throws IOException {
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
//cell style for hyperlinks
//by default hyperlinks are blue and underlined
CellStyle hlink_style = wb.createCellStyle();
Font hlink_font = wb.createFont();
hlink_font.setUnderline(Font.U_SINGLE);
hlink_font.setColor(IndexedColors.BLUE.getIndex());
hlink_style.setFont(hlink_font);
Cell cell;
Sheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell(0);
cell.setCellValue("URL Link");
Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
link.setAddress("http://poi.apache.org/");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//link to a file in the current directory
cell = sheet.createRow(1).createCell(0);
cell.setCellValue("File Link");
link = createHelper.createHyperlink(HyperlinkType.FILE);
link.setAddress("link1.xls");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//e-mail link
cell = sheet.createRow(2).createCell(0);
cell.setCellValue("Email Link");
link = createHelper.createHyperlink(HyperlinkType.EMAIL);
//note, if subject contains white spaces, make sure they are url-encoded
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//link to a place in this workbook
//create a target sheet and cell
Sheet sheet2 = wb.createSheet("Target Sheet");
sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
cell = sheet.createRow(3).createCell(0);
cell.setCellValue("Worksheet Link");
Hyperlink link2 = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
link2.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link2);
cell.setCellStyle(hlink_style);
FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
wb.write(out);
out.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.CreationHelper in project poi by apache.
the class TestCommentsTable method bug54920.
@Test
public void bug54920() throws IOException {
final Workbook workbook = new XSSFWorkbook();
final Sheet sheet = workbook.createSheet("sheet01");
// create anchor
CreationHelper helper = sheet.getWorkbook().getCreationHelper();
ClientAnchor anchor = helper.createClientAnchor();
// place comment in A1
// NOTE - only occurs if a comment is placed in A1 first
Cell A1 = getCell(sheet, 0, 0);
//Cell A1 = getCell(sheet, 2, 2);
Drawing<?> drawing = sheet.createDrawingPatriarch();
setComment(sheet, A1, drawing, "for A1", helper, anchor);
// find comment in A1 before we set the comment in B2
Comment commentA1 = A1.getCellComment();
assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
// place comment in B2, according to Bug 54920 this removes the comment in A1!
Cell B2 = getCell(sheet, 1, 1);
setComment(sheet, B2, drawing, "for B2", helper, anchor);
// find comment in A1
Comment commentB2 = B2.getCellComment();
assertEquals("should find correct comment in B2, but had null: " + commentB2, "for B2", commentB2.getString().getString());
// find comment in A1
commentA1 = A1.getCellComment();
assertNotNull("Should still find the previous comment in A1, but had null", commentA1);
assertEquals("should find correct comment in A1, but had null: " + commentA1, "for A1", commentA1.getString().getString());
workbook.close();
}
use of org.apache.poi.ss.usermodel.CreationHelper in project poi by apache.
the class TestXSSFSheet method testInsertCommentsToClonedSheet.
/**
* See bug #52425
*/
@Test
public void testInsertCommentsToClonedSheet() {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("52425.xlsx");
CreationHelper helper = wb.getCreationHelper();
Sheet sheet2 = wb.createSheet("Sheet 2");
Sheet sheet3 = wb.cloneSheet(0);
wb.setSheetName(2, "Sheet 3");
// Adding Comment to new created Sheet 2
addComments(helper, sheet2);
// Adding Comment to cloned Sheet 3
addComments(helper, sheet3);
}
Aggregations