private void btnDownload_Click(object sender, EventArgs e) { if (DB.ExportAllHouseDetails()) { DataTable dt = DB.dsResults.Tables["tbl_HouseDetails"]; //ExportCSV(dt, @"C:\Users\pmcma\Documents\Visual Studio 2012\Projects\Houses\Houses\bin\Debug\HouseList.csv"); // + DateTime.Now); ExportCSV(dt, @"C:\Houses\HouseList.csv"); MessageBox.Show("Download Complete!"); } else { MessageBox.Show("No data found."); } } public static void ExportCSV(DataTable dt, string fileName) { try { // Create the CSV file to which grid data will be exported. StreamWriter sw = new StreamWriter(fileName, false); // First we will write the headers. //DataTable dt = m_dsProducts.Tables[0]; int iColCount = dt.Columns.Count; for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); } catch (Exception ex) { throw ex; } }
StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8"));
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)