C#/ASP.NET でデータテーブルの内容を CSV 出力するサンプルを作成しました。
VisualStudio 2008 で動作確認しています。
using System; using System.Data; using System.IO; namespace WebApplication1 { public class DT : System.Data.DataTable { private string lastError = string.Empty; public string LastError { get { return lastError; } set { lastError = value; } } /// /// CSV形式で出力 /// public bool SaveToStream(MemoryStream ms, string rowDelim, char colDelim, char celDelim, int codepage) { bool result = true; try { // ヘッダ部 string line = string.Empty; foreach (DataColumn col in this.Columns) { line += celDelim; line += col.Caption; line += celDelim; line += colDelim; } // 末尾の ',' 取り除く line = line.TrimEnd(colDelim); // 改行の付加 line += rowDelim; // ストリームへ出力 byte[] byteArray = System.Text.Encoding.GetEncoding(codepage).GetBytes(line); ms.Write(byteArray, 0, byteArray.Length); // データ部 int colCount = this.Columns.Count; foreach (DataRow row in this.Rows) { line = string.Empty; for (int i = 0; i < colCount; i++) { // フィールドの取得 string field = row[i].ToString(); line += celDelim; line += field; line += celDelim; line += colDelim; } // 末尾の ',' 取り除く line = line.TrimEnd(colDelim); // 改行の付加 line += rowDelim; // ストリームへ出力 byteArray = System.Text.Encoding.GetEncoding(codepage).GetBytes(line); ms.Write(byteArray, 0, byteArray.Length); } } catch (Exception e) { LastError = e.ToString(); result = false; } return result; } /// /// サンプル データ作成 /// public void MakeSampleTable() { DataColumn column; DataRow row; // DataColumn の作成 // 1列目を作成 column = new DataColumn(); column.DataType = System.Type.GetType("System.Int32"); column.ColumnName = "ID"; column.ReadOnly = true; column.Unique = true; this.Columns.Add(column); // 2列目を作成. column = new DataColumn(); column.DataType = System.Type.GetType("System.String"); column.ColumnName = "データ"; column.AutoIncrement = false; column.Caption = "データ"; column.ReadOnly = false; column.Unique = false; this.Columns.Add(column); // ID列を主キーに設定 DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = this.Columns["ID"]; this.PrimaryKey = PrimaryKeyColumns; // データセットを作成してデータを作成 DataSet dataSet = new DataSet(); dataSet.Tables.Add(this); // データを10件追加 for (int i = 0; i <= 10; i++) { row = this.NewRow(); row["ID"] = i; row["データ"] = "データ " + i; this.Rows.Add(row); } } } public partial class _Default : System.Web.UI.Page { const string rowDelim = "rn"; const char colDelim = ','; const char celDelim = '"'; const int codepage = 932; const string csvname = "sample.csv"; protected void Page_Load(object sender, EventArgs e) { } /// /// クライアントへCSV出力 /// protected void Button1_Click(object sender, EventArgs e) { DT dt = new DT(); dt.MakeSampleTable(); // サンプル データを MemoryStream にへ出力 System.IO.MemoryStream ms = new System.IO.MemoryStream(); if (!dt.SaveToStream(ms, rowDelim, colDelim, celDelim, codepage)) { System.Diagnostics.Debug.Print(dt.LastError); } // MemoryStream をクライアント側へ送信 Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(csvname)); Response.BinaryWrite(ms.ToArray()); ms.Close(); Response.Close(); } } }