Question

The code below will generate the file with two notable errors:

  1. The SpreadSheetSetColumnWidth appears to stop working after 21 lines on the second tab
  2. The image does not appear on the second tab


Hopefully this is just a rookie mistake and not something along the lines of the date formatting issue. Any help or constructive criticism to fix/improve would be greatly welcomed.

<!--- We cannot directly reference the image --->
<cfimage source="img/A2LA_Logo.jpg" name="A2LA_Logo">
<!--- We need an absolute path, so get the current directory path.  --->
<cfset theFile=GetDirectoryFromPath(GetCurrentTemplatePath()) & "Final_Report.xlsx">

<cfscript> 
/*********************************************************************** Page 1 ***********************************************************************/
//Create a new Excel spreadsheet object - SpreadsheetNew("Tab name", "yes=2007+ no=2003");
    Tab1 = SpreadsheetNew("Final Report Page 1","yes"); 

    SpreadSheetSetRowHeight(Tab1,1,45);
    SpreadsheetSetCellValue(Tab1,"Final Test Report",1,1); SpreadsheetMergeCells(Tab1,1,1,1,10);
    SpreadsheetAddImage(Tab1, A2LA_Logo,"jpg","1,12,2,20"); 
    SpreadsheetSetCellValue(Tab1,"Confidential",1,21); SpreadsheetMergeCells(Tab1,1,1,21,30);
</cfscript>

<cfscript>  
/*********************************************************************** Page 2 ***********************************************************************/
//Create a new Excel spreadsheet object - SpreadsheetNew("Tab name", "yes=2007+ no=2003");
    Tab2 = SpreadsheetNew("Final Report Page 2","yes"); 

    SpreadSheetSetRowHeight(Tab2,1,45);
    SpreadsheetSetCellValue(Tab2,"Final Test Report",1,1); SpreadsheetMergeCells(Tab2,1,1,1,10);
    SpreadsheetAddImage(Tab1, A2LA_Logo,"jpg","1,12,2,20"); 
    SpreadsheetSetCellValue(Tab2,"Confidential",1,21); SpreadsheetMergeCells(Tab2,1,1,21,30);
</cfscript> 

<!--- There must be a better way --->
<cfscript> 
    for(index=1; index LTE 30; index++) {SpreadSheetSetColumnWidth(Tab1,index,3);}
    for(index=1; index LTE 30; index++) {SpreadSheetSetColumnWidth(Tab2,index,3);}
</cfscript> 

<!--- Write the spreadsheet to a file, replacing any existing file, then append the other tabs.  --->
<cfspreadsheet action="write" filename="#theFile#" name="Tab1" overwrite=true>
<cfspreadsheet action="update" filename="#theFile#" name="Tab2">

<!--- Needs to be at the bottom --->
<cfheader name="Content-disposition" value="attachment;filename=Final_Report.xlsx">
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" file="#theFile#">
<!--- Clean up your mess after it is served to the browser --->
<cffile action="delete" file="#theFile#">
Was it helpful?

Solution

(From the comments)

The images show up when saved separately. So I am guessing it is an update bug. My usual recommendation is "do not use update if you can avoid it". Create a single workbook, add multiple sheets and save it with a single write action.

Update: As for why, in your original code, you are creating two separate workbook objects. Conceptually think of it like creating two separate Excel files, with one sheet each. Then trying to merge them into one file:

     c:/path/to/MyWorkbookNamedTab1.xlsx   

          | Sheet1 / 

     c:/path/to/MyWorkbookNamedTab2.xlsx   

          | Sheet1 / 

That is different than creating a single workbook, with multiple sheets (like in the link above):

     c:/path/to/MySingleWorkbook.xlsx   

          | Sheet1 / Sheet2 /

There is no easy way to merge separate files (or workbooks). You basically have to copy everything from one workbook into another (which is a lot more involved than it sounds). That is what action=update does. Somewhere in the copying process it loses one of the images. I am not sure if it that is due to a CF bug one in POI.

In any case, the simplest solution is to avoid the need for copying altogether. If you stick with a single workbook (and add multiple sheets to it) then there is no need for copying or merging. Everything is already contained in one workbook. So all action=write has to do is save it to disk, exactly "as is".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top