2013-01-11 53 views
3

这是到目前为止我的代码,它从一个查询获取数据然后将其导出到Excel文档:Excel单元格的格式 - XSSF工作簿

<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数据格式“会计”。我已经做了研究,但我很困难。

任何想法?

+1

有你看着给SXXFDataFormat,https://poi.apache.org/apidocs/org/apache /poi/xssf/usermodel/XSSFDataFormat.html这个例子使用HSSF,但应该与SXXFDataFormat相同或非常相似,都使用DataFormat接口http://npoi.codeplex.com/discussions/391336 – Travis

+0

@Travis - 我不知道关于.net端口,但在Java版本中,'Accounting'是内置格式之一,即'44'。 (如果你转储他们,大多数的Excel标准都在那里) – Leigh

+0

我在vba中看到了几个,没有看到它在java ID中。老实说,从来没有用过poi,只是我在一些快速研究中能找到的东西。 – Travis

回答

1

我会做的是在Excel中创建一个文件。将“会计”格式应用于其中一个单元格。然后检查电池的style/dataFormat在POI:

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()); 
+0

我似乎无法查看格式。我如何找出Excel'Number'格式的等价物? – Alias

+0

你上面运行了最后两行代码吗?它转储所有可用的格式。 'Number'是一个包含很多“子”格式的通用类,如6,7,8,... – Leigh

+0

在Java中,您可以使用:for(String s:BuiltinFormats.getAll()){System.out.println (一个或多个);}' – Ron

相关问题