將這些代碼另存新檔“excel.htm“即可(在CSDN上運行不了)
================================================
ASP
<p>
< %
Response.ContentType = "application/vnd.ms-excel"
% >
</p>
vbscript:
<p>
<input type="button" onClick="vbscript:StartExcel()" value="vbscript1"/>
<input type="button" onClick="vbscript:StartExcel2()" value="vbscript2"/>
</p>
javascript:
<p>
<input type="button" onClick="javascript:jStartExcel()" value="javascript"/>
</p>
<p>
<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D3/S5A341.asp" target="_blank">Microsoft Excel Language Reference</a>—MSDN(推薦!)
</p>
引自:
<p>
<a href="http://builder.com.com/5100-6373-5072712.html" target="_blank">Explore ways to pass a Microsoft Excel file to the client side
</a>
</p>
<script language='VBScript'>
Dim Xcl
Sub StartExcel()
Set Xcl = CreateObject("Excel.Application")
Xcl.Visible = true
Set newBook = Xcl.Workbooks.Add
newBook.Worksheets.Add
newBook.Worksheets(1).Activate
newBook.Worksheets(1).Range("A1:B1000").NumberFormat = "0"
newBook.Worksheets(1).Range("A1:B1000").HorizontalAlignment = 1 ' 靠左對齊
newBook.Worksheets(1).Range("A1:D1").merge() ' 合併儲存格
newBook.Worksheets(1).Range("A1:D1").value="test" ' 賦值
newBook.Worksheets(1).Range("A1:D1").HorizontalAlignment=3 ' 置中對齊
newBook.Worksheets(1).Columns("A").columnwidth=50 ' 設定列寬
newBook.Worksheets(1).Rows(2).RowHeight = 40 ' 設定行高
newBook.Worksheets(1).Columns("A").WrapText = True
newBook.Worksheets(1).Columns("B").columnwidth=50
newBook.Worksheets(1).Columns("B").WrapText = true
newBook.Worksheets(1).Cells(2,1).Interior.ColorIndex="15" ' 設定字的顏色
newBook.Worksheets(1).Cells(2,1).Borders.LineStyle=1 ' 設定邊框 1為實線,2為虛線
newBook.Worksheets(1).Cells(3,2).Font.Name = "Verdana"
newBook.Worksheets(1).Cells(3,2).Font.Italic = true
newBook.Worksheets(1).Cells(3,2).HorizontalAlignment = 4 '靠右對齊
newBook.Worksheets(1).Cells(2,1).value="First Column, First Cell"
newBook.Worksheets(1).Cells(3,1).value="First Column, Second Cell"
newBook.Worksheets(1).Cells(2,2).value="Second Column, FIrst Cell"
newBook.Worksheets(1).Cells(3,2).value="Seond Column, Second Cell"
newBook.Worksheets(1).Name="My First WorkSheet"
'fname=Xcl.GetSaveAsFilename("Testing Excel Extraction.xls")
'if fname = "False" then
' fname=""
'end if
'newBook.SaveAs fname ' 檔案自動儲存為 Testing Excel Extracting.xls
'Xcl.Application.Quit ' 關閉 Excel
Set Xcl = nothing
End Sub
Sub StartExcel2()
Set Xcl = CreateObject("Excel.Application")
Xcl.Visible = true
Set newBook = Xcl.Workbooks.Add ' 填加一個新的工作表
newBook.Worksheets(1).Activate
newBook.Worksheets(1).Range("A1:B1000").NumberFormat = "0"
newBook.Worksheets(1).Cells(1,1).value="Value1"
newBook.Worksheets(1).Cells(2,1).value="Value2"
newBook.Worksheets(1).Cells(1,2).value="20"
newBook.Worksheets(1).Cells(2,2).value="10"
newBook.Worksheets(1).Cells(3,1).value="Total"
newBook.Worksheets(1).Cells(3,2).value="=Sum(B1:B2)"
newBook.Worksheets(1).Cells(4,1).value=CInt(newBook.Worksheets(1).Cells(3,2).value * 2)
newBook.Worksheets(1).Name="My Second WorkSheet" ' 工作表命名
'Xcl.Application.Quit ' 關閉 Excel
Set Xcl = nothing
End Sub
</script>
<script language="javascript">
function jStartExcel() {
var xls = new ActiveXObject ( "Excel.Application" );
xls.visible = true;
var newBook = xls.Workbooks.Add;
newBook.Worksheets.Add;
newBook.Worksheets(1).Activate;
xls.ActiveWorkBook.ActiveSheet.PageSetup.Orientation = 2;
xls.ActiveWorkBook.ActiveSheet.PageSetup.PaperSize = 5;
newBook.Worksheets(1).Columns("A").columnwidth=40; // 設定 A 列寬度為40px
newBook.Worksheets(1).Columns("A").WrapText = true; // 設定自動換行
newBook.Worksheets(1).Columns("B").columnwidth=50; // 設定 B 列寬度為50px
newBook.Worksheets(1).Columns("B").WrapText = true; // 設定自動換行
newBook.Worksheets(1).Range("A1:B1000").NumberFormat = "0";
newBook.Worksheets(1).Range("A1:B1000").HorizontalAlignment = -4131;
newBook.Worksheets(1).Range("A1:B1").merge(); // 合併儲存格地區 A1:D1
newBook.Worksheets(1).Range("A1:B1").value="My Title"; // 賦值
newBook.Worksheets(1).Range("A1:B1").HorizontalAlignment=3; //置中對齊
newBook.Worksheets(1).Range("A1:B1").Interior.ColorIndex=2; //背景色設為白色
newBook.Worksheets(1).Range("A1:B1").Font.Bold = true; // 粗體
newBook.Worksheets(1).Range("A1:B1").Font.ColorIndex = 3; // 字型為紅色
newBook.Worksheets(1).Cells(2,1).Interior.ColorIndex=15; // 背景色
newBook.Worksheets(1).Cells(2,1).value="Second Row, First Column";
newBook.Worksheets(1).Cells(2,1).Font.Italic=true;
newBook.Worksheets(1).Cells(2,1).Font.Bold=true;
newBook.Worksheets(1).Cells(2,2).value="Second Row, Second Column";
newBook.Worksheets(1).Cells(3,1).value="Third Row, First Column";
newBook.Worksheets(1).Cells(3,2).value="Third Row, Second Column";
newBook.Worksheets(1).Name="My First WorkSheet";
}
</script>