Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 22nd, 2006, 11:25 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help in exporting memo fields to CSV file

Hi Everyone:

I was trying to export a memo field from an Access file to CSV (comma separated value) file using C#. But the code is not producing the desired result because the memo field is getting broken into several cells.

The field "Description" is a memo field. I have the memo field in a temporary file called "ds". I am not able to send this field to the CSV file.

I'll explain a little bit about what i am doing in the "for" loop. I was trying to convert all the fields to string and keep each of the field in quotes so that if any filed has a comma within it that field won't get divided into two different field when the file is opened in excel.

I'd really appreciate if anyone could suggest a solution.

Thanks in Advance.

Satya



        private void bt_export2excel_Click(object sender, EventArgs e)
        {
            String PerfData = "";
            String filename = "";

            try
            {
                using (SqlCommand HFcomm = new SqlCommand(" SELECT F20, F1, F2,F3,F4,F5,F6,F7, F9, F10,F11, Description FROM Information INNER JOIN SPVendor ON Information.DataVendorName=SPVendor.DataVendorName and Information.DataVendorID=SPVendor.DataVendorID WHERE SPVendor.GID= " + this.gid, this.HFconn))
                {
                    SqlDataAdapter adapt = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    adapt.SelectCommand = HFcomm;
                    adapt.Fill(ds);

                    using (SaveFileDialog savefile = new SaveFileDialog())
                    {
                        savefile.Filter = "CSV files (*.csv)|*.csv";
                        savefile.RestoreDirectory = true;
                        savefile.Title = "Save the Return";
                        savefile.ShowDialog();
                        filename = savefile.FileName;
                    }
                    TextWriter tw = new StreamWriter(filename);

                    if (filename != "")
                    {
                        PerfData = "Name" + "," + "Manager" + "," + "Address" + "," + "Address" + "," + "City" + "," + "State" + "," + "ZIP" + "," + "Country" + "," + "Tel" + "," + "Fax" + "," + "E-mail" + ",";
                        tw.WriteLine(PerfData);

                        foreach (DataRow rw in ds.Tables[0].Rows)
                        {
                            PerfData = "";
                            for (int i = 0; i < rw.Table.Columns.Count; i++)
                            {

                                PerfData += "\"" + rw[i].ToString() + "\"" + ",";
                            }

                            tw.WriteLine(PerfData);

                        }
                        ds.Clear();
                    tw.Close();
                }
            }
            catch (System.ArgumentNullException)
            {
                MessageBox.Show("Provide a valid file name");
            }
        }

    }
}


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting a csv file (comma separated) micharl Beginning VB 6 6 May 7th, 2008 10:10 PM
Exporting a Datagrid to a CSV file pauljohns353 Visual Basic 2005 Basics 1 November 21st, 2006 04:56 PM
Exporting Data from DataGrid to a CSV file PT ASP.NET 1.0 and 1.1 Professional 2 June 22nd, 2005 04:41 AM
Memo Fields with Windows Forms in C# jim.stanley Access 0 November 15th, 2004 05:55 PM
Memo fields takabyte Classic ASP Basics 2 October 31st, 2003 06:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.