JavaScriptでActiveXObjectを利用してExcelを制御する方法
htmlのテーブルをそのままコピーします.
一般的な属性の方法:
1. EXCEL
function CopyTable(tableid)
{
// EXCEL
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
// AX excel
var oWB = oXL.Workbooks.Add();
// workbook
var oSheet = oWB.ActiveSheet;
// sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
// TextRange
sel.select();
// TextRange
sel.execCommand("Copy");
// TextRange
oSheet.Paste();
// EXCEL
oXL.Visible = true;
// excel
}
ActiveXObjectを利用してエクセルをエクスポートします.<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>
ソース:http://www.jb51.net/article/20605.htm 一般的な属性の方法:
JavaScript ActiveXObject Excel
1.
var XLObj = new ActiveXObject("Excel.Application");// Excel
var xlBook = XLObj.Workbooks.Add; //
var ExcelSheet = xlBook.Worksheets(1); //
2.
ExcelSheet.SaveAs("C:\\TEST.XLS");
3. Excel Application
ExcelSheet.Application.Visible = true; ExcelSheet.Visible=true;
4.
xlBook.PrintOut; :ExcelSheet.PrintOut;
5.
xlBook.Close(savechanges=false); ExcelSheet.Close(savechanges=false);
6.
ExcelSheet.Application.Quit() XLObj.Quit(); XLObj=null;
7.
ExcelSheet.ActiveSheet.PageSetup.LeftMargin= 2/0.035; // 2
ExcelSheet.ActiveSheet.PageSetup.RightMargin = 3/0.035; // 3 ,
ExcelSheet.ActiveSheet.PageSetup.TopMargin = 4/0.035; // 4 ,
ExcelSheet.ActiveSheet.PageSetup.BottomMargin = 5/0.035; // 5
ExcelSheet.ActiveSheet.PageSetup.HeaderMargin = 1/0.035; // 1
ExcelSheet.ActiveSheet.PageSetup.FooterMargin = 2/0.035; // 2
ExcelSheet.ActiveSheet.PageSetup.CenterHeader = " ";
ExcelSheet.ActiveSheet.PageSetup.LeftHeader = " ";
ExcelSheet.ActiveSheet.PageSetup.RightHeader = " ";
ExcelSheet.ActiveSheet.PageSetup.CenterFooter = " ";
ExcelSheet.ActiveSheet.PageSetup.LeftFooter = " ";
ExcelSheet.ActiveSheet.PageSetup.RightFooter = " ";
8. , * , ,
ExcelSheet.ActiveSheet.Cells(row,col).Value = " "; //
ExcelSheet.ActiveSheet.Cells(row,col).Borders.Weight = 1; // *()
ExcelSheet.ActiveSheet.Cells(row,col).Interior.ColorIndex = 1; // *(1- ,2- ,3- ,4- ,5- ,6- ,7- ,8- ,9- .. )
ExcelSheet.ActiveSheet.Cells(row,col).Interior.Pattern = 1; // *(1- ,2- ,3- ,4- ,5- ,6- .. )
ExcelSheet.ActiveSheet.Cells(row,col).Font.ColorIndex = 1; // *( )
ExcelSheet.ActiveSheet.Cells(row,col).Font.Size = 10; // 10 *
ExcelSheet.ActiveSheet.Cells(row,col).Font.Name = " "; // *
ExcelSheet.ActiveSheet.Cells(row,col).Font.Italic = true; // *
ExcelSheet.ActiveSheet.Cells(row,col).Font.Bold = true; // *
ExcelSheet.ActiveSheet.Cells(row,col).ClearContents; // *
ExcelSheet.ActiveSheet.Cells(row,col).WrapText=true; // *
ExcelSheet.ActiveSheet.Cells(row,col).HorizontalAlignment = 3; // * (1- ,2- ,3- ,4- ,5- 6- ,7- ,8- )
ExcelSheet.ActiveSheet.Cells(row,col).VerticalAlignment = 2; // *(1- ,2- ,3- ,4- ,5- )
// , :
ExcelSheet.ActiveSheet.Rows(row).
ExcelSheet.ActiveSheet.Columns(col).
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow). // Rows("1:5") 1 5
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol). // Columns("1:5") 1 5
// :
XLObj.Range(startcell+":"+endcell).Select;
// Range("A2:H8") A 2 H 8
XLObj.Selection.
//
XLObj.Range(startcell+":"+endcell).MergeCells = true;
// Range("A2:H8") A 2 H 8
:XLObj.Range("A2",XLObj.Cells(8, 8)).MergeCells = true;
9.
ExcelSheet.ActiveSheet.Columns(startcol+":"+endcol).ColumnWidth = 22;// firstcol stopcol 22
ExcelSheet.ActiveSheet.Rows(startrow+":"+endrow).RowHeight = 22;// firstrow stoprow 22
Var myrange oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(2,6));
myRange.Columns.AutoFit()
myRange.Rows.AutoFit()//
ソース:http://www.cnblogs.com/ForeverSoft/archive/2011/08/31/2160658.html