-------------------------------------------
使用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()//好像不太管用
//設置該區域爲自適應高度和寬度
使用JavaScript將表格內容導出到Excel的方式
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可見屬性
}
2.把單元格內容拷貝到Excel,並設置單元格格式.
function method2(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 Lenr = curTbl.rows.length;
var y=document.getElementById('dropYear');
var m=document.getElementById('dropMonth');
//取得表格行數
oSheet.Cells( 1, 1).value='商戶名稱:工商銀行商城';
oSheet.Range(oSheet.Cells(1,1),oSheet.Cells(1,6)).Merge()
oSheet.Cells(2,1).value=日期; '+y.options[y.selectedIndex].value+'-'+m.options[m.selectedIndex].value;
oSheet.Range(oSheet.Cells(2,1),oSheet.Cells(2,6)).Merge()
oSheet.Cells( 3, 1).value='銷售狀況'
oSheet.Range(oSheet.Cells(3,1),oSheet.Cells(3,6)).Merge()
oSheet.Cells( 3, 1).HorizontalAlignment=3;
oSheet.Cells( 3, 1).Font.Size=14;
oSheet.Cells( 3, 1).Font.Bold = true;
oSheet.Range(oSheet.Cells(4,1),oSheet.Cells(4,6)).Merge()
oSheet.Cells(4,1).value=document.getElementById('lbMessage').innerText;
for (i = 0; i < Lenr; i++)
{
var Lenc = curTbl.rows(i).cells.length;
//取得每行的列數
for (j = 0; j < Lenc; j++)
{
oSheet.Cells(i + 5, j + 1).value = curTbl.rows(i).cells(j).innerText;
//賦值
}
}
oSheet.Cells(i+6,2).value=document.getElementById('hidSum').value;
oSheet.Columns.AutoFit();
oSheet.Rows.AutoFit();
oSheet.Rows(4).RowHeight = 30;
oXL.Visible = true;
//設置excel可見屬性
oXL.Quit();
oXL = null;
idTmr = window.setInterval("Cleanup();",1);//強制釋放資源
}
function Cleanup()//清除Excel資源
{
window.clearInterval(idTmr);
CollectGarbage();
}
使用JavaScript按照表格格式複製內容到剪切板
1.在Excel中進行粘貼
function copytable()
{
var content=''
var tb=document.getElementById('GVmain');
var rows=tb.rows;
for(var i=0;i<rows.length;i++)
{
var cells=rows[i].cells;
for(var j=0;j<cells.length;j++)
{
content +=cells[j].innerText.toString()+'\t';
}
content +='\n';
}
if(content!='')
{
clipboardData.setData("text",content);
alert("複製成功!")
}
}字體