日期:2014-05-16  浏览次数:20507 次

自己做的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对象