导出到 Excel - 更改默认的“保存类型”到 Microsoft Office Excel 工作簿 (*xls)
我正在使用 Ruby on Ralis 应用程序导出到 Excel。我没有使用任何 gem 或插件(因为我们的要求不同,所以我们无法使用它们)。我们使用了 Ruby on Rails 附带的内置“format.xls”支持。
问题是,当我们获取 Excel 时,另存为类型
出现在网页上。我想将其更改为 Microsoft Office Excel Workbook (*xls)
。怎么做呢?
下面是我的控制器操作代码
和我们获取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>
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]><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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 config/initializers/mime_types.rb 中
,那么您不需要直接在方法中设置标头。
我不确定这是否会阻止您收到提示,但这就是我将 HTML 表作为 xls 提供的方式,并且没有人在打开它们时遇到问题。
我也没有设置文件名,因为方法名称将命名它(在您的情况下为 get_excel.xls )。
我也没有设置所有 xml 和 xmlns 内容。它就是有效的。
In config/initializers/mime_types.rb
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.