To create Excel or Word document file without Excel object model or word object model in vb.net
Step 1: Add the below code in form load for testing the Sample code
'Added a new table with column and rows for demo purpose 'Create a datatable you wish to export to excel. 'Here sample Datatable has been created.
Dim Dt As New System.Data.DataTable Dt.Columns.Add("Eno") Dt.Columns.Add("EmpName") Dt.Columns.Add("Salary") Dim Dr As DataRow Dr = Dt.NewRow() Dr("Eno") = "1000000" Dr("EmpName") = "JK" Dr("Salary") = "190809" Dt.Rows.Add(Dr)
Dr = Dt.NewRow() Dr("Eno") = "1239834" Dr("EmpName") = "Kumar" Dr("Salary") = "234234444" Dt.Rows.Add(Dr)
Dr = Dt.NewRow() Dr("Eno") = "23476234" Dr("EmpName") = "Rajes" Dr("Salary") = "12312333" Dt.Rows.Add(Dr)
Step 2: Call the function to export the data to excel: Parameters: 1st Argument: File Name 2nd Argument: File Path 3rd Argument: DataTable to be exported
'To call the function to create the excel report with filename , path and A datatable to create the excel file ExportToExcelfile("text", "C:\", Dt)
Step 3: Add this function to your code: To append the values to a string builder with Vbtab for new cell and vbcrlf for new line pass the values to create excel sheet.
'To create the values with VBTab for New cell and VBcrlf for New line and append it in a string builder 'Pass the values to the Excel report to generate it
Public Sub ExportToExcelfile(ByVal FileName As String, ByVal SavePath As String, ByVal objDataReader As System.Data.DataTable) Dim sb As New System.Text.StringBuilder Try Dim intColumn, intColumnValue As Integer Dim row As DataRow For intColumn = 0 To objDataReader.Columns.Count - 1 sb.Append(objDataReader.Columns(intColumn).ColumnName) If intColumnValue <> objDataReader.Columns.Count - 1 Then sb.Append(vbTab) End If Next sb.Append(vbCrLf) For Each row In objDataReader.Rows For intColumnValue = 0 To objDataReader.Columns.Count - 1 sb.Append(StrConv(IIf(IsDBNull(row.Item(intColumnValue)), "", row.Item(intColumnValue)), VbStrConv.ProperCase)) If intColumnValue <> objDataReader.Columns.Count - 1 Then sb.Append(vbTab) End If Next sb.Append(vbCrLf) Next SaveExcelfile(SavePath & "\" & FileName & ".xls", sb) Catch ex As Exception Throw Finally objDataReader = Nothing sb = Nothing End Try End Sub
Step 4: Add the following function to the code:
To create the Excel file using Stream writer
'Using the stream writer we can write the file with xls as extension or doc etc., 'and finally disposing the objects Private Sub SaveExcelfile(ByVal fpath As String, ByVal sb As System.Text.StringBuilder) Dim fsFile As New FileStream(fpath, FileMode.Create, FileAccess.Write) Dim strWriter As New StreamWriter(fsFile) Try With strWriter .BaseStream.Seek(0, SeekOrigin.End) .WriteLine(sb) .Close() End With Catch e As Exception Throw Finally sb = Nothing strWriter = Nothing fsFile = Nothing End Try End Sub
AttachmentsTo create a Excel report wihtout Excel object model (20501-26850-To create Excel without Excel object model in vb.doc)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|