请各位高手帮我看看。
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
Dim FileName As String
fileDialog.Filter = "Microsoft Excel files (*.xls)|*.xls"
If fileDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
If fileDialog.FileName = Nothing Then
MsgBox("请选择要导入的excel文件", , "提示")
End If
FileName = fileDialog.FileName
xlBook = excel.Application.Workbooks.Open(FileName)
xlSheet = xlBook.Application.Worksheets(1)
Dim col As Integer = 0
Dim i As Integer = 2
Me.dataGridView1.Rows.Clear()
Do While i < 5
dataGridView1.Rows.Add()
dataGridView1.Rows(col).Cells(0).Value = xlSheet.Cells(i, 1).ToString
dataGridView1.Rows(col).Cells(1).Value = xlSheet.Cells(i, 2).ToString
dataGridView1.Rows(col).Cells(2).Value = xlSheet.Cells(i, 3).ToString
i = i + 1
col = col + 1
Loop
excel.ActiveWorkbook.Close(False)
xlSheet = Nothing
xlBook = Nothing
excel = Nothing
If dataGridView1.Rows.Count >= 1 Then
MessageBox.Show("导入成功")
Else
MessageBox.Show("导入失败")
End If
End Sub
运行结果是,gridviewview中的数据全部显示“System.__ComObject”,我实在找不出来原因,希望各位能帮帮忙,分有点少,希望不要嫌弃哈。
VB.net中excel导入数据到Datagridview
xlsheet.cells(1,2)tostring 改为xlsheet.cells(1,2).value 其他也是
vb.net怎样把excel内容导入到datagridview
用一个list来显示EXCEL中的所有表,选择指定表名,打开EXCEL到DG ———如下:引用ADODB 2.8 导入:Imports System.Data Imports System.Data.Odbc Imports System.Data.OleDb 申明 Private Excelpath As String Public conn As New ADODB.Connection Public rs As New ADODB.Recordset 创建conn Public ...
C#中如何实现从 excel导入数据,并绑定到gridview上?
string connectionString;connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsFilePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";OleDbConnection conn = new OleDbConnection(connectionString);String strQuery = "SELECT * FROM [Sheet1$]"; \/\/可以更改...
vb.net中从excel读取数据到DataGridView中的数据显示问题
在DataGridView中的日期时间类型原本是像第三列一样的,至于其他列是因为经过设置或处理才没有显示时间。
vb.net从datagridview内容导入到Excel,并在Excel的第三行开始显示_百度...
直接在工作表的首行追加2行就可以了啊,你也可以直接选择我下面的函数 Public Function Export_Excel(ByVal dgv As DataGridView) As Boolean Try If dgv.Rows.Count <= 0 Then '如果没有记录就退出 MessageBox.Show("没有记录可以导出", "没有可以导出的项目", MessageBoxButtons.OK, Me...
vb.net dataGridView导出EXCEL
Public Function daochu(ByVal x As DataGridView) As Boolean '导出到Excel函数 Try If x.Rows.Count <= 0 Then '判断记录数,如果没有记录就退出 MessageBox.Show("没有记录可以导出", "没有可以导出的项目", MessageBoxButtons.OK, MessageBoxIcon.Information)Return False Else '如果有记录就...
C# 将excel导入到DataGridView上 (可以对数据进行操作)
需要把excel导入到dataset或者datatable中,然后转换成你要的值
ASP.net读取Excel数据到GridView中
首先创建一个aspx文件,在页面中加入一个Button和一个DataGrid控件。在工程的引用中添加Excel引用,并将测试的电子表格文件放到D盘中。在CS文件中的Button1_Click事件中输入如下代码:private void Button1_Click(object sender, System.EventArgs e){ string excelFilePath=@"D:\\Book1.xls";\/\/D:\\Book...
VB.NET怎么将DataGridview里的内容导出到EXCEL去
Dim table As New System.Data.DataTable table = DataSet11.Tables("INVMATLISTA") '填充表 Dim row As DataRow '定义row为表格的行 Dim col As DataColumn '定义col为表格的列 '把表格的每一列写到EXCEL去 For Each col In table.Columns colindex = colindex + 1 xlapp.Ce...
利用vs2010、C#做一个EXCEL与dataGridView的数据相互导入导出!!
导入:public DataTable ReadDataByExcel(string fileFullPath) {\/\/ strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileFullPath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1;'";\/\/只适合xls后缀 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data...