2011-03-17 32 views
0

我正在使用我的Ruby on Ralis應用程序導出到Excel。我沒有使用任何gem或Plugin(因爲我們的要求是不同的東西,我們不能使用它們)。我們使用Ruby on Rails附帶的內置「format.xls」支持。導出到Excel - 將默認「另存爲類型」更改爲Microsoft Office Excel工作簿(* xls)

問題是,當我們拿到我們的Excel時,Save as Type出現在網頁。我想將其更改爲Microsoft Office Excel Workbook (*xls)。怎麼做?

下面是我的controller action coderespective view我們用excel。

Get_excel方法在控制器

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]><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> 

回答

0

在配置/初始化/ mime_types.rb

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

然後你不應該不需要直接在方法中設置標題。

我不確定這是否會阻止您被提示,但這是我如何爲xls提供HTML表格,並且沒有人遇到過打開它們的問題。

我也沒有設置文件名,因爲方法名會命名它(get_excel.xls在你的情況下)。 我也沒有設置所有的xml和xmlns的東西。它只是工作。

+0

嗨感謝您的回覆。我試過早些時候也這樣做,但它沒有工作!我必須設置文件名,因爲我已經寫在代碼中,而我正在使用的xml模式實際上是控制我的excel的打印設置。我應該怎麼做? – AnkitG 2011-03-18 06:20:09

相關問題