javascript 操作Word和Excel的实现代码

1.保存html页面到word


代码如下:

<HTML>
<HEAD>
<title>
</title>
</HEAD>
<body>
<form id="form">
<table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" >
<TR style="text-align : center;">
<TD>单元格1</TD>
<TD>单元格2</TD>
<TD>单元格3</TD>
<TD>单元格4</TD>
</TR>
<TR>
<TD colSpan=4 style="text-align : center;"><font color="red" face="Verdana">单元格合并</FONT></TD>
</TR>
</TABLE>
<BR>
<table id = "Test" width="100%">
<tr>
<td><font color="red">test</FONT></td>
</tr>
</table>
</form>
<input type="button" onclick="javascript:MakeWord();" value="导出页面到Word">
<SCRIPT LANGUAGE="javascript">
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();
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"); //存放到指定的位置注意路径一定要是“\\”不然会报错
}
</SCRIPT>
</body>
</html>

2.用JS生成WORD

代码如下:

<script>
function wordcontorl(){
alert("1111")
var WordApp=new ActiveXObject("Word.Application");

var wdCharacter=1
var wdOrientLandscape = 1
WordApp.Application.Visible=true;
var myDoc=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()         //插入段落

var myTable=myDoc.Tables.Add (WordApp.Selection.Range, 8,7) //8行7列的表格
//myTable.Style="网格型"
var aa = "我的列标题"

var TableRange; //以下为给表格中的单元格赋值
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()
</script>

***************************************************************************************************
3.遍历导出到word
***************************************************************************************************
1、遍历导出每个文本框内的内容。


代码如下:

<script language="javascript">
function OpenWord(){//导出word
var txt="txt";
for(i=0;i<table1.rows.length;i++)//遍历导出图表和文字
{
txt="txt"+jilu[i];
myRange =mydoc.Range(myRange.End-1,myRange.End);//设定起始点
var sel=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;//隐藏虚框
}
}
</script>

2、拷贝table1内的内容到word


代码如下:

<script language="javascript">
function OpenWord(){//导出word
Layer1.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.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;
}
</script>

**************************************************************************************************
4.操作excel
**************************************************************************************************


代码如下:

<%@ page c %>
<h1>content</h1>
<html>
<head>
<script language="javascript" type="text/javascript">
function MakeExcel() {
var 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),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 = "发卡时间";
var oTable = document.all['fors: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).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;
}
</script>
<link href="css/styles3.css" rel="stylesheet" type="text/css"/>
<title>ziyuanweihu</title>
</head>
<body>
<form id="fors" method="post" action="/WebModule/admins/card/showcard.faces" enctype="application/x-www-form-urlencoded">
<table id="fors:top" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="left"><img src="images/jiao1.gif" alt="" /></td>
<td class="topMiddle"></td>
<td class="right"><img src="images/jiao2.gif" alt="" /></td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="middleLeft"></td>
<td class="btstyle">
<table id="fors:sort" border="0" cellpadding="0" cellspacing="0" style="valign:center" width="100%">
<tbody>
<tr>
<td class="btstyle">
<input type="button" name="fors:_id7" value="生成excel文件" />
</td>
</tr>
</tbody>
</table>
<table id="fors:data" border="1" cellpadding="0" cellspacing="1" width="100%">
<thead>
<tr>
<th scope="col"><span id="fors:data:headerText1">卡号</span></th>
<th scope="col"><span id="fors:data:headerText2">密码</span></th>
<th scope="col"><span id="fors:data:headerText3">计费方式</span></th>
<th scope="col"><span id="fors:data:headerText4">有效天数</span></th>
<th scope="col">金额</th>
<th scope="col"><span id="fors:data:headerText6">所属服务项目</span></th>
<th scope="col"><span id="fors:data:headerText7">发卡时间</span></th>
</tr>
</thead>
<tbody>
<tr>
<td>h000010010</td>
<td>543860</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010011</td>
<td>683352</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010012</td>
<td>433215</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010013</td>
<td>393899</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010014</td>
<td>031736</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010015</td>
<td>188600</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010016</td>
<td>363407</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010017</td>
<td>175315</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010018</td>
<td>354437</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
<tr>
<td>h000010019</td>
<td>234750</td>
<td>计点</td>
<td></td>
<td>2.0</td>
<td>测试项目</td>
<td>2006-06-23 10:14:40.843</td>
</tr>
</tbody>
</table>
</td>
<td class="middleRight"></td>
</tr>
</tbody>
</table>
<table id="fors:bottom" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="left">
<img src="images/jiao3.gif" alt=""/>
</td>
<td class="bottomMiddle"> </td>
<td class="right">
<img src="images/jiao4.gif" alt=""/>
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="fors" value="fors" />
</form>
</body>
</html>

代码如下:

<%--
// ---------------------------------- 1 ---------------------------------- \\
<HTML>
<HEAD>
<TITLE>导出到excel</TITLE>
<META NAME="Generator" C>
<META NAME="Author" C>
<META NAME="Keywords" C>
<META NAME="Description" C>
</HEAD>
<script language="javascript">
function exportExcel(tableid) {
if(typeof(EXPORT_OBJECT)!="object") {
document.body.insertAdjacentHTML("afterBegin","<OBJECT style='display:none' classid='clsid:0002E510-0000-0000-C000-000000000046' id='EXPORT_OBJECT'></Object>");
}
with(EXPORT_OBJECT) {
DataType = "HTMLData";
HTMLData =tableid.outerHTML;
try {
ActiveSheet.Export("d:\\表格.xls", 0);
alert('成功导出EXCEL表格!');
}
catch (e) {
alert('导出EXCEL表格失败,请确定已安装Excel2000(或更高版本),并且没打开同名xls文件');
}
}
}
</script>
<BODY>
<table id="tableid" border="1">
<tr>
<td>第一个单元格</td>
<td>第二个单元格</td>
<td>第三个单元格</td>
</tr>
<tr>
<td>第四个单元格</td>
<td>第五个单元格</td>
<td>第六个单元格</td>
</tr>
</table>
<input type="button" value="导出EXCEL表格" >
</BODY>
</HTML>
// ---------------------------------- 2 ---------------------------------- \\
<!-- 导出到office2000版本的word或excel中 -->
<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软件!");
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;
}
</script>
<table id="theObjTable">
<tr>
<td>在html页面中</td>
<td>把网页中的表</td>
<td>格内容导入到</td>
<td>word中</td>
<td>也可以导入到excel</td>
</tr>
</table>
<input type="button" value="导入到excel" >
<input type="button" value="导入到word" >
// ---------------------------------- 3 ---------------------------------- \\
--%>

