文本使用ColdFusion在Excel列中打包或自动调整宽度

我正在从数据库中提取数据并将其显示在Excel中供客户使用。 唯一的问题是一些数据对于列来说太大了,而且由于数据是dynamic的(当然),我基本上需要能够做一些文本换行或者扩展列的宽度。

我的代码张贴在下面

<!---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 , 因为张贴在这里 ,虽然我知道它只会文本换行1单元格。 所有完成的是我的Excel页面显示HTTP 500页面。

我也尝试了SpreadsheetFormatColumn方法, 在这里发布 ,并得到了很多错误。

任何帮助都会很棒,如果还有其他类似的post,请点这个方向。 总有可能我错过了它。

使用ColdFusion 9,对吗? 这是令人沮丧的,因为ColdFusion 9文档指出,它的工作,但它是可怕的破碎。 我认为这可能已经在最近的ColdFusion 11中得到了修复,但是它仍然使用了多个版本的Apache POI库。

尝试使用spreadsheetWrite()。 我发现使用标签与脚本进行保存时存在差异:

http://gamesover2600.tumblr.com/post/65341875453/differences-saving-coldfusion-excel-data-w-tag-vs

注意:如果这种types的testing可以使用CFLive.net或TryCF.com来完成,但是由于安全原因它们不允许文件生成。

更新:

<cfset sheet1.FormatColumn(6, {textwrap="true"})/>

不知道更多,我的猜测是这条线导致一个错误。 variables#sheet1#是POI类的一个实例: org.apache.poi.ss.usermodel.Sheet 。 该类不包含名为FormatColumn(..)的方法,所以肯定会导致某种错误。 但是,再次,如果没有实际的错误信息,甚至是屏幕截图,都很难排除错误。

(如果由于某些原因您无法访问必要的工具,我会build议在本地安装CF Developer版本(免费),然后您将拥有对CFpipe理员,日志等的完全访问权限,并且可以configurationdebugging设置喜欢。)

这就是说,我不知道为什么你首先使用所有的低级java代码。 通常情况下,如果您正在寻找一些CF不提供的额外function,或者可能需要解决一个错误,则只需要进入java。 坦率地说,我还没有说服任何一个人在这里适用… 你有没有尝试下面的例子“原样”? 它在9.0.1中运行正常。 因人而异


我试图使用SpreadsheetFormatCell …所有完成的是我的Excel页面显示HTTP 500页面。

你能发布你尝试的实际代码和日志(和你的完整版本)的错误信息吗? 在CF9中肯定有一些电子表格function的错误,但是快速testing“textwrap”在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)#">