Skip to main content
April 9, 2010
Question

how to export data to an excel by preserving styles

  • April 9, 2010
  • 1 reply
  • 2847 views

Hi All,

I am trying to export data to an excel using ColdFusion. Below is my code.

<cfheader name="content-disposition" value="attachment; filename=dashboard.xls">

<cfcontent type="application/msexcel">

<table width="200" border="1">

  <tr>

    <td width="40"> </td>

    <td width="160">

      <table width="160" border="1">

      <tr>

        <td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td><td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td>

        <td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td><td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td>

      </tr>

      </table>

    </td>

  </tr>

  <tr>

    <td width="40"> </td>

   <td width="160">

      <table width="160" border="1">

      <tr>

        <td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td><td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td>

        <td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td><td width="20" bgcolor="red"> </td><td width="20" bgcolor="black"> </td>

      </tr>

      </table>

    </td>

  </tr>

</table>

When I try to execute above code by commenting <cfheader><cfcontent> then the output is perfect. styles are preserved. And when I uncomment <cfheader><cfcontent> and execute code, then an excel file will get open. But all the styles are lost. So how to preserve all styles(table width etc.) in excel.

Can anybody please assist me on this. Thanks in advance.

Regards,

Manoz.

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    April 9, 2010

    I copied your code and did the test. Result: no problem.

    However, I used the following table data in my test. Did you use any data in yours? To fully test an HTML table, you have to put in some data.

    <!--- <cfheader name="content-disposition" value="attachment; filename=dashboard.xls">

    <cfcontent type="application/vnd.ms-excel"> --->
    <table width="200" border="1">
      <tr>
        <td width="40">xxx</td>
        <td width="160">
          <table width="160" border="1">
          <tr>
            <td width="20" bgcolor="red">AAA</td><td width="20" bgcolor="black">ccc</td><td width="20" bgcolor="red">eee</td><td width="20" bgcolor="black">ggg</td>
            <td width="20" bgcolor="red">&n b s p;</td><td width="20" bgcolor="black">ddd</td><td width="20" bgcolor="red">fff</td><td width="20" bgcolor="black">hhh</td>
          </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td width="40">yyy</td>
       <td width="160">
          <table width="160" border="1">
          <tr>
            <td width="20" bgcolor="red">111</td><td width="20" bgcolor="black">333</td><td width="20" bgcolor="red">555</td><td width="20" bgcolor="black">&n b s p;</td>
            <td width="20" bgcolor="red">222</td><td width="20" bgcolor="black">444</td><td width="20" bgcolor="red">666</td><td width="20" bgcolor="black">888</td>
          </tr>
          </table>
        </td>
      </tr>
    </table>

    Two other things to note. First, I have used &n b s p; for empty table-data. Secondly, though the MIME type you have used for excel might work, the correct  value is application/vnd.ms-excel.

    Inspiring
    April 9, 2010

    My version of excel is 2007.  I copied BKBK's code, uncommented the first two tags, and ran it.  As expected it worked, but not very well.

    Basically, Office 2007 does not play nicely with things that look like office files but are not.  When you attempt to open the files you get dialog boxes you would not want your users to see.

    If you are running cf9, the cfspreadsheet tag is available to solve this problem.  Otherwise, find Ben Nadel's POI cfc and use it.  It produces real excel files which open properly, and also allow you to add some formatting using css.

    BKBK
    Community Expert
    Community Expert
    April 9, 2010

    Dan Bracuk wrote:

    My version of excel is 2007.  I copied BKBK's code, uncommented the first two tags, and ran it.  As expected it worked, but not very well.

    Thanks, Dan. It worked on my version, 2003.

    You have a good point. Of course, there's really no such thing as Excel; there's Excel, version so-and-so.