vb.net读取excel
VB6中这样使用,请问在VB.net下怎样使用?
Public xlApp As New Excel.Application
Public xlBook As Excel.Workbook
Public xlSheet As Excel.Worksheet
.net下没这个Excel.Application
[解决办法]
要先从COM组件的Excel产生一个DLL
过程:引用->COM 选择 Microsoft Excel 9.0 Object Library,这个是Excel 2000,
将在项目中中产生Excel、Office、VBIDE三个dll引用,当然在工程的bin下也有响应的.dll文件。
[解决办法]
这个你参考
Public Class Form1 Dim i As Short Dim j As Short Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlBook As Microsoft.Office.Interop.Excel.Workbook Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet Dim xlsName As String Private Sub Botton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click xlsName = My.Application.Info.DirectoryPath & "\temp.xls" '用 Add方法 创建新的工作簿和工作表对象。 xlApp = New Microsoft.Office.Interop.Excel.Application xlBook = xlApp.Workbooks.Open(xlsName) 'Set xlBook = xlApp.Workbooks.Add xlSheet = xlBook.Worksheets("work") xlSheet.Name = "work" ' If Err.Number = 1004 Then ' xlSheet.Name = formatdatetime(DateTimePicker1, "yyyymm") ' MsgBox (DateTimePicker1 & "工作表已存在将自动产生sheel系列工作表") ' Err.Number = 0 ' End If '将data3的值赋给Microsoft Excel 单元。 'sqlString = "select * from worktable" 'If Conn.State = 1 Then ' If Rs.State = 1 Then Rs.Close() ' Rs.Open(sqlString, Conn, 1, 1) 'Else ' Conn.Open(conString) ' Rs.Open(sqlString, Conn, 1, 1) 'End If For i = 0 To 5 For j = 0 To 7 xlSheet.Cells._Default(i + 1, j + 1).Value = i & "," & j Next j Next i 'xlSheet.Cells(3, 1).Formula = "=R1C1 + R2C1" '保存工作表 'xlSheet.StandardWidth = 2 'xlSheet.SaveAs (App.path & "\" & formatdatetime(DateTimePicker1, "yyyy年mm月值班表") & formatdatetime(Now(), "hhmmss") & ".xls") xlBook.Save() xlBook.Close() '用 Quit 方法关闭 Microsoft Excel xlApp.Quit() '释放对象 Dim pro As Process For Each pro In Process.GetProcesses If pro.ProcessName = "EXCEL" Then pro.Kill() End If Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim xlsName As String = My.Application.Info.DirectoryPath & "\" & TextBox1.Text '用 Add方法 创建新的工作簿和工作表对象。 xlApp = New Microsoft.Office.Interop.Excel.Application 'xlBook = xlApp.Workbooks.Open(xlsName) xlBook = xlApp.Workbooks.Add xlSheet = xlBook.Worksheets(1) xlSheet.Name = "work" For i = 0 To 5 For j = 0 To 7 xlSheet.Cells._Default(i + 1, j + 1).Value = i & "" & j Next j Next i 'xlSheet.Cells(3, 1).Formula = "=R5C5 + R3C3"'RnCk,n为行值 k为列值可直接编辑 'xlSheet.Cells(3, 2).value = "=R5C5 + R3C3" 'xlSheet.Cells._Default(3, 4).Value = "=R5C5 + R3C3" '保存工作表 'xlSheet.StandardWidth = 2 'xlSheet.SaveAs (App.path & "\" & formatdatetime(DateTimePicker1, "yyyy年mm月值班表") & formatdatetime(Now(), "hhmmss") & ".xls") xlBook.Saved = True '保存时不提示 xlBook.SaveCopyAs(xlsName) '直接覆盖不提示 'xlBook.SaveAs(xlsName)'覆盖时提示 'MsgBox(xlsName) xlBook.Close() '用 Quit 方法关闭 Microsoft Excel xlApp.Quit() '释放对象 Dim pro As Process For Each pro In Process.GetProcesses If pro.ProcessName = "EXCEL" Then pro.Kill() End If Next MsgBox("工作表已经保存完毕!") End SubEnd Class
[解决办法]