你好,早安,
我这里有一段代码导入一个excel文件并在datagridview中填充它,就在这里。
Dim conn As OleDbConnection
Dim dts As DataSet
Dim excel As String
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim fi As New FileInfo(OpenFileDialog.FileName)
Dim FileName As String = OpenFileDialog.FileName
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';")
conn.Open()
Dim myTableName = conn.GetSchema("Tables").Rows(0)("TABLE_NAME")
Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("Select F3 as ItemCode,F4 as Description,F6 as RequestedQty,F9 as Remarks,F11 as Tag From [Official Transmittal Form$]", myTableName), conn)
dts = New DataSet
MyCommand.Fill(dts, 13, 878, "MyTable")
DataGridView1.DataSource = dts
DataGridView1.DataMember = "MyTable"
conn.Close()
' DataGridView1.Columns(7).ValueType = GetType(Double)
' DataGridView1.Columns(7).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(9).ValueType = GetType(Double)
' DataGridView1.Columns(9).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(10).ValueType = GetType(Double)
' DataGridView1.Columns(10).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(11).ValueType = GetType(Double)
' DataGridView1.Columns(11).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(12).ValueType = GetType(Double)
' DataGridView1.Columns(12).DefaultCellStyle.Format = "N2"
End If
但是我的问题是上面代码中的Official Transmittal Form
这个词。我的问题是如何只导入第一张纸?无论表名是什么。必须有一个实例,表单名称可能会更改我想要导入的第一个工作表。在我的程序中会有什么修改?
我尝试了这段代码,但它有错误
这是代码
Dim conn As OleDbConnection
Dim dts As DataSet
Dim excel As String
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim fi As New FileInfo(OpenFileDialog.FileName)
Dim FileName As String = OpenFileDialog.FileName
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Application = xlApp.Workbooks.Open(OpenFileDialog.FileName)
Dim SheetName As String = xlWorkBook.Worksheets(0).Name.ToString
excel = fi.FullName
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';")
conn.Open()
Dim myTableName = conn.GetSchema("Tables").Rows(0)("TABLE_NAME")
Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("Select F3 as ItemCode,F4 as Description,F6 as RequestedQty,F9 as Remarks,F11 as Tag From [" & SheetName & "$]", myTableName), conn)
dts = New DataSet
MyCommand.Fill(dts, 13, 878, "MyTable")
DataGridView1.DataSource = dts
DataGridView1.DataMember = "MyTable"
conn.Close()
' DataGridView1.Columns(7).ValueType = GetType(Double)
' DataGridView1.Columns(7).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(9).ValueType = GetType(Double)
' DataGridView1.Columns(9).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(10).ValueType = GetType(Double)
' DataGridView1.Columns(10).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(11).ValueType = GetType(Double)
' DataGridView1.Columns(11).DefaultCellStyle.Format = "N2"
' DataGridView1.Columns(12).ValueType = GetType(Double)
' DataGridView1.Columns(12).DefaultCellStyle.Format = "N2"
End If
这是错误
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorkbookClass' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
TYSM
答案 0 :(得分:0)
导入工作表以拖拽DataGridView。 所有数据的第一个DataGridView1。可行必须是假的。 第一个工作表name.viable的第二个DataGridView2必须为true。 你可以使用代码:
Try
DataG1.Rows.Clear()
Total2.Text = "0"
ProgressBar1.Visible = True
Dim MyConnection As System.Data.OleDb.OleDbConnection : ProgressBar1.Value = 10
Dim dataSet As System.Data.DataSet : ProgressBar1.Value = 20
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter : ProgressBar1.Value = 30
Dim path As String = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Exle.xlsx")
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection) : ProgressBar1.Value = 50
dataSet = New System.Data.DataSet : ProgressBar1.Value = 60
MyCommand.Fill(dataSet) : ProgressBar1.Value = 70
DataGridView1.DataSource = dataSet.Tables(0) : ProgressBar1.Value = 80
MyConnection.Close() : ProgressBar1.Value = 100
ProgressBar1.Visible = False
ProgressBar1.Value = 0
ProgressBar1.Visible = True
Dim I As Integer : ProgressBar1.Value = 10
For I = 0 To DataGridView1.Rows.Count - 2 : ProgressBar1.Value = 50
Dim ItemsName As DataGridViewTextBoxCell = DataGridView1.Rows(I).Cells(0)
Dim ItemsPrice As DataGridViewTextBoxCell = DataGridView1.Rows(I).Cells(1)
DataG1.Rows.Add(False, "", "", ItemsName.Value, "", "", "", "", "", "", "", "", ItemsPrice.Value)
Next : ProgressBar1.Value = 100
ProgressBar1.Visible = False
ProgressBar1.Value = 0
Total2.Text = DataG1.RowCount - 1
Catch ex As Exception
ProgressBar1.Visible = False
ProgressBar1.Value = 0
MsgBox(ex.Message.ToString)
End Try
答案 1 :(得分:0)
根据互联网的说法,OLEDB
不会保留Excel中工作表的索引和顺序。
如果您非常绝望,可以使用此解决方法:
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workook= xlApp.Workbooks.Open(OpenFileDialog.FileName)
Dim SheetName As String = xlWorkBook.Worksheets(1).Name.ToString
您现在拥有第一张工作表的名称,然后将其与查询连接。
Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("Select F3 as ItemCode,F4 as Description,F6 as RequestedQty,F9 as Remarks,F11 as Tag From [" & SheetName & "$]", myTableName), conn)