use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class WorkingWithPictures method main.
public static void main(String[] args) throws IOException {
//create a new workbook
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
try {
CreationHelper helper = wb.getCreationHelper();
//add a picture in this workbook.
InputStream is = new FileInputStream(args[0]);
byte[] bytes = IOUtils.toByteArray(is);
is.close();
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
//create sheet
Sheet sheet = wb.createSheet();
//create drawing
Drawing<?> drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture
pict.resize(2);
//save workbook
String file = "picture.xls";
if (wb instanceof XSSFWorkbook) {
// NOSONAR
file += "x";
}
OutputStream fileOut = new FileOutputStream(file);
try {
wb.write(fileOut);
} finally {
fileOut.close();
}
} finally {
wb.close();
}
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class UpdateEmbeddedDoc method checkUpdatedDoc.
/**
* Called to test whether or not the embedded workbook was correctly
* updated. This method simply recovers the first cell from the first row
* of the first workbook and tests the value it contains.
* <p/>
* Note that execution will not continue up to the assertion as the
* embedded workbook is now corrupted and causes an IllegalArgumentException
* with the following message
* <p/>
* <em>java.lang.IllegalArgumentException: Your InputStream was neither an
* OLE2 stream, nor an OOXML stream</em>
* <p/>
* to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
* is executed.
*
* @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
* Rather
* than use the specific classes (HSSF/XSSF) to handle the embedded
* workbook this method uses those defeined in the SS stream. As
* a result, it might be the case that a SpreadsheetML file is
* opened for processing, throwing this exception if that file is
* invalid.
* @throws java.io.IOException Thrown if a problem occurs in the underlying
* file system.
*/
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
for (PackagePart pPart : this.doc.getAllEmbedds()) {
String ext = pPart.getPartName().getExtension();
if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
InputStream is = pPart.getInputStream();
Workbook workbook = null;
try {
workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(SHEET_NUM);
Row row = sheet.getRow(ROW_NUM);
Cell cell = row.getCell(CELL_NUM);
assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
} finally {
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(is);
}
}
}
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class TestExcelAntWorkbookUtil method testGetWorkbook.
public void testGetWorkbook() {
fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName);
assertNotNull(fixture);
Workbook workbook = fixture.getWorkbook();
assertNotNull(workbook);
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class TestExcelAntWorkbookUtil method testWorkbookConstructor.
public void testWorkbookConstructor() throws InvalidFormatException, IOException {
File workbookFile = new File(mortgageCalculatorFileName);
FileInputStream fis = new FileInputStream(workbookFile);
Workbook workbook = WorkbookFactory.create(fis);
fixture = new ExcelAntWorkbookUtilTestHelper(workbook);
assertNotNull(fixture);
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class CellStyleDetails method main.
public static void main(String[] args) throws Exception {
if (args.length == 0) {
throw new IllegalArgumentException("Filename must be given");
}
Workbook wb = WorkbookFactory.create(new File(args[0]));
DataFormatter formatter = new DataFormatter();
for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
Sheet sheet = wb.getSheetAt(sn);
System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());
for (Row row : sheet) {
System.out.println(" Row " + row.getRowNum());
for (Cell cell : row) {
CellReference ref = new CellReference(cell);
System.out.print(" " + ref.formatAsString());
System.out.print(" (" + cell.getColumnIndex() + ") ");
CellStyle style = cell.getCellStyle();
System.out.print("Format=" + style.getDataFormatString() + " ");
System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");
Font font = wb.getFontAt(style.getFontIndex());
System.out.print("Font=" + font.getFontName() + " ");
System.out.print("FontColor=");
if (font instanceof HSSFFont) {
System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
}
if (font instanceof XSSFFont) {
System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
}
System.out.println();
System.out.println(" " + formatter.formatCellValue(cell));
}
}
System.out.println();
}
wb.close();
}
Aggregations