到目前为止,这是我的代码,它从查询中获取数据,然后将其导出到Excel文档中:
<cfscript>
oArray = CreateObject("java", "java.util.Arrays");
workbook = CreateObject("java", "org.apache.poi.xssf.usermodel.XSSFWorkbook");
workbook.init();
myFont = workbook.createFont();
myFont.setBoldweight(myFont.BOLDWEIGHT_BOLD);
boldStyle = workbook.createCellStyle();
boldStyle.setFont(myFont);
</cfscript>
<!--- Set up the headings for the Excel worksheet --->
<cfscript>
thisSheet = workbook.createSheet(JavaCast("string", 'invoices due'));
rows = {};
// we need to refer to these three rows later on
rows[1] = thisSheet.createRow(0);
rows[2] = thisSheet.createRow(1);
rows[3] = thisSheet.createRow(2);
rows[4] = thisSheet.createRow(3);
//Report parameters explanation
thisCell = rows[2].createCell(0, 1);
thisCell.setCellValue(reportSum);
// user column headings
thisCell = rows[4].createCell(0, 1);
thisCell.setCellValue('Value');
thisCell.setCellStyle(boldStyle);
thisCell = rows[4].createCell(1, 1);
thisCell.setCellValue('Team');
thisCell.setCellStyle(boldStyle);
thisCell = rows[4].createCell(2, 1);
thisCell.setCellValue('Manager');
thisCell.setCellStyle(boldStyle);
</cfscript>
<cfset row = 5>
<cfloop query="invoicesDue">
<cfscript>
thisRow = thisSheet.createRow(JavaCast("int", row));
thisCell = thisRow.createCell(0, 1);
thisCell.setCellValue(HTMLEditFormat(invoicesDue.value));
thisCell = thisRow.createCell(1, 1);
thisCell.setCellValue(HTMLEditFormat(invoicesDue.ct_team));
thisCell = thisRow.createCell(2, 1);
thisCell.setCellValue(HTMLEditFormat(invoicesDue.manager));
thisCell = thisRow.createCell(3, 1);
</cfscript>
</cfloop>
<cfscript>
// todo: change to datadir from getAppRoot
outputFileName = "invoicesDue(withfundingsource)" & "_" & RandRange(00000,99999) & ".xlsx";
fos = CreateObject("java", "java.io.FileOutputStream");
fos.init(outputFilename);
workbook.write(fos);
fos.close();
</cfscript>
我要做的是将名为“Value”的列格式化为Excel中的“Accounting”数据格式。我做过研究,但我很困难。
有什么想法吗?
答案 0 :(得分:2)
我要做的是在Excel中创建一个文件。将“Accounting”格式应用于其中一个单元格。然后检查POI中的单元格style/dataFormat:
dataFormatIndex = theAccountingCell.getCellStyle().getDataFormat();
在我的测试中,POI报告“会计”格式是BuiltInFormat 44
。你可以像这样申请:
accountingStyle = workbook.createCellStyle();
accountingStyle.setDataFormat( javacast("int", 44) ) ;
someRow = someSheet.createRow(0);
someCell = someRow.createCell(0);
someCell.setCellStyle( accountingStyle );
someCell.setCellValue( javacast("double", 123.75) );
要查看所有BuiltInFormat值:
formats = createObject("java", "org.apache.poi.ss.usermodel.BuiltinFormats");
writeDump(formats.getAll());