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();

方法:
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;
        }
posted @ 2007-09-25 10:27  Nina  阅读(444)  评论(0编辑  收藏  举报