简体   繁体   中英

How to export html table to excel with correct latitude/longitude format?

I have an HTML table that contains 2 columns of latitudes and longitudes. I can easily export this table to excel using the following JavaScript function:

function downloadExcel() {
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel"> <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

tab_text = tab_text + '<x:Name>Results</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 border='1px'>";
tab_text = tab_text + $('#gcResults_table').html();
tab_text = tab_text + '</table></body></html>';

var data_type = 'data:application/vnd.ms-excel; charset=UTF-8;base64,';

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
    if (window.navigator.msSaveBlob) {
        var blob = new Blob([tab_text], {
            type: "application/csv;charset=utf-8;"
        });
        navigator.msSaveBlob(blob, 'Results.xls');
    }
} else {
    $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text));
    $('#test').attr('download', 'Results.xls');
}
}// END of downloadExcel()

It works perfectly. But when an excel file has exported the table, latitudes and longitudes are not shown in correct format. A sample row of the excel file can be seen below:

在此输入图像描述

Therefore, my question is, how can I correctly write these coordinates to excel? I have searched allot on several blogs but have not been able to find a solution. Thanks allot for your time and support.

UPDATE: Using the above excel export method, whenever an xls file is exported, I get the following notification when I open the file in MS Excel:

在此输入图像描述

After clicking on "Yes", it shows me the excel data but format of latitude/longitude is not correct, as it is mentioned above.

You must specify the data type of the cells as string for xml spreadsheet format. Your table should look like:

<Table border='1px'>
    <Column ss:Width="75" ss:AutoFitWidth="0"/>
    <Row>
      <Cell>
          <Data ss:Type="String">48.210221, 16.390036</Data>
      </Cell>
    </Row>
</Table>

For the mhtml file format a simple table like in the following code, works fine for me:

<html xmlns:x="urn:schemas-microsoft-com:office:excel"> 
<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
  <head>
    <xml>
      <x:ExcelWorkbook>
     <x:ExcelWorksheets>
            <x:ExcelWorksheet>
        <x:Name>Results</x:Name>
        <x:WorksheetOptions>
            <x:Panes></x:Panes>
        </x:WorksheetOptions>
        </x:ExcelWorksheet>
     </x:ExcelWorksheets>
       </x:ExcelWorkbook>
    </xml>
  </head>
  <body>
     <table border='1px'>
        <tr>
           <td >48.210221, 16.390036</td>
        </tr>

     </table>
  </body>
</html>

Be sure that inside the file are indeed those values. Maybe the values are transformed before being saved to the file.

The warning that you get, it's because you are not saving real Excel (xls) file. You have an html file saved with xls extension. MS Excel knows to read HTML files and to display them into a worksheet.

There is a problem on this because excel (mso-number-format) doesn't support that type of number. I would go and format it as text if thats possible in your case.

See this link for avaliable mso-number-formats and this link for a reformatting in excel .

Try to add these css classes

.text {
   mso-number-format: "\@";
}
.number {
   mso-number-format: General;
}

And give these css classes to your data fields.

<div class="text">11</div>
<div class="number">11.0</div>

Just export from JS as CSV-File and format the coordinates with the syntax ="[NUM]". Like this:

Name;Lat;Lng
Test1;="32.056717";="11.335333"
Test2;="12.056717";="19.335333"

In Excel:

在此输入图像描述

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