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