Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
115 views
in Technique[技术] by (71.8m points)

javascript - Export HTML Table to Excel- Doesn't Open in Office 2010

I am using the following Javascript code to generate an excel on the fly, which converts HTML table to a spreadsheet.

The excel file does not open in Office 2010, shows blank. The same spreadsheet opens with Openoffice. What can be the issue? Is this something related to encoding

function ExcelReport() {
              var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
              tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';
              tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';
              tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
              tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';      
              tab_text = tab_text + "<table>";
              var headingTable = $('#h_tbl').clone();
              tab_text = tab_text + headingTable.html();
              tab_text = tab_text + '</table>';
              $('.c_tbl').each(function( index ) {
                    tab_text = tab_text + "<table>";
                    tab_text = tab_text + "<tr><td></td></tr><tr><td></td></tr>";
                    tab_text = tab_text + '</table>';
                    tab_text = tab_text + "<table>";
                    var exportTable = $(this).clone();
                    tab_text = tab_text + exportTable.html();
                    tab_text = tab_text + '</table>';
              });
              tab_text = tab_text + '</body></html>';
              var fileName = name + '.xls';
              var blob = new Blob([tab_text], { type: "application/vnd.ms-excel;charset=utf-8" })
              window.saveAs(blob, wo_var + ".xls");
            }

When I open the excel in notepad the html code looks like the following

<html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>
        <tbody><tr><td>abc:</td></tr></tbody></table></body></html>

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We had the same issue too many complaints from our customers. We traced it to the Excel OFffice Security patch KB3115262 - https://support.microsoft.com/en-us/kb/3115262 Which came out July 12, 2016

To work around the issue, we had customers make a change in their Excel to allow files from the internet.

To fix: 1) Open Excel Go to File Options

2) Click Trust Center -> Trust Center Settings

3) Go to Protected View. there are 3 options that show that were all clicked

We uncheck the first option that reads -- "Enable Protected View for files originating from the Internet"

That fixed the issue. Perhaps not the best solution. I'm not sure why this particular KB broke this but I think perhaps the fact the file format is not expected and this setting are conflicting with each other.

On a computer I have that doesn't have this KB installed, all those are checked and it still works fine (not blank but prompts file format is different) and I think shows in protected view.

What led us to fact its not the content of the file is we noticed if we resave the file in notepad or notepad++ without making any changes, the file behaves fine so excel must be reading some property of the file rather than the content to block it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...