作者:乔山办公网日期:
返回目录:excel表格制作
VB.NET2008代码:
Dim xlapp1 As Excel.Application
Dim xlworksheet As Excel.Worksheet
Dim xlworkbk As Excel.Workbook
xlapp1 = New Excel.Application
xlworkbk = xlapp1.Workbooks.Open("Excel文件名百")
xlworksheet = xlworkbk.Worksheets("Sheet名")
xlworksheet.Cells.Item(行编号度, 列编号).value ="需要输入的数据问"
xlworkbk.Close()
另外,xlworksheet.UsedRange.Rows.Count是最大行数,可以用这个算行答编号。
注意,专需要引用Microsoft Excel Object Library(在COM的那个选项属卡里)。
首先,工程-引用 microsoft Excel 11.0 )bject Library
然后代码如下:抄
Dim xlApp As Object
Dim xlBook As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
With xlApp
xlApp.Visible = False
.Rows(1).Font.Bold = True '设置第一行为粗体
.Cells(10, 10).Value="姓名"
.Application.Visible = True
End With
Set xlApp = Nothing
Set xlBook = Nothing
如想了解其他操作excel的代码,可以在excel中,点击菜单的 工具——宏——录制新宏袭。
然后你手动对excel操作,操作完成后停止录制。再点击 工具——宏——visual basic编辑器,zd找到刚才录制的宏,就可以看到刚才对excel所做的操作对应的vb代码了。
在Ex.save之前加一行
Ex.displayalerts=false
就可以了
最好在Ex.save之后改回去
Ex.displayalerts=true
现在工程里添加引用Microsoft.Office.Interop.Excel,然后可以使用如下代码输出。
Dim oXl As Excel.Application = New Excel.Application()
Dim oWb As Excel.Workbook
Dim oWs As Excel.Worksheet
On Error GoTo Morn
oWb = oXl.Workbooks.Add()
oWs = oWb.Worksheets(1)
With oWs
.Cells._Default(1, 1).Value ="a"
.Cells._Default(1, 2).Value ="b"
.Cells._Default(1, 3).Value ="c"
.Cells._Default(1, 4).Value ="d"
.Cells._Default(2, 1).Value ="32"
.Cells._Default(2, 2).Value ="1"
.Cells._Default(2, 3).Value ="90"
.Cells._Default(2, 4).Value ="合格"
End With
oWs.Parent.Names.Add("CostRange", "=" & "A1:B39")
oWs.SaveAs(sFileName)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
If MsgBox("已将数据输出到Excel文件中!e69da5e6ba90e799bee5baa6e79fa5e98193332 现在打开该文件?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "已完成") = MsgBoxResult.No Then
oXl.Quit()
Else
oXl.Visible = True
End If
oXl = Nothing
oWs = Nothing
oWb = Nothing