当前位置: 代码迷 >> JavaScript >> 自个儿做的javascript读取word、excel、xml文件
  详细解决方案

自个儿做的javascript读取word、excel、xml文件

热度:326   发布时间:2013-08-26 12:17:40.0
自己做的javascript读取word、excel、xml文件

<!--把以下的代码保存为SeeResult.html即可看到效果-->

<HTML>
?<HEAD>
??<title>GetDataFromExcelPage</title>
??<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
??<meta content="C#" name="CODE_LANGUAGE">
??<meta content="JavaScript" name="vs_defaultClientScript">
??<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
??<style type="text/css"> .myCss { background-color: #c0ddff; text-align:center; vertical-align:sub; }
??</style>
??<style type="text/css"> .myBtn { width:50px; height:20px; background-color: #d8f2fe?}
??</style>
??<script language="javascript">
??var idTmr = "";
??function InertDataFromExcelToDataBase()
??{
???var vsFilePath=document.all("InputExcel").value;
???if( vsFilePath == undefined || vsFilePath == null || vsFilePath == "undefined"
????|| vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.XLS') == -1)
???{
????alert("please choose the excel file !");
????return false;
???}
???//创建Excel程序对象
???var vsExcel = "";
???try
???{
????vsExcel = new ActiveXObject("Excel.Application");
???}
???catch(err)
???{
????alert(err.description);
????return false;
???}
???//打开
???var vsBook = vsExcel.Workbooks.Open(vsFilePath);???
???//Excel的第一张表格
???var vsSheet = vsBook.Worksheets(1);?
???vsSheet.Select();
???//6行
???for(var i=1;i<7;i++)
???{
????//2列
????for(var j=1;j<3;j++)
????{
?????//单元格取值
?????alert(vsSheet.Cells(i,j).value);
????}
???}
???
???/*可以如下写法,但是Excel默认的行和列都很大的,好几万呢:)
???for(var i=1;i<vsSheet.Rows.Count;i++)
???{
????for(var j=1;j<vsSheet.Columns.Count;j++)
????{
?????alert(vsSheet.Cells(i,j).value);
????}
???}
???*/
???
???
???vsSheet=null;
???vsBook=null;
???//退出
???vsExcel.Quit();
???vsExcel = null;
???//GarbageCollection
???idTmr = window.setInterval("Cleanup();",1);???

???return false;
??}
??function InsertDataFromWordToDataBase()
??{
???var vsFilePath=document.all("InputWord").value;
???if( vsFilePath == undefined || vsFilePath == null
???????|| vsFilePath == "undefined" || vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.DOC') == -1)
???{
????alert("please choose the word file !");
????return false;
???}
???//创建Word对象
???try
???{
????var vsWordApp = new ActiveXObject("Word.Application");
???}
???catch(err)
???{
????alert(err.description);
????return false;
???}
???//打开Word文档
???var wordInfo = vsWordApp.Documents.Open(vsFilePath);
???/*??其他的一些属性
????word (index)
????Range对象

????characters (index)
????Range对象

????sentences (index)
????Range对象

????paragraphs (index)
????Paragraph对象

????sections (index)
????Section对象
???*/
???//不可见
???vsWordApp.Visible = false;
???//需要关闭其他word文档,防止出现读取其他文档情况
???
???/*
???//逐个字符读取整个文档
???var characters = vsWordApp.documents(1).characters
???for(var i=1;i<characters.count;i++)
???{
????alert(characters(i).text);

????//alert(vsWordApp.Application.Selection);
????//移动一个单元,宽度为1(无论是汉字还是英文字母)
????//vsWordApp.Application.Selection.MoveRight(Unit=1,Count=1);
???}
???
???//逐个字读取整个文档
???var words = vsWordApp.documents(1).words;
???for(var i=1;i<=words.count;i++)
???{
????alert(words(i).text);
???}
???*/
???
???//逐个段落读取整个文档
???var paragraphs = vsWordApp.documents(1).paragraphs;
???for(var i=1;i<=paragraphs.count;i++)
???{
????alert(paragraphs(i).range.text);
???}
??????
???vsWordApp = null;
???//关闭
???wordInfo.Close();
???wordInfo = null;
???//GarbageCollection
???idTmr = window.setInterval("Cleanup();",1);?
???
???return false;?
??}
??function InsertDataFromXMLToDataBase()
??{
???var vsFilePath=document.all("InputXML").value;
???if( vsFilePath == undefined || vsFilePath == null
????|| vsFilePath == "undefined" || vsFilePath == "" || vsFilePath.toUpperCase().indexOf('.XML') == -1)
???{
????alert("please choose the xml file !");
????return false;
???}
???var vsXMLApp = "";
???//创建XML对象
???try
???{
????vsXMLApp = new ActiveXObject("Microsoft.XMLDOM");
???}
???catch(err)
???{
????alert(err.description);
????return false;
???}
???vsXMLApp.async = true;
???vsXMLApp.resolveExternals = false;
???//打开
???vsXMLApp.load(vsFilePath)
???//取得跟节点下面所有子节点集合
???var nodes = vsXMLApp.documentElement.childNodes;
???for( var i = 0; i < nodes.length ; i++ )
???{
????//对每个子节点取得标记集合
????var nodeInfo = nodes[i].getElementsByTagName_r("RecordsetInfo").context.attributes;
????for(var j=0;j<nodeInfo.length;j++)
????{
?????//每个标记的值
?????alert(nodeInfo[j].nodeValue);
????}
???}
?????//vsXMLApp.Close();
????vsXMLApp = null;
????//GarbageCollection
????idTmr = window.setInterval("Cleanup();",1);
???
????return false;
??}
??function Cleanup()
??{
???window.clearInterval(idTmr);
???CollectGarbage();
??}
??</script>
?</HEAD>
?<body bgColor="gray" MS_POSITIONING="GridLayout">
??<form id="GetDataFromExcel" method="post" runat="server">
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center" bgColor="green">
????<tr>
?????<td align="center"><font color="red" size="5">Javascript Read Data From Office File To
???????Show You</font></td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 80px" align="center">
????<tr>
?????<td class="myCss" style="WIDTH: 205px" align="left"><label id="lb" title="选择文件">Please
???????Choose The Excel File :</label>
?????</td>
?????<td class="myCss" align="right"><input id="InputExcel" type="file" name="InputExcel"><br>
??????<font color="yellow" size="2">Read First Row 6 and Column 2</font>
?????</td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center" bgColor="green">
????<tr>
?????<td></td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center">
????<tr>
?????<td align="center"><input class="myBtn" id="Import" onclick="InertDataFromExcelToDataBase()" type="button"
???????value="OK">&nbsp;&nbsp; <input class="myBtn" id="QuitHere" onclick="window.close();" type="button" value="Exit">
?????</td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 80px" align="center">
????<tr>
?????<td class="myCss" style="WIDTH: 203px" align="center"><label id="lb2">Please Choose The
???????Word File :</label>
?????</td>
?????<td class="myCss" align="center"><input id="InputWord" type="file">
?????</td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center" bgColor="green">
????<tr>
?????<td></td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center">
????<tr>
?????<td align="center"><input class="myBtn" id="ImportWord" onclick="InsertDataFromWordToDataBase()" type="button"
???????value="OK"> <input class="myBtn" id="QuitPage" onclick="window.close();" type="button" value="Exit">
?????</td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 80px" align="center">
????<tr>
?????<td class="myCss" style="WIDTH: 203px" align="center"><label id="lb3">Please Choose The
???????XML File :</label>
?????</td>
?????<td class="myCss" align="center"><input id="InputXML" type="file">
?????</td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center" bgColor="green">
????<tr>
?????<td><FONT face="宋体"></FONT></td>
????</tr>
???</table>
???<table style="WIDTH: 800px; HEIGHT: 10px" align="center">
????<tr>
?????<td align="center"><input class="myBtn" id="ImportXML" onclick="InsertDataFromXMLToDataBase()" type="button"
???????value="OK"> <input class="myBtn" id="QuitHtml" onclick="window.close();" type="button" value="Exit">
?????</td>
????</tr>
???</table>
??</form>
?</body>
</HTML>

?