乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎么用js将<em>excel</em>中的数据<em>读取</em>后

怎么用js将<em>excel</em>中的数据<em>读取</em>后

作者:乔山办公网日期:

返回目录:excel表格制作


先去了解下服务端模版吧,用服务端模版写动态页,比js容易。是什么模版与你用的服务端语言及框架有关,肯定都有
当然用js也可以,得用ajax获取数据,然后js渲染到页面中

服务端代码读取excel,和操作数据库差不多
然后用服务端模版写到页面中即可,table标签行合并rowspan(写在td属性中,rowspan="2"表示该单元格占两行),列合并colspan(写法同行合并,rowspan="2"表示该单元格占两列)

以前636f7079e799bee5baa6e79fa5e98193330读书的时候绝不会想到会用客户端脚本来实现这些功能,现在却一开始就要用上了,而且还觉得挺实用的。
参考《Windows脚本技术》,应该会有一点收获。

<html xmlns="http:///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" width="300" 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:///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" width="300" 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>


public function excel(){
e5a48de588b6e79fa5e98193333$list = M('map') -> select();//var_dump($list);die;
foreach($list as $key => $val){
$uid[$key] = $val['uid'];
}
$uid = arr_to_str(array_unique($uid));
$user_info = json_decode(getUserInfoList($uid));
foreach($user_info as $key => $val){
if(($username = $val -> user_name) || ($username = $val -> true_name) || ($username = $val -> email)){
$username = $username;
}
$user_list[$val->id] =  $username;
}

$genre = array('','自然灾害','环境问题','其它','社会问题');
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=益图-".date("Ymd",time()).".xls");
echo iconv('utf-8', 'gb2312', '地图标题'); echo  "\t";
echo iconv('utf-8', 'gb2312', '发布人'); echo  "\t";
echo iconv('utf-8', 'gb2312', '介绍'); echo  "\t";
echo iconv('utf-8', 'gb2312', '分类'); echo  "\t";
echo iconv('utf-8', 'gb2312', '发布时间'); echo  "\t";
echo "\n";
foreach($list as $key => $val){
echo iconv('utf-8', 'gb2312',$val['title']);echo  "\t";
echo iconv('utf-8', 'gb2312',$user_list[$val['uid']]);echo  "\t";
echo iconv('utf-8', 'gb2312',t($val['content']));echo  "\t";
echo iconv('utf-8', 'gb2312',$genre[$val['genre_id']]);echo  "\t";
echo iconv('utf-8', 'gb2312',date("Y-m-d",$val['create_time']));echo  "\t";
echo "\n";
}
}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网