怎么用js将excel中的数据读取后显示到网页中的表格
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用js将excel中的数据读取后显示到网页中的表格相关的知识,希望对你有一定的参考价值。
以前读书的时候绝不会想到会用客户端脚本来实现这些功能,现在却一开始就要用上了,而且还觉得挺实用的。参考《Windows脚本技术》,应该会有一点收获。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
objCon = new ActiveXObject("ADODB.Connection");
objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
objCon.CursorLocation = 1;
objCon.Open;
var strQuery;
//Get the SheetName
var strSheetName = "Sheet1$";
var rsTemp = new ActiveXObject("ADODB.Recordset");
rsTemp = objCon.OpenSchema(20);
if(!rsTemp.EOF)
strSheetName = rsTemp.Fields("Table_Name").Value;
rsTemp = null
rsExcel = new ActiveXObject("ADODB.Recordset");
strQuery = "SELECT * FROM [" + strSheetName + "]";
rsExcel.ActiveConnection = objCon;
rsExcel.Open(strQuery);
while(!rsExcel.EOF)
for(i = 0;i<rsExcel.Fields.Count;++i)
alert(rsExcel.Fields(i).value);
rsExcel.MoveNext;
// Close the connection and dispose the file
objCon.Close;
objCon =null;
rsExcel = null;
</script>
</head>
<body>
<input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value==\'\')alert(\'请选择xls文件\');else importXLS(f.value)" />
</body>
</html>
trackback:http://hi.baidu.com/netcorner/blog/item/4c35a818788f670635fa41d3.html
通过Javascript操作Excel
function AutomateExcel()
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & " " & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
// Make sure Excel is visible and give the user control
// of Excel\'s lifetime.
oXL.Visible = true;
oXL.UserControl = true;
<HTML>
<HEAD>
<TITLE>将页面中指定表格的数据导入到Excel中</TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
var oXL = new ActiveXObject("Excel.Application"); //创建应该对象
var oWB = oXL.Workbooks.Add();//新建一个Excel工作簿
var oSheet = oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表
var table = document.all.data;//指定要写入的数据源的id
var hang = table.rows.length;//取数据源行数
var lie = table.rows(0).cells.length;//取数据源列数
// Add table headers going cell by cell.
for (i=0;i<hang;i++)//在Excel中写行
for (j=0;j<lie;j++)//在Excel中写列
//定义格式
oSheet.Cells(i+1,j+1).NumberFormatLocal = "@";
//!!!!!!!上面这一句是将单元格的格式定义为文本
oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗
oSheet.Cells(i+1,j+1).Font.Size = 10;//字体大小
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向单元格写入值
oXL.Visible = true;
oXL.UserControl = true;
//-->
</SCRIPT>
</HEAD>
<BODY>
<table border="0" id="data" bgcolor="black" cellspacing="1">
<tr bgcolor="white">
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
</tr>
<tr bgcolor="white">
<td>0001</td>
<td>张三</td>
<td>22</td>
<td>女</td>
</tr>
<tr bgcolor="white">
<td>0002</td>
<td>李四</td>
<td>23</td>
<td>男</td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel">
</BODY>
</HTML>
以前读书的时候绝不会想到会用客户端脚本来实现这些功能,现在却一开始就要用上了,而且还觉得挺实用的。
参考《Windows脚本技术》,应该会有一点收获。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
objCon = new ActiveXObject("ADODB.Connection");
objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
objCon.CursorLocation = 1;
objCon.Open;
var strQuery;
//Get the SheetName
var strSheetName = "Sheet1$";
var rsTemp = new ActiveXObject("ADODB.Recordset");
rsTemp = objCon.OpenSchema(20);
if(!rsTemp.EOF)
strSheetName = rsTemp.Fields("Table_Name").Value;
rsTemp = null
rsExcel = new ActiveXObject("ADODB.Recordset");
strQuery = "SELECT * FROM [" + strSheetName + "]";
rsExcel.ActiveConnection = objCon;
rsExcel.Open(strQuery);
while(!rsExcel.EOF)
for(i = 0;i<rsExcel.Fields.Count;++i)
alert(rsExcel.Fields(i).value);
rsExcel.MoveNext;
// Close the connection and dispose the file
objCon.Close;
objCon =null;
rsExcel = null;
</script>
</head>
<body>
<input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value==\'\')alert(\'请选择xls文件\');else importXLS(f.value)" />
</body>
</html>
trackback:http://hi.baidu.com/netcorner/blog/item/4c35a818788f670635fa41d3.html
通过Javascript操作Excel
function AutomateExcel()
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & " " & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
// Make sure Excel is visible and give the user control
// of Excel\'s lifetime.
oXL.Visible = true;
oXL.UserControl = true;
<HTML>
<HEAD>
<TITLE>将页面中指定表格的数据导入到Excel中</TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
var oXL = new ActiveXObject("Excel.Application"); //创建应该对象
var oWB = oXL.Workbooks.Add();//新建一个Excel工作簿
var oSheet = oWB.ActiveSheet;//指定要写入内容的工作表为活动工作表
var table = document.all.data;//指定要写入的数据源的id
var hang = table.rows.length;//取数据源行数
var lie = table.rows(0).cells.length;//取数据源列数
// Add table headers going cell by cell.
for (i=0;i<hang;i++)//在Excel中写行
for (j=0;j<lie;j++)//在Excel中写列
//定义格式
oSheet.Cells(i+1,j+1).NumberFormatLocal = "@";
//!!!!!!!上面这一句是将单元格的格式定义为文本
oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗
oSheet.Cells(i+1,j+1).Font.Size = 10;//字体大小
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向单元格写入值
oXL.Visible = true;
oXL.UserControl = true;
//-->
</SCRIPT>
</HEAD>
<BODY>
<table border="0" id="data" bgcolor="black" cellspacing="1">
<tr bgcolor="white">
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
</tr>
<tr bgcolor="white">
<td>0001</td>
<td>张三</td>
<td>22</td>
<td>女</td>
</tr>
<tr bgcolor="white">
<td>0002</td>
<td>李四</td>
<td>23</td>
<td>男</td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel">
</BODY>
</HTML> 参考技术A
1、进入Internet属性。
2、点击安全。
3、选择自定义级别。
4、把ActiveX控件和插件下的所有选项都改成启用。
5、服务器生成html格式的Excel,然后设置
后台管理的功能:
1、需要将excel表格中的数据一次性复制到html table中
2、点击提交按钮,将table中的数据提交到服务器端进行处理。
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<script src="http://code.jquery.com/jquery-1.7.1.min.js" language="javascript"></script>
<style type="text/css">
body
background-color: white;
margin: 0;
padding: 0;
table
width:95%;
padding: 0;
margin-left:30px;
text-align: center;
th
font: 15px "trebuchet ms", '楷体_GB2312';
color: #4f6b72;
border-right: 1px dashed #c1dad7;
border-bottom: 1px dashed #c1dad7;
border-top: 1px dashed #c1dad7;
letter-spacing: 2px;
text-transform: uppercase;
background: #cae8ea;
margin: 0;
td
border-right: 1px dashed #c1dad7;
border-top: 1px dashed #c1dad7;
border-bottom: 1px dashed #c1dad7;
background: #fff;
font-size:12px;
color: #4f6b72;
margin: 0;
.btn_03
background-attachment: scroll;
background-clip: border-box;
background-color: #cae8ea;
background-origin: padding-box;
background-size: auto auto;
width: 65px;
.error
width: 12%;
vertical-align: top;
input
padding: 0;
margin: 0;
border: 0;
background: white;
width: 100%;
height:100%
</style>
</head>
<br/>
Jquery tableExport.js将网页中的表格导出为Excel
需求:将如下网页中的所有表格一次导入到Excel文件中。
方法:使用jQuery的tableExport.js插件,可以将网页中指定的table表格数据导出到Excel文件,而不需要经过后台。
操作步骤:
1. 在需要导出表格的页面引入以下两个js文件:
<script type="text/javascript" src="js/excel/FileSaver.min.js"></script> <script type="text/javascript" src="js/excel/tableExport.min.js"></script>
2. 点击导出按钮时,调用以下代码即可将多个table表格数据导出:
$(\'table\').tableExport({ type:\'excel\', ignoreColumn:[5],//从1开始,忽略第5列 mso: {//若table表格中使用了以下指定的样式属性,则将该样式同步到Excel中(可以保留表格原有的样式到Excel中) styles:[\'background-color\', \'border-top-color\', \'border-left-color\', \'border-right-color\', \'border-bottom-color\', \'border-top-width\', \'border-left-width\', \'border-right-width\', \'border-bottom-width\', \'border-top-style\', \'border-left-style\', \'border-right-style\', \'border-bottom-style\', \'color\'] } });
若需要导出指定的某个table表格,将$(\'table\')改为$(\'#表格ID\')即可。
导出的数据在Excel中的效果如下图:
插件的下载及其它功能的使用方法请看:
https://github.com/hhurz/tableExport.jquery.plugin
以上是关于怎么用js将excel中的数据读取后显示到网页中的表格的主要内容,如果未能解决你的问题,请参考以下文章
从网页中复制的下拉框粘贴到EXCEL中,如何导出下拉框中的数据,或者网页中下拉框的数据要怎样快速地采集