我从数据库中提取数据并在Excel中为客户提供数据。唯一的问题是一些数据对于列太大了,并且由于数据是动态的(当然)我基本上需要能够进行一些文本包装或者将列的宽度扩展为必要的。
我的代码发布在
下面<!---Used for streaming to browser--->
<cfset context = getPageContext()>
<cfset context.setFlushOutput(false)>
<cfset response = context.getResponse().getResponse()>
<cfset out = response.getOutputStream()>
<cfset response.setContentType("application/vnd.ms-excel")>
<cfset response.setHeader("Content-Disposition", "attachment; filename=ERD_Report.xls" )>
<!---Create Workbook and Sheets in workbook--->
<cfset wb = createObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").init()/>
<cfset sheet1 = wb.createSheet("ERD Report")/>
<!---Formatting--->
<cfset cellStyleStatic = createObject("java","org.apache.poi.hssf.usermodel.HSSFCellStyle")/>
<cfset cellHSSFFont = createObject("java","org.apache.poi.hssf.usermodel.HSSFFont")/>
<!---Title--->
<cfset fontTitle = wb.createFont()/>
<cfset fontTitle.setFontName("Calibri") />
<cfset fontTitle.setFontHeightInPoints(javacast("int",11))/>
<cfset styleTitle = wb.createCellStyle()/>
<cfset styleTitle.setfont(fontTitle)/>
<cfset styleTitle.setFillPattern(styleTitle.SOLID_FOREGROUND)/>
<cfset styleTitle.setFillForegroundColor(createObject("java","org.apache.poi.hssf.util.HSSFColor$GREY_25_PERCENT").getIndex())/>
<cfset styleTitle.setBorderBottom(styleTitle.BORDER_THIN)/>
<cfset styleTitle.setBorderLeft(styleTitle.BORDER_THIN)/>
<cfset styleTitle.setBorderRight(styleTitle.BORDER_THIN)/>
<cfset styleTitle.setBorderTop(styleTitle.BORDER_THIN)/>
<!---Regular centered white cell with Borders--->
<cfset styleCenter = wb.createCellStyle()/>
<cfset styleCenter.setAlignment(cellStyleStatic.ALIGN_CENTER)/>
<!---Dates--->
<cfset styleDate = wb.createCellStyle()/>
<cfset styleDate.setDataFormat(createObject("java","org.apache.poi.hssf.usermodel.HSSFDataFormat").getBuiltinFormat("m/d/yy"))/>
<!--- Text Wrapping--->
<cfset sheet1.FormatColumn(6, {textwrap="true"})/>
<!---Sheet 1 (Numbers)--->
<!---Rows and columns--->
<cfset row = sheet1.createRow(0)/>
<cfset sheet1.setColumnWidth(0,6000)/>
<cfset sheet1.setColumnWidth(1,6000)/>
<cfset sheet1.setColumnWidth(2,6000)/>
<cfset sheet1.setColumnWidth(3,6000)/>
<cfset sheet1.setColumnWidth(4,6000)/>
<cfset sheet1.setColumnWidth(5,6000)/>
<cfset sheet1.setColumnWidth(6,6000)/>
<cfset sheet1.setColumnWidth(7,6000)/>
<cfset sheet1.setColumnWidth(8,6000)/>
<cfset sheet1.setColumnWidth(9,6000)/>
<cfset sheet1.setColumnWidth(10,6000)/>
<cfset sheet1.setColumnWidth(11,6000)/>
<cfset sheet1.setColumnWidth(12,6000)/>
<cfset sheet1.setColumnWidth(13,6000)/>
<cfset sheet1.setColumnWidth(14,6000)/>
<cfset sheet1.setColumnWidth(15,6000)/>
<cfset sheet1.setColumnWidth(16,6000)/>
<cfset sheet1.setColumnWidth(17,6000)/>
<cfset sheet1.setColumnWidth(18,6000)/>
<cfset cell = row.createCell(0)/>
<cfset cell.setCellValue('Header1')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(1)/>
<cfset cell.setCellValue('Header2')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(2)/>
<cfset cell.setCellValue('Header3')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(3)/>
<cfset cell.setCellValue('Header4')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(4)/>
<cfset cell.setCellValue('Header5')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(5)/>
<cfset cell.setCellValue('Header6')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(6)/>
<cfset cell.setCellValue('Header7')/>
<cfset cell.setCellStyle(styleTitle)/>
<!---<cfset cell = row.createCell(7)/>
<cfset cell.setCellValue('Header8')/>
<cfset cell.setCellStyle(styleTitle)/>--->
<cfset cell = row.createCell(7)/>
<cfset cell.setCellValue('Header9')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(8)/>
<cfset cell.setCellValue('Header10')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(9)/>
<cfset cell.setCellValue('Header11')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(10)/>
<cfset cell.setCellValue('Header12')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(11)/>
<cfset cell.setCellValue('Header13')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(12)/>
<cfset cell.setCellValue('Header14')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(13)/>
<cfset cell.setCellValue('Header15')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(14)/>
<cfset cell.setCellValue('Header16')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(15)/>
<cfset cell.setCellValue('Header17')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(16)/>
<cfset cell.setCellValue('Header18)/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(17)/>
<cfset cell.setCellValue('Header19')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfset cell = row.createCell(18)/>
<cfset cell.setCellValue('Header20')/>
<cfset cell.setCellStyle(styleTitle)/>
<cfloop query="getall">
<!--- Query Engineer Notes for this Tracking Number --->
<cfquery name="Eng_Notes" datasource="#request.dsn#">
SELECT Eng_Notes AS Note
FROM tbl_ERD_Eng_NOTES
WHERE ERD_ID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#getAll.ID#" />
</cfquery>
<cfset variables.lstNotes = valueList(Eng_Notes.Note, " *** ") />
<cfset row = sheet1.createRow(javacast("int",#getall.currentrow#))/>
<cfset cell = row.createCell(0)/>
<cfset cell.setCellValue('#getall.info1#')/>
<cfset cell = row.createCell(1)/>
<cfset cell.setCellValue('#getall.info2#')/>
<cfset cell = row.createCell(2)/>
<cfset cell.setCellValue('#getall.info3#')/>
<cfset cell = row.createCell(3)/>
<cfset cell.setCellValue('#getall.info4#')/>
<cfset cell = row.createCell(4)/>
<cfset cell.setCellValue('#getall.info5#')/>
<cfset cell = row.createCell(5)/>
<cfset cell.setCellValue('#getall.info6#')/>
<cfset cell = row.createCell(6)/>
<cfset cell.setCellValue('#variables.info7#')/>
<!---<cfset cell = row.createCell(7)/>
<cfset cell.setCellValue('#getall.info8#')/>--->
<cfset cell = row.createCell(7)/>
<cfset cell.setCellValue('#getall.info9#')/>
<cfset cell = row.createCell(8)/>
<cfset cell.setCellValue('#getall.info10#')/>
<cfset cell = row.createCell(9)/>
<cfset cell.setCellValue('#getall.info11#')/>
<cfset cell = row.createCell(10)/>
<cfset cell.setCellValue('#getall.info12#')/>
<cfset cell = row.createCell(11)/>
<cfset cell.setCellValue('#getall.info13#')/>
<cfset cell = row.createCell(12)/>
<cfset cell.setCellValue('#getall.info14#')/>
<cfset cell = row.createCell(13)/>
<cfset cell.setCellValue('#getall.info13#')/>
<cfset cell = row.createCell(14)/>
<cfset cell.setCellValue('#getall.info14#')/>
<cfset cell = row.createCell(15)/>
<cfset cell.setCellValue('#getall.info15#')/>
<cfset cell = row.createCell(16)/>
<cfset cell.setCellValue('#getall.info16#')/>
<cfset cell = row.createCell(17)/>
<cfset cell.setCellValue('#getall.info17#')/>
<cfset cell = row.createCell(18)/>
<cfset cell.setCellValue('#getall.info18#')/>
</cfloop>
我尝试使用SpreadsheetFormatCell
,as posted here,虽然我知道它只会文本换行1个单元格。完成的只是我的Excel页面显示HTTP 500页面。
我还尝试了SpreadsheetFormatColumn
方法,as posted here,并且遇到了很多错误。
任何帮助都会很棒,如果还有其他类似的帖子,请指出我的方向。我总是有可能错过它。
答案 0 :(得分:2)
使用ColdFusion 9,对吧?令人沮丧的是因为ColdFusion 9文档声明它可以工作,但它可怕地被打破了。我认为这可能已在最近的ColdFusion 11中得到修复,但它仍然使用多个旧版本的Apache POI库。
尝试使用spreadsheetWrite()。我发现使用标记与脚本保存时存在差异:
http://gamesover2600.tumblr.com/post/65341875453/differences-saving-coldfusion-excel-data-w-tag-vs
注意:如果使用CFLive.net或TryCF.com进行此类测试会很好,但出于安全原因,它们不允许生成文件。
答案 1 :(得分:0)
<强>更新强>
<cfset sheet1.FormatColumn(6, {textwrap="true"})/>
不知道更多,我的猜测是该行会导致错误。变量#sheet1#
是POI类的实例:org.apache.poi.ss.usermodel.Sheet
。该类不包含名为FormatColumn(..)
的方法,因此肯定会导致某种错误。但同样,在没有实际错误消息甚至是屏幕截图的情况下,很难对错误进行故障排除。
(如果您由于某种原因无法访问必要的工具,我建议您在本地(免费)安装CF Developer版本。然后您将可以完全访问CF管理员,日志等,并可以配置调试但是你喜欢的设置。)
那就是说,我不知道为什么你首先使用所有低级别的java代码。通常,如果您正在寻找CF未提供的一些额外功能,或者可能解决错误,那么您只需要需要来使用。坦率地说,我还不相信其中任何一个适用于此...... 你有没有试过下面的例子&#34;原样&#34;?它在9.0.1下运行良好。 YMMV
我试过使用SpreadsheetFormatCell ......所有这一切 已完成,我的Excel页面显示HTTP 500页面。
您可以发布您尝试的实际代码和日志(以及完整版)中的错误消息吗?在CF9中肯定存在一些电子表格函数的错误,但是快速测试了#text; textwrap&#34; CF9.0.1为我工作得很好。
<cfset sheet = spreadSheetNew()>
<!--- enabled wrapping for single cell --->
<cfset SpreadsheetFormatCell(sheet, {textwrap="true"}, 2, 1)>
<!---
enable wrapping for entire column
<cfset SpreadsheetFormatColumn(sheet, {textwrap="true"}, 2)>
--->
<!--- add text --->
<cfset SpreadSheetSetCellValue(sheet, "Short text", 1, 1)>
<cfset SpreadSheetSetCellValue(sheet, RepeatString("aaa ", 50), 2, 1)>
<cfset SpreadSheetSetCellValue(sheet, "More text", 3, 1)>
<cfset SpreadSheetSetCellValue(sheet, "Short text", 1, 2)>
<cfset SpreadSheetSetCellValue(sheet, "More text", 2, 2)>
<!--- resize column --->
<cfset SpreadSheetSetColumnWidth(sheet, 1, 35)>
<!--- display results --->
<cfheader name="Content-Disposition" value="attachment; filename=test.xls">
<cfcontent type="application/vnd.ms-excel"
variable="#SpreadSheetReadBinary(sheet)#">