dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
sharmila
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » General

How to save datatable as a .csv file?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 20


Code is written in C# and application is windows based application. You can create a class library and put this code snippet in to it and call whenever it is required. .CSV is a comma seperated file.



 


How to save datatable as a .csv file?
Description This method is used to take datatable as a parameter and save all datatable contents as .csv file.

public void ConvertDatatableToCSV(Datatable user_defined_datatable )
{

private System.Windows.Forms.SaveFileDialog user_save_dialog_box;
try
{
user_save_dialog_box.Filter = "CSV Files | *.csv";
if (user_save_dialog_box.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
user_defined_file_name = user_save_dialog_box.FileName;

Microsoft.Office.Interop.Excel.Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
excelApplication.Visible = false;

Microsoft.Office.Interop.Excel.Workbook workbook = excelApplication.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

// This is used for Header.
for (int i = 0; i < user_defined_datatable.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = user_defined_datatable.Columns[i].ColumnName;
}

// This is used for contents
for (int i = 0; i < user_defined_datatable.Rows.Count; i++)
{
for (int j = 0; j < user_defined_datatable.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = user_defined_datatable.Rows[i][j].ToString();
}
}

workbook.SaveAs(user_defined_file_name, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, false, false, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(true, Type.Missing, Type.Missing);
excelApplication.Quit();
}

}
catch
{
MessageBox.Show("Save Error", "Application_name", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to save datatable as a .csv file?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Delete Lists and items from SharePoint site using CAML Query.
    Previous Resource: How to check whether an element is present in the expected range at xpath in XML file?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.