A memorandum on how to create an Excel file using a template file in poi.
Example
File file = new File("C:\\tmp\\templates\\templates.xml");
Workbook workbook = null;
try (InputStream is = new ByteArrayInputStream(
Files.readAllBytes(file.toPath()));) {
workbook = WorkbookFactory.create(is);
} catch (IOException e) {
e.printStackTrace();
}
Example
FileOutputStream fout = null;
try {
fout = new FileOutputStream("C:\\tmp\\");
workbook.write(fout);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
}
}
}
Example
Sheet sheet = workbook.getSheet("Sheet1");
getCell(sheet, 0, 0).setCellValue("test");
private Cell getCell(Sheet sheet, int rowIndex, int colIndex) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
row = sheet.createRow(rowIndex);
}
Cell cell = row.getCell(colIndex);
if (cell == null) {
cell = row.createCell(colIndex);
}
return cell;
}
Example
workbook.removeSheetAt(0);
Example
workbook.setPrintArea(0, 0, 1, 0, 1);
Recommended Posts