Protected Sub btnUpload_Click(sender As Object, e As EventArgs) If FileUpload1.HasFile Then Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName) Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath") Dim FilePath As String = Server.MapPath(FolderPath + FileName) FileUpload1.SaveAs(FilePath) Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text) End If End Sub Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String) Dim conStr As String = "" Select Case Extension Case ".xls" conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString Exit Select Case ".xlsx" conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString Exit Select End Select conStr = String.Format(conStr, FilePath, isHDR) Dim connExcel As New OleDbConnection(conStr) Dim cmdExcel As New OleDbCommand() Dim oda As New OleDbDataAdapter() Dim dt As New DataTable() cmdExcel.Connection = connExcel connExcel.Open() Dim dtExcelSchema As DataTable dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing) Dim SheetName As String = dtExcelSchema.Rows(0)("TABLE_NAME").ToString() connExcel.Close() connExcel.Open() cmdExcel.CommandText = "SELECT * From [" & SheetName & "]" oda.SelectCommand = cmdExcel oda.Fill(dt) connExcel.Close() Dim GridView1 As GridView = New GridView GridView1.AutoGenerateColumns = False For i As Integer = 0 To dt.Columns.Count - 1 Dim boundfield As BoundField = New BoundField boundfield.DataField = dt.Columns(i).ColumnName.ToString() boundfield.HeaderText = dt.Columns(i).ColumnName.ToString() GridView1.Columns.Add(boundfield) Next GridView1.DataSource = dt GridView1.DataBind() Panel1.Controls.Add(GridView1) End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)