******************************************************************************************************
5.有关用js实现网页中的内容直接转化为excel的方法
******************************************************************************************************


代码如下:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type c>
<script>
function out(){
try{
var elTable = document.getElementById("out");
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText( elTable );
oRangeRef.execCommand( "Copy" );
var oXL = new ActiveXObject("Excel.Application")
var oWB = oXL.Workbooks.Add ;
var oSheet = oWB.ActiveSheet ;
oSheet.Paste();
oSheet.Cells.NumberFormatLocal = "@";
oSheet.Columns("D:D").Select
oXL.Selection.ColumnWidth = 20
//oSheet.Columns("A:A").Select
//oSheet.Columns("A").Width = 1000;
oXL.Visible = true;
oSheet = null;
oWB = null;
appExcel = null;
}catch(e){alert(e.description)}
}
</script>
</head>
<body>
<table id ='out'>
<tr>
<td x:str bgcolor="#FF0000">00001</td>
<td x:str>0002</td>
</tr>
<tr>
<td height=18 align=right style='height:13.5pt' x:str>00001</td>
<td align=right x:str>000002</td>
</tr>
<tr>
<td x:str>0003</td>
<td x:str>00003</td>
</tr>
</table>
<INPUT type="button" value="Button" id=button1 name=button1 >
</body>
</html>

(0)

相关推荐

  • javascript操作excel生成报表示例

    复制代码 代码如下: <html> <head><script language="javascript" type="text/javascript">function MakeExcel(){var i,j;    try {      var xls    = new ActiveXObject ( "Excel.Application" );     }    catch(e) {         al

  • javascript EXCEL 操作类代码

    复制代码 代码如下: ExcelOperation = function(){ this.oXL = null; this.oWB = null; this.oSheet = null; this.xlsRowCount = 0; //总记录数 this.excelFileName = null; this.currentRow = 2; //当前行 /** * 得到EXCEL表格中的总记录数 */ this.getRowCount = function(){ //oSheet.Range("C

  • 通过Javascript读取本地Excel文件内容的代码示例

    读取本地Excel文件内容的Javascript代码: 复制代码 代码如下: <script type="text/javascript">function read_excel(){ var filePath="D:\abcd9.com.xls"; //要读取的xls    var sheet_id=2; //读取第2个表    var row_start=3; //从第3行开始读取    var tempStr='';    try{       

  • JavaScript导出Excel实例详解

    本文实例讲述了JavaScript导出Excel的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>WEB页面导出为EXCEL文

  • javascript操作excel生成报表全攻略

    最近做一个项目,用到了javascript操纵excel以生成报表,下面是标有详细注解的实例 复制代码 代码如下: <html> <head> <script language="javascript" type="text/javascript"> </script><script language="javascript" type="text/javascript"&

  • 通过复制Table生成word和excel的javascript代码

    复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&qu

  • Javascript客户端将指定区域导出到Word、Excel的代码

    复制代码 代码如下: <table id = "PrintA" width="100%" border="1" cellspacing="0" cellpadding="0" bgcolor = "#61FF13"> <TR style="text-align : center;"> <TD>单元格A</TD> <

  • JavaScript将页面表格导出为Excel的具体实现

    复制代码 代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>导出Excel</TITLE> <script type="text/javascript"> var idTmr = ""; function killExcelProcess(appE

  • 用javascript将数据导入Excel示例代码

    网上收集的代码 <input type="button" name="out_excel" onclick="AutomateExcel();" value="导出到excel" class="notPrint"> 导出到Excel代码 <SCRIPT LANGUAGE="javascript"> <!-- function AutomateExcel()

  • JavaScript将Table导出到Excel实现思路及代码

    复制代码 代码如下: function copyToExcel(tableid) { //控制按钮 var btn = document.getElementById("copy"); btn.setAttribute("disabled", "true"); btn.setAttribute("value", "处理中..."); var curTbl = document.getElementById(

  • javascript Excel操作知识点

    1创建 var XLObj = new ActiveXObject("Excel.Application" ); var xlBook = XLObj.Workbooks.Add; //新增工作簿 var ExcelSheet = xlBook.Worksheets(1); //创建工作表 2.保存表格 ExcelSheet.SaveAs("C:\\TEST.XLS" ); 3.使 Excel 通过 Application 对象可见 ExcelSheet.Appli

随机推荐