Exporting CSV files in .NET
Join the DZone community and get the full member experience.
Join For FreeThis article was written by the Imaginovation team. They are a Raleigh web design and software development company who uses .NET, PHP, HTML5, JavaScript, and jQuery technologies.
In development scenarios we often come across the need to export data from .net into CSV file format. A CSV file can be generated using different programming methods as follows.
1. Using string builder: FileStream and StreamWriter are used to create CSV files. Here is a sample code. It is most commonly used in web services and windows services.
Pros: It is very simple to implement and can be used in all different applications like web based application, windows application, windows services, and web services.
Cons: It writes a file to a particular folder and does not allow the user to download the file. If this code is used in a web based application, then it writes files on the server and will be difficult for the user to access it.
Dim fs As FileStream = Nothing Dim sw As StreamWriter = Nothing Try Dim fileName As String = "c:\test.csv" If File.Exists(fileName) Then fs = New FileStream(fileName, FileMode.Append, FileAccess.Write) sw = New StreamWriter(fs) Else fs = New FileStream(fileName, FileMode.Create, FileAccess.Write) sw = New StreamWriter(fs) Dim HeaderRow As String = "Col1,Col2,Col3" sw.WriteLine(HeaderRow) End If Dim RowData As String = "col1Data.col2Data,col3Data" sw.WriteLine(RowData) Catch ex As Exception Finally If Not sw Is Nothing Then sw.Close() End If If Not fs Is Nothing Then fs.Close() End If End Try
2. MS Excel application: This method used the MS application DCOM object. You need to use the reference of “Microsoft.Office.Interop.Excel”. Here is the sample code.
Pros: It is very simple to implement and can be used in all different applications like web based application, windows application, windows services, and web services.
Cons: First, it writes a file to a particular folder and does not allow the user to download the file. If this code is used in a web based application, then it writes files on the server and will be difficult for the user to access it. Second, this code does not work if “Microsoft.Office.Interop.Excel” is not installed on the server.
Imports Excel = Microsoft.Office.Interop.Excel Try Dim fileName As String = "C:\test.csv" Dim objExcel As New Excel.ApplicationClass Dim wBook As Excel.Workbook Dim wSheet As Excel.Worksheet wBook = _excel.Workbooks.Add() wSheet = wBook.ActiveSheet() Dim rowIndex As Integer = 1 objExcel.Cells(0, 1) = "Col1" objExcel.Cells(0, 2) = "Col2" objExcel.Cells(0, 3) = "Col3" objExcel.Cells(1, 1) = "Col1Data" objExcel.Cells(1, 2) = "Col2Data" objExcel.Cells(1, 3) = "Col3Data" wSheet.Columns.AutoFit() If System.IO.File.Exists(fileName) Then System.IO.File.Delete(fileName) End If wBook.SaveAs(fileName) wBook.Close() objExcel.Quit() Catch ex As Exception End Try
3. Export Gridview data: This is the easiest way to import gridview data to a CSV format. It allows you to download data in CSV format which you can save on your local hard drive. It uses standard library function. Here is a sample code.
Pros: It is very simple to implement and allow user to download it on button click event in web based application.
Cons: It is used only in web based application.
GridViewExportUtil.Export("Test.csv", GridView)
* If you want more information about this or if your in need of any web development or software services, please request a quote on our website at www.imaginovation.net, or feel free to give us a call at (888) 723-8643.
Opinions expressed by DZone contributors are their own.
Comments