HTML table 转化成 Excel电子表格? 请高手帮忙 :(

苏丹-陈 2002-07-24 11:05:01
怎样才能让IE自动打开Excel,加载内容。
或者怎么才能直接保存数据到本地硬盘,提示用户输入文件名?

function tableToExcel(o,table){
o.document.write("<html xmlns:o='urn:schemas-microsoft-com:office:office'");
o.document.write("xmlns:x='urn:schemas-microsoft-com:office:excel'");
o.document.write("xmlns='http://www.w3.org/TR/REC-html40'>");
o.document.write("<head>");
o.document.write("<meta http-equiv=Content-Type content='text/html; charset=GB2312'>");
o.document.write("<meta name=ProgId content=Excel.Sheet>");
o.document.write("<meta name=Generator content='Microsoft Excel 9'>");
o.document.write("<xml>");
o.document.write(" <x:ExcelWorkbook>");
o.document.write(" <x:ExcelWorksheets>");
o.document.write(" <x:ExcelWorksheet>");
o.document.write(" <x:Name>导出表格数据</x:Name>");
o.document.write(" <x:WorksheetOptions>");
o.document.write(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
o.document.write(" <x:Print>");
o.document.write(" <x:ValidPrinterInfo/>");
o.document.write(" <x:PaperSizeIndex>9</x:PaperSizeIndex>");
o.document.write(" <x:HorizontalResolution>600</x:HorizontalResolution>");
o.document.write(" <x:VerticalResolution>600</x:VerticalResolution>");
o.document.write(" </x:Print>");
o.document.write(" <x:Selected/>");
o.document.write(" <x:ProtectContents>False</x:ProtectContents>");
o.document.write(" <x:ProtectObjects>False</x:ProtectObjects>");
o.document.write(" <x:ProtectScenarios>False</x:ProtectScenarios>");
o.document.write(" </x:WorksheetOptions>");
o.document.write(" </x:ExcelWorksheet>");
o.document.write(" </x:ExcelWorksheets>");
o.document.write(" <x:WindowHeight>7965</x:WindowHeight>");
o.document.write(" <x:WindowWidth>13905</x:WindowWidth>");
o.document.write(" <x:WindowTopX>360</x:WindowTopX>");
o.document.write(" <x:WindowTopY>1830</x:WindowTopY>");
o.document.write(" <x:ProtectStructure>False</x:ProtectStructure>");
o.document.write(" <x:ProtectWindows>False</x:ProtectWindows>");
o.document.write(" </x:ExcelWorkbook>");
o.document.write("</xml>");
o.document.write("</head>");

o.document.write("<body>");
o.document.write(table);
o.document.write("</body>");

o.document.write("</html>");
}

function exportToExcel(){
var win;
win = window.open("")
tableToExcel(win,printContent.outerHTML)
}
...全文
116 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
X东海 2002-08-18
  • 打赏
  • 举报
回复
gz
苏丹-陈 2002-07-24
  • 打赏
  • 举报
回复
想这样做。生成一个模板xls文件下载后,自动打开,IE执行
textRange = document.body.createTextRange();
textRange.moveToElementText(printContent);
textRange.execCommand("Copy");
然后到打开Excel的IE执行Paste功能。
但是需要人为控制。
苏丹-陈 2002-07-24
  • 打赏
  • 举报
回复
直接Open服务器上的一个xls文件,内容如上就可以用Excel打开
但是我服务将table的内容插入到下载的文件中去。
还有一法,就是
textRange = document.body.createTextRange();
textRange.moveToElementText(printContent);
textRange.execCommand("Copy");
复制table的内容,由用户去执行paste,但是不知道是不是可以用程序
自动完成。

87,903

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