简体   繁体   English

如何使用正确的纬度/经度格式将html表导出为ex​​cel?

[英]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. 我有一个HTML表,包含2列纬度和经度。 I can easily export this table to excel using the following JavaScript function: 我可以使用以下JavaScript函数轻松地将此表导出到excel:

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. 但是当excel文件导出表格时,纬度和经度不会以正确的格式显示。 A sample row of the excel file can be seen below: excel文件的示例行如下所示:

在此输入图像描述

Therefore, my question is, how can I correctly write these coordinates to excel? 因此,我的问题是,我怎样才能正确地将这些坐标写入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: 更新:使用上面的excel导出方法,每当导出xls文件时,我在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. 单击“是”后,它会显示excel数据,但纬度/经度的格式不正确,如上所述。

You must specify the data type of the cells as string for xml spreadsheet format. 您必须将单元格的数据类型指定为xml电子表格格式的字符串 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: 对于mhtml文件格式,一个简单的表,如下面的代码,对我来说很好:

<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. 你得到的警告,是因为你没有保存真正的Excel(xls)文件。 You have an html file saved with xls extension. 您有一个使用xls扩展名保存的html文件。 MS Excel knows to read HTML files and to display them into a worksheet. MS Excel知道读取HTML文件并将其显示在工作表中。

There is a problem on this because excel (mso-number-format) doesn't support that type of number. 这有一个问题,因为excel(mso-number-format)不支持这种类型的数字。 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 . 请参阅此链接了解可用的mso-number-formats以及此链接,以便在excel中重新格式化

Try to add these css classes 尝试添加这些css类

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

And give these css classes to your data fields. 并将这些css类提供给您的数据字段。

<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]". 只需从JS导出为CSV-File,并使用syntax =“[NUM]”格式化坐标。 Like this: 像这样:

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

In Excel: 在Excel中:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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