ColdFusion: how can I create a table without saving it as a file?

In the ColdFusion app, I need users to be able to save the spreadsheet to their hard drives.

I was able to generate files on the fly by creating them as XML or HTML tables, applying the application/vnd.ms-excelMIME type and saving them with the extension .xls.

<cfheader name="Content-Disposition" value="attachment; filename=MySpreadsheet.xls"> 
<cfcontent type="application/vnd.ms-excel"> 

But when users download such a file, they invariably receive an annoying message (due to the Excel extension extension function ):

The file you are trying to open, "_____. Xls", is in a different format than indicated by the file extension. Make sure the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

I would rather avoid this post.

I understand that tag <cfspreadsheet>and SpreadsheetWrite()will generate a valid Excel file, which does not display this message - but they both require arguments, indicating where on the server to be recorded file. Since the download may contain confidential information, and because I cannot easily provide security after the file is written to the server, I do not want to create such a file - even temporarily. (I know that I can provide a password, but this is not an ideal solution in the context of a business process in which an Excel file will be used after it is created.)

How can I generate an Excel file and a download request without first writing to the file?

+4
source share
1 answer

Qaru , , . , , , .

SpreadsheetReadBinary() variable <cfcontent>.

, ColdFusion, ( <cfheader> <cfcontent> ).

<cfheader name="Content-Disposition" value="attachment; filename=MySpreadsheet.xls"> 
<cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary( objSpreadsheet )#">

- , .cfm Excel:

<!---
Building a spreadsheet that looks like:

+-----+-----+-----+
| FOO | BAR | BAZ |
+-----+-----+-----+
| 101 | 102 | 103 |
+-----+-----+-----+
| 201 | 202 | 203 |
+-----+-----+-----+

--->


<!--- Create a new spreadsheet. --->
<cfset objSpreadsheet = SpreadsheetNew()>

<!--- Create and format the header row. --->
<cfset SpreadsheetAddRow( objSpreadsheet, "FOO,BAR,BAZ" )>
<cfset SpreadsheetFormatRow( objSpreadsheet, {bold=TRUE, alignment="center"}, 1 )>

<!--- Populate the spreadsheet. --->
<!--- In a real situation, this would be looped programmatically; it is done cell-by-cell here for readability. --->
<cfset SpreadsheetSetCellValue( objSpreadsheet, 101, 2, 1, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 102, 2, 2, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 103, 2, 3, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 201, 3, 1, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 202, 3, 2, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 203, 3, 3, "NUMERIC" ) >

<cfheader name="Content-Disposition" value="attachment; filename=MySpreadsheet.xls"> 
<cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary( objSpreadsheet )#"> 
+6

Source: https://habr.com/ru/post/1649292/


All Articles