CSDN博客

img rootcn

vbscript 和 javascript 生成 Excel 文件[office 2000].

发表于2004/9/15 14:52:00  5426人阅读

分类: 脚本语言

将这些代码另存为“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>

0 0

相关博文

我的热门文章

img
取 消
img