此代码中的问题出在哪里?请帮助我! [英] Where's the problem in this code? Plese help me !

查看:75
本文介绍了此代码中的问题出在哪里?请帮助我!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误消息:
对象引用未设置为对象的实例.在此行:

Error message :
Object reference not set to an instance of an object. at this line :

Dim r As DataRow = ds.Tables("Sheet1").NewRow



我想将数据从excel文件从A列导入到H列?



i want to import data from excel file from column A to column H ??

Dim SourceBookPath As String = Nothing

Me.OpenFileDialog1.Filter = "Excel files (*.xls,*.xlsx)|*.xls;*.xlsx"

Me.OpenFileDialog1.ShowDialog()

If Not Me.OpenFileDialog1.FileName Is Nothing Then

SourceBookPath = Me.OpenFileDialog1.FileName End If

'=======================================================================================================================

Dim MsExcel = CreateObject("Excel.Application")

Dim ExcelBook = MsExcel.Workbooks.Open(SourceBookPath)

Dim con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SourceBookPath & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1"

Dim str As String = "select * from Sheet1"

Dim ds As New DataSet

Dim I, J As Integer

Dim lastRow As Long = 0

lastRow = MsExcel.Range("A15").Find("*", MsExcel.Range("A15"), XlFindLookIn.xlValues, XlSearchOrder.xlByRows, XlSearchDirection.xlPrevious).row

For I = 8 To lastRow

Dim r As DataRow = ds.Tables("Sheet1").NewRow

For J = 1 To 8 r(J - 1) = MsExcel.Cells(I, J).Value

Next J

ds.Tables("Sheet1").Rows.Add(r)

Next I

DataGridView1.DataSource = ds.Tables("Sheet1") 

推荐答案

在Visual Studio调试器(f5)中运行代码,它将停止并向您显示问题所在.

ds变量很可能是null,应首先初始化.

查看您的代码,您应该连接到excel文件并首先填充数据集.
Run your code in the Visual Studio Debugger (f5) and it will stop and show you where the problem is.

Most probably the ds variable is null and should be initialized first.

Looking at your code you should connect to the excel file and fill the dataset first.


这将从excel中获取数据并将其放入数据集中.然后,您可以做任何您想做的事.

This will get the data from the excel and put it in the dataset. Then you can do whatever you want to do with it.

Dim MyConnection As System.Data.OleDb.OleDbConnection
       Dim myPath As String = filepath
       Try
           Dim DS As System.Data.DataSet
           Dim strPath, strSheet As String
           strPath = filepath
           strSheet = "Sheet1"


           Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
           MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filepath & ";Extended Properties=''Excel 12.0 Xml;HDR=YES''")

           '' Select the data from Sheet1 of the workbook.

           MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & strSheet & "


", MyConnection) DS = New System.Data.DataSet MyCommand.Fill(DS) If Not DS Is Nothing Then Return DS Else Return Nothing End If Catch exp As Exception MsgBox(exp.Message) Return Nothing End Try
", MyConnection) DS = New System.Data.DataSet MyCommand.Fill(DS) If Not DS Is Nothing Then Return DS Else Return Nothing End If Catch exp As Exception MsgBox(exp.Message) Return Nothing End Try


这篇关于此代码中的问题出在哪里?请帮助我!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