Skip to main content
tims4831424
Inspiring
April 6, 2021
Answered

Create Excel and save it to server

  • April 6, 2021
  • 1 reply
  • 1269 views

Hi all,

i create a xsl file with:

 

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

 

But the browser wat everytime to download the file.

i want to make a shedule cfm site and save the file to the server... 

but how can i say save as to server not open in browser?

 

Kindly regards.

thorsten

 

    This topic has been closed for replies.
    Correct answer BKBK

    Let's say,

    1.  our location is the webroot.

    2.  the name of the page that generates the Excel sheet is generateExcel.cfm. That is,

     

    <!--- generateExcel.cfm --->
    <!--- Send the contents, consisting of Excel binary data, to page output --->
    <cfcontent type="application/vnd.ms-excel" >
    <cfheader name="Content-Disposition" value="inline;filename=fehlmenge2.xls">

     

    Then a possible solution is to create a new CFM page, say, saveExcel.cfm. This page GETs the Excel contents by HTTP and saves it at a location on the server:

     

    <!--- saveExcel.cfm --->
    <!---
    The page generateExcel.cfm generates Excel content. The current page gets the binary content and saves it to a location on the ColdFusion server.
     --->
    <cfhttp url="http://localhost:8500/generateExcel.cfm" method="get" getasbinary="yes" charset="utf-8">
    <cffile action="write" file="C:\ColdFusion2021\cfusion\wwwroot\docs\ExcelDoc.xls" output="#cfhttp.filecontent#">
    Excel file saved to server.

     

     

    1 reply

    George____
    Inspiring
    April 6, 2021

    My guess is that you're generating an HTML table and passing that to the browser with an xls extension.    These aren't excel files and if you look at them in notepad, you'll see it's just HTML.   If you want a true xls or xlsx file use spreadsheetnew.   xlsx files will be smaller in size.   You can also use cfspreadsheet if you want xls.

     

    Lots of examples online, but this is one I found quickly:

    https://blog.mattclemente.com/2019/10/11/til-generating-xlsx-files-with-coldfusion.html

     

    If you really want to save an HTML content then just surround the table with cffile.   Make sure you specify a full path for the filename or it might not save where you expect it to.

    <cffile action="write" file="#filename#">

        your html table

    </cffile>

    tims4831424
    Inspiring
    April 6, 2021

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

     

    Are genrating an excel... if i show the page the excel file will automatically download.. and i can open it normal in excel.

    but i want to genrate on a sheduler the excel files . in couse of that i donwt want to download the file by load the page.. 

     

    i know cfspreadsheet. But the simple way to genrate an excel by a table wth cfcontent and cfheader is simpler...

     

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    April 11, 2021

    Let's say,

    1.  our location is the webroot.

    2.  the name of the page that generates the Excel sheet is generateExcel.cfm. That is,

     

    <!--- generateExcel.cfm --->
    <!--- Send the contents, consisting of Excel binary data, to page output --->
    <cfcontent type="application/vnd.ms-excel" >
    <cfheader name="Content-Disposition" value="inline;filename=fehlmenge2.xls">

     

    Then a possible solution is to create a new CFM page, say, saveExcel.cfm. This page GETs the Excel contents by HTTP and saves it at a location on the server:

     

    <!--- saveExcel.cfm --->
    <!---
    The page generateExcel.cfm generates Excel content. The current page gets the binary content and saves it to a location on the ColdFusion server.
     --->
    <cfhttp url="http://localhost:8500/generateExcel.cfm" method="get" getasbinary="yes" charset="utf-8">
    <cffile action="write" file="C:\ColdFusion2021\cfusion\wwwroot\docs\ExcelDoc.xls" output="#cfhttp.filecontent#">
    Excel file saved to server.