Excel 单元格格式 - XSSF 工作簿

标签 excel coldfusion xssf

这是我到目前为止的代码,它从查询中获取数据,然后将其导出到 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>

我想要做的是将标题为“值”的列格式化为 Excel 中的数据格式“会计”。我已经做了研究,但我很困惑。

有什么想法吗?

最佳答案

我要做的是在 Excel 中创建一个文件。将“会计”格式应用到单元格之一。然后检查单元格的style/dataFormat在兴趣点:

    dataFormatIndex = theAccountingCell.getCellStyle().getDataFormat();

在我的测试中,POI 报告“Accounting”格式为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());

关于Excel 单元格格式 - XSSF 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278198/

相关文章:

C# 如何阻止用户使用程序创建的Excel应用程序

sql-server - 如何以与 SQL Server 中相同的顺序将列从 SQL 导入 Excel?

Excel - 在字符串中查找多个值并返回第一个

vba - 默认范围属性产生意外结果

excel - 如何使用 NPOI XSSF 创建 Excel 表

java - 线程 "main"java.lang.IllegalArgumentException : Sheet index (0) is out of range (0. 中的异常 .-1)

jquery - 不允许列,ajax提交

coldfusion - Ehcache.Cache.put 不适用于特定的 ColdFusion 元素(大约需要 4 秒)

java - 如何开始编写代码覆盖工具?

java - 我可以使用 Writer 类生成 xlsx 文件吗?