1、js操作wordexcelJS操作Word和Excel的方法* 单元格1 单元格2 单元格3 单元格4 单元格合并test function MakeWord() var word = new ActiveXObject(Word.Application); / var doc = word .documents.open(c:test.doc); /此处为打开已有的模版var doc = word .Documents.Add(,0,1);/不打开模版直接加入内容 var Range=doc.Range(); var sel = document.body.createTextRange()
2、; 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* function wordcontorl(
3、) alert(1111) var WordApp=new ActiveXObject(Word.Application); var wdCharacter=1var wdOrientLandscape = 1WordApp.Application.Visible=true;var myDoc=WordApp.Documents.Add(); WordApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape WordApp. Selection.ParagraphFormat.Alignment=1 /1居中对齐,0为居右Word
4、App. 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()/插入段落 v
5、ar myTable=myDoc.Tables.Add (WordApp.Selection.Range, 8,7) /8行7列的表格 /myTable.Style=网格型 var aa = 我的列标题 var TableRange; /以下为给表格中的单元格赋值 for (i= 0;i7;i+) with (myTable.Cell(1,i+1).Range) font.Size = 12;InsertAfter(aa);ColumnWidth =4for (i =0;i7;i+)for (n =0;n7 ;n+) with (myTable.Cell(i+2,n+1).Range) fon
6、t.Size = 12; InsertAfter(bbbb); row_count = 0; col_count = 0myDoc.Protect(1)wordcontorl()*3.遍历导出到word*1、遍历导出每个文本框内的内容。function OpenWord()/导出wordvar txt=txt;for(i=0;itable1.rows.length;i+)/遍历导出图表和文字txt=txt+jilui;myRange =mydoc.Range(myRange.End-1,myRange.End);/设定起始点var sel=Layer1.document.body.create
7、TextRange();/sel.moveToElementText(table1);sel.moveToElementText(document.alltxt);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、拷贝
8、table1内的内容到wordfunction OpenWord()/导出wordLayer1.style.border=0;ExcelSheet = new ActiveXObject(Word.Application);ExcelSheet.Application.Visible = true;var mydoc=ExcelSheet.Documents.Add(,0,0);myRange =mydoc.Range(0,1);myRange =mydoc.Range(myRange.End-1,myRange.End);/设定起始点var sel=Layer1.document.body.
9、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*content function MakeExcel() va
10、r i, j, n; try var xls = new ActiveXObject(Excel.Application); catch(e) alert( 要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。请点击【帮助】了解浏览器设置方法!); return ; xls.visible =true; / 设置excel为可见 var xlBook = xls.Workbooks.Add; var xlsheet = xlBook.Worksheets(1); xlsheet.Range(xlsheet.Cells(1,1),xl
11、sheet.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 .ColorIn
12、dex = 5 ;/设置底色为蓝色 设置背景色 Rows(1).Font.ColorIndex=4 xlsheet.Rows(1).RowHeight = 25; xlsheet.Rows(1).Font.Size=14; xlsheet.Rows(1).Font.Name=黑体; xlsheet.Columns(A:D).ColumnWidth =18; xlsheet.Columns(2).NumberFormatLocal=; xlsheet.Columns(7).NumberFormatLocal=; /设置单元格内容自动换行 range.WrapText = true ; /设置单元
13、格内容水平对齐方式 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 = 计费方式
14、; xlsheet.Cells(2, 4).Value = 有效天数; xlsheet.Cells(2, 5).Value = 金额; xlsheet.Cells(2, 6).Value = 所属服务项目; xlsheet.Cells(2, 7).Value = 发卡时间; var oTable = document.allfors:data; var rowNum = oTable.rows.length; for(i = 2; i = rowNum; i+) for (j = 1; j = 7; j+) /html table类容写到excel xlsheet.Cells(i + 1, j
15、).Value = oTable.rows(i - 1).cells(j - 1).innerHTML; / 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(r
16、owNum+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(
17、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; /很重要,不能省略,不然会出问题 意思是exce
18、l交由用户控制 xls=null; xlBook=null; xlsheet=null; ziyuanweihu 卡号 密码 计费方式 有效天数 金额 所属服务项目 发卡时间 h000010010 543860 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010011 683352 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010012 433215 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010013 393899 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010014 031736 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010015 188600 计点 2.0 测试项目 2006-06-23 10:14:40.843 h000010016 363407 计点 2.0 测试项目 20
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1