Import XLS 97-2003 Excel files was excellent by is there code to do the same job as code XLSX Upload using the code for XLS shown under the XLSX.
XLS Upload
Protected Sub Upload(sender As Object, e As EventArgs)
'Upload and save the file
Dim excelPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(excelPath)
Dim connString As String = String.Empty
Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Select Case extension
Case ".xls"
'Excel 97-03
connString = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
Exit Select
Case ".xlsx"
'Excel 07 or higher
connString = ConfigurationManager.ConnectionStrings("Excel07+ConString").ConnectionString
Exit Select
End Select
connString = String.Format(connString, excelPath)
Using excel_con As New OleDbConnection(connString)
excel_con.Open()
Dim sheet1 As String = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing).Rows(0)("TABLE_NAME").ToString()
Dim dtExcelData As New DataTable()
'[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
dtExcelData.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Salary", GetType(Decimal))})
Using oda As New OleDbDataAdapter((Convert.ToString("SELECT * FROM [") & sheet1) + "]", excel_con)
oda.Fill(dtExcelData)
End Using
excel_con.Close()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(conString)
Using sqlBulkCopy As New SqlBulkCopy(con)
'Set the database table name
sqlBulkCopy.DestinationTableName = "dbo.tblPersons"
'[OPTIONAL]: Map the Excel columns with that of the database table
sqlBulkCopy.ColumnMappings.Add("Id", "PersonId")
sqlBulkCopy.ColumnMappings.Add("Name", "Name")
sqlBulkCopy.ColumnMappings.Add("Salary", "Salary")
con.Open()
sqlBulkCopy.WriteToServer(dtExcelData)
con.Close()
End Using
End Using
End Using
End Sub
XLSX Upload
For Each postedfile As HttpPostedFile In FileUpload1.PostedFiles
Dim txt = postedfile
Using workBook As New XLWorkbook(txt.InputStream)
fn = fn & postedfile.FileName & " "
filecnt = filecnt + 1
'Read the first Sheet from Excel file.
Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
'Loop through the Worksheet rows.
Dim firstRow As Boolean = True
For Each row As IXLRow In workSheet.Rows()
'Use the first row to add columns to DataTable.
If firstRow Then
If filecnt = 1 Then
For Each cell As IXLCell In row.Cells()
dt.Columns.Add(cell.Value.ToString())
HeadDesc.Add(cell.Value.ToString())
Next
End If
firstRow = False
Else
'Add rows to DataTable.
dt.Rows.Add()
Dim i As Integer = 0
For Each cell As IXLCell In row.Cells(1, dt.Columns.Count)
' For Each cell As IXLCell In row.Cells()
dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
i += 1
Next
End If
Next
End Using
Next
GridView1.DataSource = dt
GridView1.DataBind()
So change the code to below but how do you create the column headings from the existing Excel spreadsheet not using.
dtExcelData.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), _
New DataColumn("Name", GetType(String)), _
New DataColumn("Salary", GetType(Decimal))})
and do you just add this to the end
Gridview1.datasource = dtExcelData
Gridview1.Databind()