本文整理匯總了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();
}