use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class SettingExternalFunction method main.
public static void main(String[] args) throws IOException {
// or new HSSFWorkbook()
Workbook wb = new XSSFWorkbook();
// register the add-in
wb.addToolPack(new BloombergAddIn());
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(0);
row.createCell(0).setCellFormula("BDP(\"GOOG Equity\",\"CHG_PCT_YTD\")/100");
row.createCell(1).setCellFormula("BDH(\"goog us equity\",\"EBIT\",\"1/1/2005\",\"12/31/2009\",\"per=cy\",\"curr=USD\") ");
row.createCell(2).setCellFormula("BDS(\"goog us equity\",\"top_20_holders_public_filings\") ");
FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx");
wb.write(out);
out.close();
wb.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class SpreadsheetHandler method handleWorkbook.
public void handleWorkbook(Workbook wb) throws IOException {
// try to access some of the content
readContent(wb);
// write out the file
writeToArray(wb);
// access some more content (we had cases where writing corrupts the data in memory)
readContent(wb);
// write once more
ByteArrayOutputStream out = writeToArray(wb);
// read in the written file
Workbook read;
try {
read = WorkbookFactory.create(new ByteArrayInputStream(out.toByteArray()));
} catch (InvalidFormatException e) {
throw new IllegalStateException(e);
}
assertNotNull(read);
readContent(read);
extractEmbedded(read);
modifyContent(read);
read.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class CellUtil method setFont.
/**
* Take a cell, and apply a font to it
*
* @param cell the cell to set the alignment for
* @param font The Font that you want to set.
* @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
*/
public static void setFont(Cell cell, Font font) {
// Check if font belongs to workbook
Workbook wb = cell.getSheet().getWorkbook();
final short fontIndex = font.getIndex();
if (!wb.getFontAt(fontIndex).equals(font)) {
throw new IllegalArgumentException("Font does not belong to this workbook");
}
// Check if cell belongs to workbook
// (checked in setCellStyleProperty)
setCellStyleProperty(cell, FONT, fontIndex);
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class TestXSSFChartAxis method testGetChartAxisBug57362.
public void testGetChartAxisBug57362() {
//Load existing excel with some chart on it having primary and secondary axis.
final Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("57362.xlsx");
final Sheet sh = workbook.getSheetAt(0);
final XSSFSheet xsh = (XSSFSheet) sh;
final XSSFDrawing drawing = xsh.createDrawingPatriarch();
final XSSFChart chart = drawing.getCharts().get(0);
final List<? extends XSSFChartAxis> axisList = chart.getAxis();
assertEquals(4, axisList.size());
assertNotNull(axisList.get(0));
assertNotNull(axisList.get(1));
assertNotNull(axisList.get(2));
assertNotNull(axisList.get(3));
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class TestXSSFWorkbook method testBug56957CloseWorkbook.
@Test
public void testBug56957CloseWorkbook() throws Exception {
File file = TempFile.createTempFile("TestBug56957_", ".xlsx");
final Date dateExp = LocaleUtil.getLocaleCalendar(2014, 10, 9).getTime();
try {
// as the file is written to, we make a copy before actually working on it
FileHelper.copyFile(HSSFTestDataSamples.getSampleFile("56957.xlsx"), file);
assertTrue(file.exists());
// read-only mode works!
Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
Date dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
// now check read/write mode
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
dateAct = workbook.getSheetAt(0).getRow(0).getCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).getDateCellValue();
assertEquals(dateExp, dateAct);
workbook.close();
workbook = null;
} finally {
assertTrue(file.exists());
assertTrue(file.delete());
}
}
Aggregations