Home  >  Article  >  Web Front-end  >  javascript implementation code for operating Word and Excel_javascript skills

javascript implementation code for operating Word and Excel_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:43:171068browse

1.保存html页面到word

复制代码 代码如下:



<br>













单元格1 单元格2 单元格3 单元格4
单元格合并







test









2.用JS生成WORD

复制代码 代码如下:


<script> <br>function wordcontorl(){ <br>alert("1111") <br>var WordApp=new ActiveXObject("Word.Application"); <br><br>var wdCharacter=1 <br>var wdOrientLandscape = 1 <br>WordApp.Application.Visible=true; <br>var myDoc=WordApp.Documents.Add(); <br><br>WordApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape <br><br>WordApp. Selection.ParagraphFormat.Alignment=1 //1居中对齐,0为居右 <br>WordApp. Selection.Font.Bold=true <br>WordApp. Selection.Font.Size=20 <br><br>WordApp. Selection.TypeText("我的标题"); <br>WordApp. Selection.MoveRight(wdCharacter);    //光标右移字符 <br>WordApp.Selection.TypeParagraph()         //插入段落 <br>WordApp. Selection.Font.Size=12 <br>WordApp. Selection.TypeText("副标题"); //分行插入日期 <br>WordApp.Selection.TypeParagraph()         //插入段落 <br><br><br><br>var myTable=myDoc.Tables.Add (WordApp.Selection.Range, 8,7) //8行7列的表格 <br>//myTable.Style="网格型" <br>var aa = "我的列标题" <br><br>var TableRange; //以下为给表格中的单元格赋值 <br>for (i= 0;i<7;i ) <BR>{ <BR>with (myTable.Cell(1,i 1).Range) <BR>{ <BR>font.Size = 12; <BR>InsertAfter(aa); <BR>ColumnWidth =4 <BR>} <BR>} <br><br>for (i =0;i<7;i ) <BR>{ <BR>for (n =0;n<7 ;n ) <BR>{ <br><br>with (myTable.Cell(i 2,n 1).Range) <BR>{font.Size = 12; <BR>InsertAfter("bbbb"); <BR>} <BR>} <BR>} <BR>row_count = 0; <BR>col_count = 0 <BR>myDoc.Protect(1) <BR>} <BR>wordcontorl() <BR></script>

***************************************************************************************************
3.遍历导出到word
***************************************************************************************************
1、遍历导出每个文本框内的内容。
复制代码 代码如下:



2. Copy the content in table1 to word
Copy code The code is as follows:



****************************** *************************************************** ************************
4. Operation excel
****************** *************************************************** ****************************
Copy code The code is as follows:

<%@ page c %>

content






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 测试项目 2006-06-23 10:14:40.843
h000010017 175315 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010018 354437 计点 2.0 测试项目 2006-06-23 10:14:40.843
h000010019 234750 计点 2.0 测试项目 2006-06-23 10:14:40.843




















复制代码 代码如下:

<%--
// ---------------------------------- 1 ---------------------------------- \


Export to excel














< ;td>Fourth cell



First cell Second cell Third cell
Fifth cell Sixth cell




// ---------------------------------- 2 ---------- ----------------------- \

< script language="javascript">
function tableToExcel() {
window.clipboardData.setData("Text",document.all('theObjTable').outerHTML);
try {
var ExApp = new ActiveXObject("Excel.Application");
var ExWBk = ExApp.workbooks.add();
var ExWSh = ExWBk.worksheets(1);
ExApp.DisplayAlerts = false;
ExApp.visible = true;
}
catch(e) {
alert("Microsoft Excel software is not installed on your computer! ");
return false;
}
ExWBk.worksheets(1).Paste;
}
function tableToWord() {
var oWD = new ActiveXObject("Word.Application ");
var oDC = oWD.Documents.Add("",0,1);
var oRange =oDC.Range(0,1);
var sel = document.body.createTextRange( );
sel.moveToElementText(theObjTable);
sel.select();
sel.execCommand("Copy");
oRange.Paste();
oWD.Application.Visible = true;
}








in html pageImport the content of the table in the webpage into the Word Can also be imported into excel



// -------------------------- ---------- 3 ---------------------------------- \
-- %>

**************************************** *************************************************** ****************
5. About the method of using js to directly convert the content in the web page into excel
**************** *************************************************** ***************************************
Copy code The code is as follows:

xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">


<script> <br>function out(){ <br>try{ <br>var elTable = document.getElementById("out"); <br>var oRangeRef = document.body.createTextRange(); <br>oRangeRef.moveToElementText( elTable ); <br>oRangeRef.execCommand( "Copy" ); <br>var oXL = new ActiveXObject("Excel.Application") <br>var oWB = oXL.Workbooks.Add ; <br>var oSheet = oWB.ActiveSheet ; <br>oSheet.Paste(); <br>oSheet.Cells.NumberFormatLocal = "@"; <br>oSheet.Columns("D:D").Select <br>oXL.Selection.ColumnWidth = 20 <br>//oSheet.Columns("A:A").Select <br>//oSheet.Columns("A").Width = 1000; <br>oXL.Visible = true; <br>oSheet = null; <br>oWB = null; <br>appExcel = null; <br>}catch(e){alert(e.description)} <br>} <br></script>















00001 0002
00001 000002
0003 00003




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn