javascript导出excelWord格式文档下载.docx

上传人:b****6 文档编号:19906121 上传时间:2023-01-12 格式:DOCX 页数:9 大小:20.12KB
下载 相关 举报
javascript导出excelWord格式文档下载.docx_第1页
第1页 / 共9页
javascript导出excelWord格式文档下载.docx_第2页
第2页 / 共9页
javascript导出excelWord格式文档下载.docx_第3页
第3页 / 共9页
javascript导出excelWord格式文档下载.docx_第4页
第4页 / 共9页
javascript导出excelWord格式文档下载.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

javascript导出excelWord格式文档下载.docx

《javascript导出excelWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《javascript导出excelWord格式文档下载.docx(9页珍藏版)》请在冰豆网上搜索。

javascript导出excelWord格式文档下载.docx

HtmlNodehnPreviousSibling=phnTd.PreviousSibling;

while(hnPreviousSibling!

=null&

&

hnPreviousSibling.Name!

=phnTd.Name)

hnPreviousSibling=hnPreviousSibling.PreviousSibling;

}

8

if(hnPreviousSibling!

=null)

10 

11 

intnColSpan=hnPreviousSibling.GetAttributeValue("

colspan"

1);

12 

returnHorizontalDeduction(hnPreviousSibling)+nColSpan;

13 

14

15 

return0;

16 

  经过横向推导,各td的X、Y坐标如下图所示:

  纵向推导(VerticalDeduction)

  一次纵向推导的过程可以描述为(当前推导td用A表示):

   

找到A之前的行tr中与A具有相同X坐标的td节点B

if(B.rowspan>

(A.Y-B.Y))

X+=B.colspan,即A的X坐标向后推B.colspan的位置

同时,与A同处一tr但在其后边的td节点均应向后推B.colspan个位移

  对td节点A反复执行这样的一个过程,直到确定A无需再次移动。

  纵向推导核心代码为:

1boolbActedPush=false;

2

3do

