DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Exporting CSV files in .NET

Michael Georgiou user avatar by
Michael Georgiou
·
Sep. 26, 12 · Interview
Like (0)
Save
Tweet
Share
9.06K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

CSV application Web Service

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • How To Avoid “Schema Drift”
  • Problems of Cloud Cost Management: A Socio-Technical Analysis
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: