将Excel工作表导入Visual Studio 2005中的DataGridView中 [英] Import excel sheet into datagridview in visual studio 2005

查看:257
本文介绍了将Excel工作表导入Visual Studio 2005中的DataGridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下错误
Comma, '')'', or a valid expression continuation expected.

该代码是将Excel工作表导入Visual Studio 2005中的datagridview.
我在此行中遇到错误,请帮助我

I got below error
Comma, '')'', or a valid expression continuation expected.

The code is to import excel sheet into datagridview in visual studio 2005.
I got error in this line help me

DataGridViewConverter.ExportToDataGridView(workbook.Worksheets.ActiveWorksheet, Me.DataGridView1, New ExportToDataGridViewOptions() With {.ColumnHeaders = True})



我尝试过的事情:



What I have tried:

DataGridView1.DataSource = Nothing
            DataGridView1.Visible = True
            dv = New DataView

            OpenFileDialog1.Filter = "Excel files (*.xls)|*.xls"
            OpenFileDialog1.FileName = "*.xls"
            OpenFileDialog1.FilterIndex = 2
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.FILENAME_TextBox.Text = OpenFileDialog1.FileName

                Dim workbook = ExcelFile.Load(OpenFileDialog1.FileName)
                ' DataGridViewConverter.ExportToDataGridView(workbook.Worksheets.ActiveWorksheet, Me.DataGridView1, New ExportToDataGridViewOptions() With {.ColumnHeaders = True})
                DataGridViewConverter.ExportToDataGridView(workbook.Worksheets.ActiveWorksheet,Me.DataGridView1,New ExportToDataGridViewOptions() With {.ColumnHeaders = True})

推荐答案

检查文档: DataGridViewConverter.ExportToDataGridView方法 [从Windows窗体应用程序将Excel导入/导出到DataGridView [
Check the documentation: DataGridViewConverter.ExportToDataGridView Method[^]. At the bottom of page, you''ll find a link to sample code: Import / Export Excel to DataGridView from Windows Forms application[^].

First error is here:
Dim workbook = ExcelFile.Load(OpenFileDialog1.FileName)


应该是:


it should be:

Dim ef As ExcelFile = ExcelFile.Load(openFileDialog.FileName)


然后:


then:

DataGridViewConverter.ExportToDataGridView(ef.Worksheets.ActiveWorksheet, Me.DataGridView1, New ExportToDataGridViewOptions() { ColumnHeaders = True })



另一方面,还有其他几种方法可以使用datagridview投标Excel工作表,请参见:
如何:将数据绑定到Windows窗体DataGridView控件Microsoft文档 [ DataGridView.DataSource属性(System.Windows.表单) [



On the other hand, there''s few other ways to bid Excel sheet with datagridview, see:
How to: Bind Data to the Windows Forms DataGridView Control | Microsoft Docs[^]
DataGridView.DataSource Property (System.Windows.Forms)[^]


这篇关于将Excel工作表导入Visual Studio 2005中的DataGridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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