简体   繁体   中英

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>

在此输入图像描述

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.

You are saving a file with HTML content as *.xls . Excel can open this but will normaly show a warning message:

The file you are trying to open, 'name.ext', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

If that warning not occurs and the file is not opening, then maybe the

DWORD Value ExtensionHardening in HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\<version number>\\Excel\\Security is set to 2 in Windows Registry

That means "Check the file name extension and the file type. If they do not match, do not open the file."

See https://support.microsoft.com/en-us/kb/948615 .

Maybe your administrator has set this via Group Policy setting. If you are in an administrated network, ask your administrator.

If you are administrator of your own, then look for that registry setting and change it to 1 , the default.

Instead of doing:

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

Do this:

Go to Properties of the file and click Unblock (If you know the file is safe).

Image: Go to Properties of the file and click Unblock

I had a similar issue, intranet wise. Grid export to HTML Excel all of a sudden stopped working when user clicked the open. Quick workaround, Save and Open drop down option or just save then open.

My fix was to change content-disposition from attached to inline. Now the users get the old popup center page instead of the one at the bottom and open works just fine.

Similar issue, my code dumps from a GridView to an XLS file using StringWriter, HTMLTextWriter and RenderControl.

Since our ASP.NET code runs on a company intranet we uninstalled the following updates across the company instead of going into 100s of user PCs and changing their settings.

Uninstalled:

  • Windows Update KB3115262 (Excel 2013)
  • Windows Update KB3115130 (Excel 2010)
  • Windows Update KB3115272 (Excel 2016/64 bit)

This fixed our issue, and would also fix yours and others affected by these updates.

For those more security paranoid, I saw another answer on SalesForce site, which should work too - https://success.salesforce.com/answers#!/feedtype=SINGLE_QUESTION_DETAIL&dc=All&criteria=BESTANSWERS&id=9063A000000iTIyQAM

Basically marking the site you are downloading from as safe.

I haven't tried it myself

Basically open the website where you will be downloading excel files in Internet Explorer

Go to Settings Internet Options Security Trusted Sites Sites Add

This should add the site in question as allowed and then you can use whatever browser you want eg firefox / chrome etc.

Possible fixes for the problem:

Solution 1:

1) Type Regedit in start menu

2) Navigate to registry as,

In the registry: HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\\\Excel\\Security

where =

 15.0 for Office 2013
 16.0 for Office 2016
 12.0 for Office 2010

create a DWORD(32bit) value and name it: ExtensionHardening. The possible values are:

0: Do not check the file name extension and
   the file type, and bypass the function of the warning message.

1: Check the file name extension and the file type.
   If they do not match, display the warning message.

2: Check the file name extension and the file
   type. If they do not match, do not open the file.

Use 1 as the value.

Solution 2:

Right click on Excel -->Properties-->Unblock-->Ok.

UnblockExcel

SOlution 3

Uninstall the updates:

Windows Update KB3115272 (Excel 2016/64 bit)

Windows Update KB3115262 (Excel 2013)

Windows Update KB3115130 (Excel 2010)

For more info about the issue .please refer the link below. https://social.technet.microsoft.com/Forums/en-US/d2d84793-9920-45e3-baef-5027a4ac1ae0/kb3170008-for-office-2016-breaks-functionality-ms16088?forum=officeitpro

Microsoft patched Excel on Aug 9 to restore the original functionality. Scroll to post on Aug 9 in this thread for more info: https://social.technet.microsoft.com/Forums/en-US/d2d84793-9920-45e3-baef-5027a4ac1ae0/kb3170008-for-office-2016-breaks-functionality-ms16088?forum=officeitpro

Updating Excel fixed the problem for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM