本文整理汇总了Java中org.apache.poi.xssf.usermodel.XSSFDataFormat类的典型用法代码示例。如果您正苦于以下问题:Java XSSFDataFormat类的具体用法?Java XSSFDataFormat怎么用?Java XSSFDataFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
XSSFDataFormat类属于org.apache.poi.xssf.usermodel包,在下文中一共展示了XSSFDataFormat类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: render
import org.apache.poi.xssf.usermodel.XSSFDataFormat; //导入依赖的package包/类
/**
* Render the specified table
*
* @param table the table view
* @param formatted
* true if a formatting must be applied
* @return a byte array (Excel file)
*/
private static byte[] render(Table<?> table, boolean formatted) {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("export");
// Default date format
XSSFCellStyle dateCellStyle = wb.createCellStyle();
XSSFDataFormat df = wb.createDataFormat();
dateCellStyle.setDataFormat(df.getFormat(DEFAULT_EXCEL_DATE_FORMAT));
// Write the header
Row headerRow = sheet.createRow(0);
int columnIndex = 0;
CellStyle headerCellStyle = wb.createCellStyle();
Font f = wb.createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
headerCellStyle.setFont(f);
for (ColumnDef header : table.getHeaders()) {
Cell cell = headerRow.createCell(columnIndex);
cell.setCellValue(Msg.get(header.getLabel()));
cell.setCellStyle(headerCellStyle);
columnIndex++;
}
// Write the cells
if (formatted) {
writeFormattedRows(table, sheet, dateCellStyle);
} else {
writeNotFormattedRows(table, sheet, dateCellStyle);
}
ByteArrayOutputStream outBuffer;
try {
outBuffer = new ByteArrayOutputStream();
wb.write(outBuffer);
outBuffer.close();
} catch (Exception e) {
log.error("Error while generating an Excel file from a Table", e);
throw new RuntimeException("Error while generating an Excel file from a Table", e);
}
return outBuffer.toByteArray();
}