将数据从Excel导入GridView [英] Import Data from Excel to GridView

查看:79
本文介绍了将数据从Excel导入GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



以下是我的代码,我已成功将数据从Excel导入GridView。



问题是我有两个Column Hour&描述。当我导入到GridView时某些描述数据没有完全导入到GridView中,一些文本丢失了。意味着

在Excel中



小时说明

1 abcdefghijklmnopqrstuuudddd



在GridView



小时说明

1 abcdefghijklmno



文本大约64字左右,其中包括所有字符,如2 7/8



这里VB.Net代码



进口System.Data

进口System.Data.OleDb

进口System.IO



部分类WebForm1

继承System.Web.UI.Page

受保护的子Page_Load(ByVal sender As Object,ByVal e As EventArgs)

End Sub

受保护的子btnImport_Click(ByVal发送者为对象,ByVal e As EventArgs)

Dim connString As String =

Dim strFileType As String =。xls

Dim path__1 As String =c:\ccc.xls

'到Excel W的连接字符串orkbook

如果strFileType.Trim()=。xls那么

connString =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =& path__1& ;扩展属性=Excel 8.0; HDR =是; IMEX = 2

ElseIf strFileType.Trim()=。xlsx然后

connString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source =& path__1& ;扩展属性=Excel 12.0; HDR =是; IMEX = 2

结束如果

Dim query As String =SELECT [Hours(hr) ],[描述] FROM [sheet1 $]

Dim conn As New OleDbConnection(connString)

如果conn.State = ConnectionState.Closed那么

conn.Open()

结束如果

Dim cmd As New OleDbCommand(query,conn)

Dim da As New OleDbDataAdapter(cmd )

Dim ds作为新数据集()

da.Fill(ds)

grvExcelData.DataSource = ds.Tables(0)

grvExcelData.DataBind()

da.Dispose()

conn.Close()

conn.Dispose()

结束次级

结束班级





Dear,

Below is my code where i'm successfully import data from Excel to GridView.

The Problem is I have two Column Hour & Description. When i import to an GridView Some of the Description data is not fully imported to an GridView some of the Text is missing. Means
In Excel

Hour Description
1 abcdefghijklmnopqrstuuudddd

In GridView

Hour Description
1 abcdefghijklmno

The Text is quit big around 64 words which inludes all characters also like 2 7/8"

Here VB.Net Code

Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim connString As String = ""
Dim strFileType As String = ".xls"
Dim path__1 As String = "c:\ccc.xls"
'Connection String to Excel Workbook
If strFileType.Trim() = ".xls" Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path__1 & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
ElseIf strFileType.Trim() = ".xlsx" Then
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path__1 & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End If
Dim query As String = "SELECT [Hours (hr)],[Description] FROM [sheet1$]"
Dim conn As New OleDbConnection(connString)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim cmd As New OleDbCommand(query, conn)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
grvExcelData.DataSource = ds.Tables(0)
grvExcelData.DataBind()
da.Dispose()
conn.Close()
conn.Dispose()
End Sub
End Class


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication49.WebForm1" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read and Display Data From an Excel File (.xsl or .xlsx) in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Please Select Excel File: </b>
<asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
<asp:GridView ID="grvExcelData" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>

推荐答案



Dim conn如新的OleDbConnection(connString)

如果conn.State = ConnectionState.Closed那么

conn.Open()

结束如果

Dim cmd As New OleDbCommand(query,conn)

Dim da As New OleDbDataAdapter(cmd)

Dim ds As New DataSet()

da.Fill(ds)

grvExcelData.DataSource = ds.Tables(0)

grvExcelData.DataBind()

da。 Dispose()

conn.Close()

conn.Dispose()

End Sub

End Class < br $>




"
Dim conn As New OleDbConnection(connString)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim cmd As New OleDbCommand(query, conn)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
grvExcelData.DataSource = ds.Tables(0)
grvExcelData.DataBind()
da.Dispose()
conn.Close()
conn.Dispose()
End Sub
End Class


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication49.WebForm1" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read and Display Data From an Excel File (.xsl or .xlsx) in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Please Select Excel File: </b>
<asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
<asp:GridView ID="grvExcelData" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>


这篇关于将数据从Excel导入GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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