We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

The following code lines are useful to create an Excel sheet from a CSV file in VB.Net.
We need to pass the complete folder path and csv file name as two parameters.
In the code I have given "NewExcelFile.xls" as the file name for new file to be created.
This code will generate an NewExcelFile.xls file inside the given path with all records from csv file.
 
To use Microsoft.Office.Interop.Excel namespace we need to add reference of Microsoft Excel 12.0 Object Library.
Vb.Net Code:
 
Imports Excel = Microsoft.Office.Interop.Excel
 
Public Sub CreateExcelFromCsvFile(ByVal strFolderPath As String, ByVal strFileName As String)
 
        Dim newFileName As String = "NewExcelFile.xls"
        Dim oExcelFile As Object
 
       ' Open Excel application object
        Try
            oExcelFile = GetObject(, "Excel.Application")
        Catch
            oExcelFile = CreateObject("Excel.Application")
        End Try
 
        oExcelFile.Visible = False
        oExcelFile.Workbooks.Open(strFolderPath  + "\" + strFileName)
 
        ' Turn off message box so that we do not get any messages
        oExcelFile.DisplayAlerts = False
 
        ' Save the file as XLS file
        oExcelFile.ActiveWorkbook.SaveAs(Filename:=strFolderPath + "\" +   newFileName, FileFormat:=Excel.XlFileFormat.xlExcel5, CreateBackup:=False)
 
        ' Close the workbook
        oExcelFile.ActiveWorkbook.Close(SaveChanges:=False)
 
        ' Turn the messages back on
        oExcelFile.DisplayAlerts = True
 
        ' Quit from Excel
        oExcelFile.Quit()
 
        ' Kill the variable
        oExcelFile = Nothing
 
    End Sub


Related Tags:

VB.NET, CSV, Excel

Author: Shibani Shubhadarshini

top

VB.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login