javascript 显示 Excel 工作表中的表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15542106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:08:31  来源:igfitidea点击:

display a table from a excel sheet

javascripthtmlexcelexporthtml-table

提问by userX

I want to display a table from a excel sheet in my html page. There are multiple tables in a excel sheet. I want a specific table to be displayed. Please Help..

我想在我的 html 页面中显示来自 Excel 工作表的表格。一个excel表中有多个表。我想显示一个特定的表。请帮忙..

The HTML code i used to extract information from excel file

我用来从excel文件中提取信息的HTML代码

 <html>
 <head>
 <title>
 From Excel
 </title>
 <script language="javascript" >
 function GetData(cell,row){
 var excel = new ActiveXObject("Excel.Application");
 var excel_file = excel.Workbooks.Open("C:\abc.xlsx");
 var excel_sheet = excel.Worksheets("25 PEs");
 var data = excel_sheet.Cells(cell,row).Value;
 document.getElementById('div1').innerText =data;
}
</script>
</head>

<body>

<div id="div1" style="background: #DFDFFF; width:'100%';" align="center">
Click buttons to fetch data from c:\abc.xlsx
</div>

<input name="Button1" type="button" onClick="GetData(1,1)" value="button1">
<input name="Button2" type="button" onClick="GetData(1,2)" value="button2">
<input name="Button3" type="button" onClick="GetData(2,1)" value="button3">
<input name="Button4" type="button" onClick="GetData(2,2)" value="button4">


</body>
</html>

This code extracts one cell at a time. I want to extract an entire table at once. How to do it?

此代码一次提取一个单元格。我想一次提取整个表格。怎么做?

回答by lukeocom

A good option is to use the jquery library dataTables. You can then use a tool such as that provided by Mr DataConverter to convert your spreadsheet into json. You then point your datatables to the json file, filter out any columns you dont need, and viola, your data is displayed in a nice html table, and customised to how you like it. I have used this approach just recently and it has made displaying a huge amount of data that much easier.

一个不错的选择是使用 jquery 库 dataTables。然后,您可以使用诸如 Mr DataConverter 提供的工具将电子表格转换为 json。然后,您将数据表指向 json 文件,过滤掉不需要的任何列,并且中提琴,您的数据显示在一个漂亮的 html 表中,并根据您的喜好进行自定义。我最近才使用这种方法,它使显示大量数据变得更加容易。

DataTables - http://www.datatables.net/

数据表 - http://www.datatables.net/

Mr Data Converter - http://www.shancarter.com/data_converter/index.html

数据转换器先生 - http://www.shancarter.com/data_converter/index.html

The code to hide individual columns would look something like this in your dataTables intializer:

隐藏单个列的代码在 dataTables 初始化程序中看起来像这样:

"aoColumnDefs" : [{
    "bVisible" : false,
    "bSearchable": true, 
    "aTargets" : [0, 1, 4, 6, 9, 10, 11, 12]
}

and to re-order the columns would look something like:

并重新排序列看起来像:

"aoColumns": [//reorder the columns
    { "mDataProp": [0] },
    { "mDataProp": [1] },
    { "mDataProp": [2] },
    { "mDataProp": [7] },
    { "mDataProp": [4] },
],

hope this is of some help...

希望这个对你有帮助...