js操作wordexcel.docx
- 文档编号:5761025
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:18
- 大小:19.95KB
js操作wordexcel.docx
《js操作wordexcel.docx》由会员分享,可在线阅读,更多相关《js操作wordexcel.docx(18页珍藏版)》请在冰豆网上搜索。
js操作wordexcel
JS操作Word和Excel的方法
***************************************************************************************
center;">单元格1
center;">
MakeWord();"value="导出页面到Word"> functionMakeWord() { varword=newActiveXObject("Word.Application"); //vardoc=word.documents.open("c: \test.doc");//此处为打开已有的模版 vardoc=word.Documents.Add("",0,1);//不打开模版直接加入内容 varRange=doc.Range(); varsel=document.body.createTextRange(); sel.moveToElementText(form);//此处form是页面form的id sel.select(); sel.execCommand("Copy"); Range.Paste(); word.Application.Visible=true; alert("s"); word.Application.Selection.InlineShapes.AddPicture("c: \\m20.gif"); alert("n"); doc.saveAs("c: \\ba.doc");//存放到指定的位置注意路径一定要是“\\”不然会报错 } *************************************************************************************************** 2.用JS生成WORD *************************************************************************************************** functionwordcontorl(){ alert("1111") varWordApp=newActiveXObject("Word.Application"); varwdCharacter=1 varwdOrientLandscape=1 WordApp.Application.Visible=true; varmyDoc=WordApp.Documents.Add(); WordApp.ActiveDocument.PageSetup.Orientation=wdOrientLandscape WordApp.Selection.ParagraphFormat.Alignment=1//1居中对齐,0为居右 WordApp.Selection.Font.Bold=true WordApp.Selection.Font.Size=20 WordApp.Selection.TypeText("我的标题"); WordApp.Selection.MoveRight(wdCharacter); //光标右移字符 WordApp.Selection.TypeParagraph() //插入段落 WordApp.Selection.Font.Size=12 WordApp.Selection.TypeText("副标题");//分行插入日期 WordApp.Selection.TypeParagraph() //插入段落 varmyTable=myDoc.Tables.Add(WordApp.Selection.Range,8,7)//8行7列的表格 //myTable.Style="网格型" varaa="我的列标题" varTableRange;//以下为给表格中的单元格赋值 for(i=0;i<7;i++) { with(myTable.Cell(1,i+1).Range) { font.Size=12; InsertAfter(aa); ColumnWidth=4 } } for(i=0;i<7;i++) { for(n=0;n<7;n++) { with(myTable.Cell(i+2,n+1).Range) {font.Size=12; InsertAfter("bbbb"); } } } row_count=0; col_count=0 myDoc.Protect (1) } wordcontorl() *************************************************************************************************** 3.遍历导出到word *************************************************************************************************** 1、遍历导出每个文本框内的内容。 functionOpenWord(){//导出word vartxt="txt"; for(i=0;i { txt="txt"+jilu[i]; myRange=mydoc.Range(myRange.End-1,myRange.End);//设定起始点 varsel=Layer1.document.body.createTextRange(); //sel.moveToElementText(table1); sel.moveToElementText(document.all[txt]); sel.select(); Layer1.document.execCommand('Copy'); sel.moveEnd('character'); myRange.Paste(); myRange=mydoc.Range(myRange.End-1,myRange.End); myRange.InsertAfter("\n"); ExcelSheet.ActiveWindow.View.TableGridlines=false;//隐藏虚框 } } 2、拷贝table1内的内容到word functionOpenWord(){//导出word Layer1.style.border=0; ExcelSheet=newActiveXObject('Word.Application'); ExcelSheet.Application.Visible=true; varmydoc=ExcelSheet.Documents.Add('',0,0); myRange=mydoc.Range(0,1); myRange=mydoc.Range(myRange.End-1,myRange.End);//设定起始点 varsel=Layer1.document.body.createTextRange(); sel.moveToElementText(table1); sel.select(); Layer1.document.execCommand('Copy'); sel.moveEnd('character'); myRange.Paste(); myRange=mydoc.Range(myRange.End-1,myRange.End); myRange.InsertAfter("\n"); ExcelSheet.ActiveWindow.View.TableGridlines=false; } ************************************************************************************************** 4.操作excel ************************************************************************************************** <%@pagec%>content
functionMakeExcel(){
vari,j,n;
try{
varxls=newActiveXObject("Excel.Application");
}
catch(e){
alert("要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX控件”,您的浏览器须允许执行控件。
请点击【帮助】了解浏览器设置方法!
");
return"";
}
xls.visible=true;//设置excel为可见
varxlBook=xls.Workbooks.Add;
varxlsheet=xlBook.Worksheets
(1);
--合并-->
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).mergecells=true;
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).value="发卡记录";
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,3)).Interior.ColorIndex=5;//设置底色为蓝色
//xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,6)).Font.ColorIndex=4;//设置字体色
//xlsheet.Rows
(1).Interior.ColorIndex=5;//设置底色为蓝色设置背景色Rows
(1).Font.ColorIndex=4
--设置行高-->
xlsheet.Rows
(1).RowHeight=25;
--设置字体ws.Range(ws.Cells(i0+1,j0),ws.Cells(i0+1,j1)).Font.Size=13-->
xlsheet.Rows
(1).Font.Size=14;
--设置字体设置选定区的字体xlsheet.Range(xlsheet.Cells(i0,j0),ws.Cells(i0,j0)).Font.Name="黑体"-->
xlsheet.Rows
(1).Font.Name="黑体";
--设置列宽xlsheet.Columns
(2)=14;-->
xlsheet.Columns("A:
D").ColumnWidth=18;
--设置显示字符而不是数字-->
xlsheet.Columns
(2).NumberFormatLocal="@";
xlsheet.Columns(7).NumberFormatLocal="@";
//设置单元格内容自动换行range.WrapText=true;
//设置单元格内容水平对齐方式range.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;//设置单元格内容竖直堆砌方式
//range.VerticalAlignment=Excel.XlVAlign.xlVAlignCenter
//range.WrapText=true;xlsheet.Rows(3).WrapText=true自动换行
//设置标题栏
xlsheet.Cells(2,1).Value="卡号";
xlsheet.Cells(2,2).Value="密码";
xlsheet.Cells(2,3).Value="计费方式";
xlsheet.Cells(2,4).Value="有效天数";
xlsheet.Cells(2,5).Value="金额";
xlsheet.Cells(2,6).Value="所属服务项目";
xlsheet.Cells(2,7).Value="发卡时间";
varoTable=document.all['fors:
data'];
varrowNum=oTable.rows.length;
for(i=2;i<=rowNum;i++){
for(j=1;j<=7;j++){
//htmltable类容写到excel
xlsheet.Cells(i+1,j).Value=oTable.rows(i-1).cells(j-1).innerHTML;
}
}
--xlsheet.Range(xls.Cells(i+4,2),xls.Cells(rowNum,4)).Merge;-->
//xlsheet.Range(xlsheet.Cells(i,4),xlsheet.Cells(i-1,6)).BorderAround,4
//for(mn=1,mn<=6;mn++).xlsheet.Range(xlsheet.Cells(1,mn),xlsheet.Cells(i1,j)).Columns.AutoFit;
xlsheet.Columns.AutoFit;
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(rowNum+1,7)).HorizontalAlignment=-4108;//居中
xlsheet.Range(xlsheet.Cells(1,1),xlsheet.Cells(1,7)).VerticalAlignment=-4108;
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Font.Size=10;
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(3).Weight=2;//设置左边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders(4).Weight=2;//设置右边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders
(1).Weight=2;//设置顶边距
xlsheet.Range(xlsheet.Cells(2,1),xlsheet.Cells(rowNum+1,7)).Borders
(2).Weight=2;//设置底边距
xls.UserControl=true;//很重要,不能省略,不然会出问题意思是excel交由用户控制
xls=null;
xlBook=null;
xlsheet=null;
}
top"border="0"cellpadding="0"cellspacing="0"width="100%">
sort"border="0"cellpadding="0"cellspacing="0"style="valign: center"width="100%"> _id7"value="生成excel文件"/>
data"border="1"cellpadding="0"cellspacing="1"width="100%"> data: headerText1">卡号 data: headerText2">密码 data: headerText3">计费方式 data: headerText4">有效天数 data: headerText6">所属服务项目 data: headerText7">发卡时间 14: 40.843 14: 40.843 14: 40.843 14: 40.843 14: 40.843 14: 40.843 如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。 copyright@ 2008-2022 冰点文档网站版权所有 经营许可证编号:鄂ICP备2022015515号-1h000010010 543860 计点 2.0 测试项目 2006-06-2310: h000010011 683352 计点 2.0 测试项目 2006-06-2310: h000010012 433215 计点 2.0 测试项目 2006-06-2310: h000010013 393899 计点 2.0 测试项目 2006-06-2310: h000010014 031736 计点 2.0 测试项目 2006-06-2310: h000010015 188600 计点 2.0 测试项目 2006-06-2310: h000010016 363407 计点 2.0 测试项目 20