网上得到如下代码可以实现js读取word、excel的内容。。我想得到word、Excel的页数怎么才能获得呢,求各位大侠鼎力相助。。。
GetDataFromExcelPage
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
{
for(var j=1;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
{
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
{
//每个标记的值
alert(nodeInfo[j].nodeValue);
}
}
//vsXMLApp.Close();
vsXMLApp = null;
//GarbageCollection
idTmr = window.setInterval("Cleanup();",1);
return false;
}
function Cleanup()
{
window.clearInterval(idTmr);
CollectGarbage();
}