download csv
調用:
方法:
string data = CSV("SOCommitReportFp2g",ds);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", "attachment;filename=SOCommitReportFp2g.csv");
Response.ContentType="csv/html";
Response.Clear();
Response.Write(data);
Response.End();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("big5");
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", "attachment;filename=SOCommitReportFp2g.csv");
Response.ContentType="csv/html";
Response.Clear();
Response.Write(data);
Response.End();
方法:
private string CSV(string reportname,DataSet ds)
{
StringBuilder data=new StringBuilder();
//寫出欄名
foreach (DataColumn column in ds.Tables[0].Columns)
{
data.Append(column.ColumnName.TrimEnd() + ",");
}
data.Append("\n");
//寫出資料
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (DataColumn column in ds.Tables[0].Columns)
{
data.Append(row[column].ToString().TrimEnd() + ",");
}
data.Append("\n");
}
data.Append("\n");
string str=data.ToString();
return str;
}
{
StringBuilder data=new StringBuilder();
//寫出欄名
foreach (DataColumn column in ds.Tables[0].Columns)
{
data.Append(column.ColumnName.TrimEnd() + ",");
}
data.Append("\n");
//寫出資料
foreach (DataRow row in ds.Tables[0].Rows)
{
foreach (DataColumn column in ds.Tables[0].Columns)
{
data.Append(row[column].ToString().TrimEnd() + ",");
}
data.Append("\n");
}
data.Append("\n");
string str=data.ToString();
return str;
}