开发者

Export To Excel - Change Default "Save as type" to Microsoft Office Excel Workbook(*xls)

开发者 https://www.devze.com 2023-02-18 11:37 出处:网络
I am Doing an Export to Excel with my Ruby on Ralis application.I have not used any gem or Plugin (since our requirement was something different, we couldn\'t use them). We used the inbuilt \"format.x

I am Doing an Export to Excel with my Ruby on Ralis application.I have not used any gem or Plugin (since our requirement was something different, we couldn't use them). We used the inbuilt "format.xls" support that comes with Ruby on Rails.

The problem is that, when we get our Excel, Save as Type appears on the webpage. I want to change this to Microsoft Office Excel Workbook (*xls). How to do it?

Below is my controller action code and the respective view with which we get the excel.

Get_excel methods in controller

def get_excel
    format.xls do
        headers['Content-Type'] = "application/vnd.ms-excel"
        headers["Content-disposition"] = 'inline;  filename="myexcel.xls"'
        headers['Cache-Control'] = ''
    end
end

VIEW (get_excel.xls.erb)

<html xmlns:o="urn:schemas-microsoft-com:office:office
      xmlns:x="urn:schemas-microsoft-com:office:excel"
      xmlns="http://www.w3.org/TR/REC-html40"
      xmlns:ss = "urn:schemas-microsoft-com:office:spreadsheet" />
<head>
  <meta http-equiv=Content-Type content="text/html; charset=UTF-8"/>
  <meta name=ProgId content=Excel.Sheet/>
  <meta name=Generator content="Microsoft Excel 11"/>
  <style type="text/css">
    @page {
      mso-header-data : '&R CONFIDENTIAL';
      margin: 0.75in 0.20in 0.5in 0.20in;
      mso-header-margin: 0.25in;
      mso-footer-margin:0.25in;
      mso-page-orientation: landscape;
    }
    table{
      page-break-inside: avoid;
    }
    td{
      white-space:nowrap;
    }
  </style>

  <!--[if gte mso 9开发者_JAVA百科]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets
   <x:ExcelWorksheet>
    <x:Name>Gantt Detail</x:Name>
    <x:WorksheetOptions>  
    <x:DefaultRowHeight>319</x:DefaultRowHeight>  
     <x:Print>
      <x:FitHeight>15</x:FitHeight>
      <x:ValidPrinterInfo/>
      <x:Scale>74</x:Scale>
      <x:HorizontalResolution>600</x:HorizontalResolution>
      <x:VerticalResolution>600</x:VerticalResolution>
     </x:Print>
     <x:Selected/>
     <x:FrozenNoSplit/>
     <x:SplitHorizontal>5</x:SplitHorizontal>
     <x:TopRowBottomPane>5</x:TopRowBottomPane>
     <x:ActivePane>2</x:ActivePane>
          <x:Panes>
            <x:Pane>
            <x:Number>3</x:Number>
            </x:Pane>
            <x:Pane>
            <x:Number>2</x:Number>
            </x:Pane>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:WindowHeight>8580</x:WindowHeight>
  <x:WindowWidth>12120</x:WindowWidth>
  <x:WindowTopX>120</x:WindowTopX>
  <x:WindowTopY>45</x:WindowTopY>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
 </x:ExcelWorkbook>
 <x:ExcelName>
  <x:Name>Print_Titles</x:Name>
  <x:SheetIndex>1</x:SheetIndex>
  <x:Formula>='Gantt Detail'!$3:$5</x:Formula>
 </x:ExcelName>
</xml><![endif]-->

</head>
<body>
  <table>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
<tr>some code</tr>
  </table>
</body>


In config/initializers/mime_types.rb

Mime::Type.register "application/vnd.ms-excel", :xls

Then you shouldn't need to set headers directly in the method.

I'm not sure if this will stop you from being prompted, but it's how I serve HTML tables as xls and nobody ever has a problem opening them.

I also don't set a filename, as the method name will name it (get_excel.xls in your case). I also don't set all the xml and xmlns stuff either. It just works.

0

精彩评论

暂无评论...
验证码 换一张
取 消