export excel sheet data using bulk copy method in asp.net and at the same time appending a column to excel data
VB.Net code
Public Sub ExcelToDbBulkCopy()
Try
Dim ExcelConn As OleDbConnection
Dim ExcelCmd As OleDbCommand
Dim Excelda As OleDbDataAdapter
Dim Excelds As DataSet
Dim Excelquery As String
Dim ExcelconnString As String = String.Empty
Dim strConString As String = String.Empty
Dim strFileName As String = DateTime.Now.ToString("ddMMyyyy-HHmmss.fff")
Dim strFileType As String = System.IO.Path.GetExtension(FileUpl.FileName).ToString().ToLower()
If FileUpl.HasFile Then
If strFileType.Trim = ".xlsx" Then
FileUpl.SaveAs(Server.MapPath("Data/" & strFileName & strFileType))
Else
WebMessage("Only excel files allowed with '.xlsx'")
Exit Sub
End If
Else
WebMessage("Please select excel file to upload")
Exit Sub
End If
Dim strNewPath As String = Server.MapPath("Data/" & strFileName & strFileType)
ExcelconnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
ExcelConn = New OleDbConnection(ExcelconnString)
If ExcelConn.State = ConnectionState.Closed Then ExcelConn.Open()
Dim dt As DataTable = Nothing
Dim SheetName As String = String.Empty
dt = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
For Each row As DataRow In dt.Rows
SheetName = row("TABLE_NAME").ToString
Exit For
Next
Excelquery = "SELECT * FROM [" & SheetName & "]"
ExcelCmd = New OleDbCommand(Excelquery, ExcelConn)
Excelda = New OleDbDataAdapter(ExcelCmd)
Excelds = New DataSet()
Excelda.Fill(Excelds)
'append column to excel datatble
Excelds.Tables(0).Columns.Add("CreatedBy")
Dim rownum As Integer = 0
For Each row As DataRow In Excelds.Tables(0).Rows
Dim dtView As DataView = Excelds.Tables(0).DefaultView
dtView.Item(rownum).Row.Item("CreatedBy") = "SYSTEM"
Excelds.Tables(0).AcceptChanges()
rownum += 1
Next
'Bulk copy function
strConString = System.Configuration.ConfigurationManager.ConnectionStrings("ConMaturity").ConnectionString
Dim bcp As SqlBulkCopy = New SqlBulkCopy(strConString)
bcp.DestinationTableName = "dbo.TblMaturityListing"
'mapping required if source and destination column names are different
bcp.ColumnMappings.Add("CERTIFICATE SEQN NO", "CertSeqNo")
bcp.ColumnMappings.Add("CERTIFICATENO", "CertNo")
bcp.ColumnMappings.Add("PRODUCTCODE", "PrdCode")
bcp.ColumnMappings.Add("PRODUCTNAME", "PrdName")
bcp.ColumnMappings.Add("CreatedBy", "CreatedBy")
bcp.WriteToServer(Excelds.Tables(0))
Excelda.Dispose()
ExcelConn.Close()
ExcelConn.Dispose()
System.IO.File.Delete(strNewPath)
WebMessage("Data Uploaded Successfully.")
Catch ex As Exception
WebMessage(ex.ToString)
End Try
End Sub
VB.Net code
Public Sub ExcelToDbBulkCopy()
Try
Dim ExcelConn As OleDbConnection
Dim ExcelCmd As OleDbCommand
Dim Excelda As OleDbDataAdapter
Dim Excelds As DataSet
Dim Excelquery As String
Dim ExcelconnString As String = String.Empty
Dim strConString As String = String.Empty
Dim strFileName As String = DateTime.Now.ToString("ddMMyyyy-HHmmss.fff")
Dim strFileType As String = System.IO.Path.GetExtension(FileUpl.FileName).ToString().ToLower()
If FileUpl.HasFile Then
If strFileType.Trim = ".xlsx" Then
FileUpl.SaveAs(Server.MapPath("Data/" & strFileName & strFileType))
Else
WebMessage("Only excel files allowed with '.xlsx'")
Exit Sub
End If
Else
WebMessage("Please select excel file to upload")
Exit Sub
End If
Dim strNewPath As String = Server.MapPath("Data/" & strFileName & strFileType)
ExcelconnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strNewPath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
ExcelConn = New OleDbConnection(ExcelconnString)
If ExcelConn.State = ConnectionState.Closed Then ExcelConn.Open()
Dim dt As DataTable = Nothing
Dim SheetName As String = String.Empty
dt = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
For Each row As DataRow In dt.Rows
SheetName = row("TABLE_NAME").ToString
Exit For
Next
Excelquery = "SELECT * FROM [" & SheetName & "]"
ExcelCmd = New OleDbCommand(Excelquery, ExcelConn)
Excelda = New OleDbDataAdapter(ExcelCmd)
Excelds = New DataSet()
Excelda.Fill(Excelds)
'append column to excel datatble
Excelds.Tables(0).Columns.Add("CreatedBy")
Dim rownum As Integer = 0
For Each row As DataRow In Excelds.Tables(0).Rows
Dim dtView As DataView = Excelds.Tables(0).DefaultView
dtView.Item(rownum).Row.Item("CreatedBy") = "SYSTEM"
Excelds.Tables(0).AcceptChanges()
rownum += 1
Next
'Bulk copy function
strConString = System.Configuration.ConfigurationManager.ConnectionStrings("ConMaturity").ConnectionString
Dim bcp As SqlBulkCopy = New SqlBulkCopy(strConString)
bcp.DestinationTableName = "dbo.TblMaturityListing"
'mapping required if source and destination column names are different
bcp.ColumnMappings.Add("CERTIFICATE SEQN NO", "CertSeqNo")
bcp.ColumnMappings.Add("CERTIFICATENO", "CertNo")
bcp.ColumnMappings.Add("PRODUCTCODE", "PrdCode")
bcp.ColumnMappings.Add("PRODUCTNAME", "PrdName")
bcp.ColumnMappings.Add("CreatedBy", "CreatedBy")
bcp.WriteToServer(Excelds.Tables(0))
Excelda.Dispose()
ExcelConn.Close()
ExcelConn.Dispose()
System.IO.File.Delete(strNewPath)
WebMessage("Data Uploaded Successfully.")
Catch ex As Exception
WebMessage(ex.ToString)
End Try
End Sub
No comments:
Post a Comment