日期:2014-05-18  浏览次数:20798 次

.net-sql-动态csv
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class Default2 : System.Web.UI.Page
{
  private BaseDB db = new BaseDB();
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnDC_Click(object sender, EventArgs e)
  {

  DataSet ds = new DataSet();
  ds = db.dataSet("select * from User_tab");
  //Comm.ex
  // DataTable dt = ds.Tables;
  DataTable dt = ds.Tables[0];
  dt.DataSet.DataSetName = ds.ToString();
  HttpContext.Current.Response.Clear();

  System.IO.StringWriter sw = new System.IO.StringWriter();

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

  foreach (DataRow dr in dt.Rows)
  {

  for (int i = 0; i < iColCount; i++)
  {

  if (!Convert.IsDBNull(dr[i]))

  sw.Write("\"'" + dr[i].ToString() + "\"");

  else

  sw.Write("\"\"");

  if (i < iColCount - 1)
  {

  sw.Write(",");

  }

  }

  sw.Write(sw.NewLine);

  }

  sw.Close();

  HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=ss.csv");

  HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

  HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

  HttpContext.Current.Response.Write(sw);

  HttpContext.Current.Response.End();

   
  }
}







结果为:
第一列 第二列 第三列
,A ,B .C
,21 ,123 ,213
然后按按钮后下载成CVS文件




需要结果为合并为一列

A;B;C;
21;123;213;

所有数据都是一列,存放到项目中,随数据库值更新覆盖原来数据更新。







因为在是在做amcharts线型图,有没有动态数据源的范例

------解决方案--------------------
保存在项目中,你使用
 System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("~/xx.csv"));就可以了