将html表格导出为word文件并更改文件方向
export html table as word file and change file orientation
我有jquery功能,可以将html表导出到word文件。函数工作得很好,但我需要将一个word文件旋转到landsacpe方向。有人能帮我吗?
这里是js函数:
<SCRIPT type="text/javascript">
$(document).ready(function () {
$("#btnExport").click(function () {
var htmltable= document.getElementById('tblExport');
var html = htmltable.outerHTML;
window.open('data:application/msword,' + ''uFEFF' + encodeURIComponent(html));
});
});
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.docx");
将HTML导出到Microsoft Word
您可以通过在导出的HTML中包含CSS来设置页面方向、纸张大小和许多其他属性。有关可用样式的列表,请参阅带有MS Office前缀的样式属性有些样式具有相关性。例如,要设置mso页面方向,还必须设置页面的大小,如下代码所示。
更新时间:
使用Word 2010-2013在FireFox、Chrome、Opera、IE10-11中进行测试。对代码进行小的更改,使其能够使用Chrome和IE10。不适用于缺少窗口的旧版浏览器(IE<10)。Blob对象。如果您收到";createObjectURL不是函数";错误:https://stackoverflow.com/a/10195751/943435
2022年更新:
修复了损坏的GitHub链接
@page WordSection1{
mso-page-orientation: landscape;
size: 841.95pt 595.35pt; /* EU A4 */
/* size:11.0in 8.5in; */ /* US Letter */
}
div.WordSection1 {
page: WordSection1;
}
要查看完整的工作演示,请参阅:https://jsfiddle.net/78xa14vz/3/
用于导出到Word:的Javascript
function export2Word( element ) {
var html, link, blob, url, css;
css = (
'<style>' +
'@page WordSection1{size: 841.95pt 595.35pt;mso-page-orientation: landscape;}' +
'div.WordSection1 {page: WordSection1;}' +
'</style>'
);
html = element.innerHTML;
blob = new Blob([''ufeff', css + html], {
type: 'application/msword'
});
url = URL.createObjectURL(blob);
link = document.createElement('A');
link.href = url;
link.download = 'Document'; // default name without extension
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob ) navigator.msSaveOrOpenBlob( blob, 'Document.doc'); // IE10-11
else link.click(); // other browsers
document.body.removeChild(link);
};
以及HTML:
<button onclick="export2Word(window.docx)">Export</button>
<div id="docx">
<div class="WordSection1">
<!-- The html you want to export goes here -->
</div>
</div>
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- ng映射方向备选方案
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在php文件中获取$.post-ajax传递的值
- 直接下载文件,而不是从window.open(url)
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 使用压缩的JavaScript文件(不是运行时压缩)
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 将html表格导出为word文件并更改文件方向
- 有人能为我指明正确的方向,让一个javascript文件在另一个单独的javascript文件之后运行吗
- 如何在使用角度文件上传之前修复EXIF方向
- 谷歌方向Api JavaScript链接Api密钥到php文件