4{

intnComparedItemIndex=-1;

for(intj=i-1;

j>

=0;

j--)

if(plstCells[j]._nStartX==oCurrentCell._nStartX)

nComparedItemIndex=j;

break;

if(nComparedItemIndex>

=0)

17 

if(plstCells[nComparedItemIndex]._nRowSpan>

(oCurrentCell._nStartY-plstCells[nComparedItemIndex]._nStartY))

18 

19 

oCurrentCell._nStartX+=plstCells[nComparedItemIndex]._nColSpan;

20

21 

bActedPush=true;

22

23 

for(intk=i+1;

k<

plstCells.Count;

k++)

24 

25 

if(plstCells[k]._nStartY==oCurrentCell._nStartY)

26 

27 

plstCells[k]._nStartX+=plstCells[nComparedItemIndex]._nColSpan;

28 

29 

30 

31 

else

32 

33 

bActedPush=false;

34 

35 

36 

37 

38 

39 

40 

41 

while(bActedPush);

  以示例table中的fourtd为例,其经过纵向推导过程后的坐标位置情况如下图:

  关于示例代码的几点说明:

  1、在示例代码中,我通过一个Config文件对生成的Excel的文件名、其内报表的内容和位置做了一些控制。

基本内容如下:

<

ExcelDocument>

<

BaseInfo>

FileName>

SampleExcelFile<

/FileName>

!

--生成Excel的文件名-->

SheetCount>

1<

/SheetCount>

--Excel中sheet的数量-->

/BaseInfo>

Tables>

ExcelTable>

TableName>

示例表一<

/TableName>

--报表名称-->

WhichSheet>

/WhichSheet>

--所在sheet的序号-->

StartX>

2<

/StartX>

--左上角X坐标-->

StartY>

/StartY>

--左上角Y坐标-->

Source>

Sample_Page_1.aspx<

/Source>

--table所在页面-->

/ExcelTable>

/Tables>

/ExcelDocument>

  2、在解析html的过程中,我使用了HtmlAgilityPack解析工具。

感兴趣的朋友可以研究一下。

地址是这里:

  3、HtmlAgilityPack解析html的过程中,其将html标签之间的空隙也会看成是一个节点,为内容为空字符串的文本节点,这点大家应注意。

  4、该示例代码基本上是一个完整的功能,并且和系统中其他模块耦合度很小。

有类似需求的朋友可以拿来直接用。

javascript导出excel(应用) 

2008-03-1915:

16:

40| 

分类:

雨天の技术| 

标签:

|字号大中小 

订阅

divalign="

right"

>

inputname="

exp"

value="

导出页面为EXCEL"

type="

button"

onclick="

tableToExcel()"

/>

inputtype="

file"

name="

excelfile"

导入EXCEL文件"

readExcel()"

im"

/>

/div>

--TABLE中的内容是JAVASCRIPT自动生成的-->

tablewidth="

100%"

techTable"

id="

border="

0"

align="

center"

cellpadding="

5"

cellspacing="

1"

bgcolor="

D4DBED"

/table>

SCRIPTLANGUAGE="

JavaScript"

functiontableToExcel(){

varrowNum=0;

varoXL=newActiveXObject("

Excel.Application"

);

//创建应该对象

 

varoWB=oXL.Workbooks.Add();

//新建一个Excel工作簿

varoSheet=oWB.ActiveSheet;

//指定要写入内容的工作表为活动工作表

// 

vartable=pageId;

varrows=table.rows.length;

// 

varcolSumCols=table.rows(0).cells.length;

varbeginCol=0;

varcols=colSumCols;

varrowSpans=1;

//行合并

varcolSpans=1;

//列合并

oXL.visible=true;

oWB.Worksheets

(1).Range("

B4:

D4:

F4"

).ColumnWidth=20;

/**********表头**********************/

varexcRange=oSheet.Range(oSheet.Cells(1,1),oSheet.Cells(1,6));

excRange.Merge(true);

//获取型号等信息

varexcelName="

%=session.getAttribute("

excel"

)%>

"

;

excelName=chkrecName+"

-"

+excelName+"

-技术参数"

excRange.value=excelName;

excRange.HorizontalAlignment=3;

//'

居中

excRange.Font.Name="

黑体"

rowNum=rowNum+1;

/****************结束结果设置******************/

varobj1=document.getElementById('

techTable'

rowNum=resultToExcel(rowNum,obj1,oSheet,"

rowNum=rowNum+2;

//增加提示,不可以编辑EXCEL的批注

varexcRange2=oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(2,6));

excRange2.Merge(true);

excRange2.value="

提示:

务必不要编辑下面单元格中批注的内容!

否则将无法实现导入!

excRange2.HorizontalAlignment=2;

excRange2.Font.Name="

宋体"

excRange2.Font.Size=10;

try{

oWB.SaveAs("

c:

\\"

.xls"

oWB.Close;

oXL.visible=false;

oXL.Quit();

alert("

文件保存成功!

已保存到C:

}catch(e){

文件保存失败!

}

functionresultToExcel(rowNum,obj,oSheet,tableTile){

if(obj==null){

returnrowNum;

varrows=obj.rows.length;

varcolSumCols=obj.rows(0).cells.length;

varbeginCol=0;

excRange=oSheet.Range(oSheet.Cells(rowNum+1,1),oSheet.Cells(rowNum+1,colSumCols));

excRange.value=tableTile;

//寻找列数,考虑到第一行可能存在合并

for(vari=0;

i<

colSumCols;

i++){

vartmpcolspan=obj.rows(0).cells(i).colSpan;

if(tmpcolspan>

1){

cols+=tmpcolspan-1;

//定义2维容器数据,1:

行;

2:

列;

值(0可以填充,1已被填充)

varcontainer=newArray(rows);

i<

rows;

container[i]=newArray(cols);

for(j=0;

j<

cols;

j++){

container[i][j]=0;

//将所有单元置为文本,避免非数字列被自动变成科学计数法和丢失前缀的0

oSheet.Range(oSheet.Cells(rowNum+1,1),oSheet.Cells(rowNum+rows,cols)).NumberFormat="

@"

//Addobjheadersgoingcellbycell.

//varstr="

for(i=0;

i++){

j++){

//寻找开始列

for(k=j;

k<

k++){

if(container[i][k]==0){

beginCol=k;

k=cols;

//退出循环

//赋值

try{

varisHtml=obj.rows(i).cells(j).innerHTML;

varinnerText=obj.rows(i).cells(j).innerText;

//判断是否是文本框

if(isHtml.indexOf("

INPUT"

)==-1){

oSheet.Cells(i+1+rowNum,beginCol+1).value=innerText;

}else{

//str=str+"

Names:

:

+obj.rows(i).cells(j).getElementsByTagName("

input"

)[2].name+"

\n"

oSheet.Cells(i+1+rowNum,beginCol+1).AddComment(obj.rows(i).cells(j).getElementsByTagName("

)[1].id);

//oSheet.Cells(i+1+rowNum,beginCol+1).value=obj.rows(i).cells(j).innerText;

colSpans=obj.rows(i).cells(j).colSpan;

if(colSpans>

1){

//合并

oSheet.Range(oSheet.Cells(i+1+rowNum,beginCol+1),oSheet.Cells(i+1+rowNum,beginCol+colSpans)).Merge();

//将当前obj位置填写到对应的容器中

for(k=0;

k<

colSpans;

k++){

container[i][beginCol+k]=1;

rowSpans=obj.rows(i).cells(j).rowSpan;

if(rowSpans>

1){//行合并

oSheet.Range(oSheet.Cells(i+1+rowNum,beginCol+1),oSheet.Cells(i+rowSpans+rowNum,beginCol+colSpans)).Merge();

for(k=1;

rowSpans;

k++){ 

//由于第0行已经被colSpans对应的代码填充了,故这里从第1行开始

for(l=0;

l<

l++){

container[i+k][beginCol+l]=1;

//如果开始列+合并列已经等于列数了,故不需要再循环htmltable

if(beginCol+colSpans>

=cols)j=cols;

}catch(e){

//字符大小为9

oSheet.Range(oSheet.Cells(rowNum+1,1),oSheet.Cells(rowNum+rows,cols)).Font.Size=9;

//自动调整列宽

oSheet.Range(oSheet.Cells(1,1),oSheet.Cells(rowNum+rows,cols)).Columns.AutoFit();

//自动调整行高

oSheet.Range(oSheet.Cells(rowNum+1,1),oSheet.Cells(rowNum+rows,cols)).Rows.AutoFit();

//划边框

oSheet.Range(oSheet.Cells(rowNum+1,1),oSheet.Cells(rowNum+rows,cols)).Borders.LineStyle=1;

rowNum=rowNum+rows;

returnrowNum;

functionreadExcel()

{

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 预防医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1