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文件。
[解决办法]
这个你参考
- VB.NET code
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
[解决办法]
[解决办法]
生成/读取(反向更新数据库) Excel文件(示例代码下载):
http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx
[解决办法]
dim xlApp = New Excel.ApplicationClass
'xlApp.UserName = Operator
xlApp.Caption = "Enquire & Hotline information "
If xlApp Is Nothing Then
MessageBox.Show("ERROR: EXCEL couldn't be started", "output to Excel", MessageBoxButtons.OK, MessageBoxIcon.Error)
Console.WriteLine("ERROR: EXCEL couldn't be started!")
Return False
End If
If File.Exists(strFile) Then File.Delete(strFile)
If Not File.Exists(strTemplate) Then
dim xlBook = xlApp.Workbooks.Add()
dim xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Activate()
xlSheet.Columns("a:g").Font.Size = 8
xlSheet.Columns("a:g").font.name = "Arial"
xlSheet.Columns("a").ColumnWidth = 25
xlSheet.Columns("b").ColumnWidth = 12
xlSheet.Columns("c").ColumnWidth = 8
xlSheet.Columns("d").ColumnWidth = 12
xlSheet.Columns("e").ColumnWidth = 10
xlSheet.Columns("f").ColumnWidth = 10
'initiate table head
'xlSheet.Cells(2, 1).value = "No."
'xlSheet.Cells(2, 1).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 1).value = "Department/Branch/Subsidiary/Associate"
xlSheet.Cells(2, 1).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 2).value = "Section Name"
xlSheet.Cells(2, 2).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 3).value = "Sub Section Name"
xlSheet.Cells(2, 3).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 4).value = "Description"
xlSheet.Cells(2, 4).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 5).value = "Telephone Number"
xlSheet.Cells(2, 5).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Cells(2, 6).value = "Fax Number"
xlSheet.Cells(2, 6).Borders.LineStyle = Excel.XlLineStyle.xlContinuous
xlSheet.Range(xlSheet.Cells(2, 1), xlSheet.Cells(2, 6)).Font.Bold = True
xlSheet.Rows(1).RowHeight = 30
xlSheet.Cells(1, 1).value = "BEA Phonebook - Enquiries / Hotline Information"
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 6)).MergeCells = True
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 6)).Font.Size = 16
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 6)).Font.Bold = True
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 6)).HorizontalAlignment = XlHAlign.xlHAlignCenter
xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(2, 6)).VerticalAlignment = XlVAlign.xlVAlignCenter '垂直居中 xlSheet.Columns(2).HorizontalAlignment = XlVAlign.xlVAlignCenter '1列水平居中对齐