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");
}
}
}
}
|