在ASP.NET中将DataTable导出为.CSV文件时,对DataTable的更改不会持久存在

时间:2015-03-18 10:03:06

标签: c# asp.net csv datatable persistence

以下代码仅在.csv文件中发送DataTable的最后一行。我在这里看不到我错的地方,我是新来的,请帮助我

DataTable dt22 = new DataTable(); 
dt22.Columns.Add("Tasks");
DataRow dr = dt22.NewRow(); 
dr[0] = TextBox2.Text;

dt22.Rows.Add(dr);

GridView1.DataSource = dt22; 
GridView1.DataBind();

Session["Data"] = dt22;

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dt22.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt22.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

File.WriteAllText(@"c:\Tasks.csv", sb.ToString());

1 个答案:

答案 0 :(得分:1)

<强>更新

很难准确理解这个问题,但我认为这是在回发之间的数据持久性。当用户向网格添加一行时,您是否更新了基础数据源,然后继续创建CSV?

看起来您正在尝试使用Session来保存数据,您是否尝试过这样的事情:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        InitialDataLoad();
}

private void InitialDataLoad()
{
    var dt = new DataTable();
    dt.Columns.Add("Tasks");
    dt.Columns.Add("AnotherColumn");

    var dr = dt.NewRow();
    dr[0] = "task1";
    dr[1] = "c1";
    dt.Rows.Add(dr);

    GridView1.DataSource = dt;
    GridView1.DataBind();

    Session["dt"] = dt;
}

protected void Button1_Click(object sender, EventArgs e)
{
    // adds a row

    var dt = Session["dt"] as DataTable;

    if (dt != null)
    {
        var dr = dt.NewRow();
        dr[0] = "anothertask";
        dr[1] = "c2";
        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        GridView1.DataBind();

        Session["dt"] = dt;
    }
}

protected void Button2_Click(object sender, EventArgs e)
{
    // create csv

    var dt = Session["dt"] as DataTable;

    var sb = new StringBuilder();

    IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().
                                      Select(column => column.ColumnName);

    sb.AppendLine(string.Join(",", columnNames));

    foreach (DataRow row in dt.Rows)
    {
        IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
        sb.AppendLine(string.Join(",", fields));
    }

    File.WriteAllText(@"c:\Tasks.csv", sb.ToString());
}

旧答案:

您的代码的最后一个循环能够打印的不仅仅是最后一行,所以我不会在那里看到问题。问题可能出在您期望在dt22中。看起来您只是将一行数据添加到代码顶部的dt22 DataTable中,那么您期望有多少行?

以下是使用相同代码的更多数据的示例:

DataTable dt22 = new DataTable();
dt22.Columns.Add("Tasks");
dt22.Columns.Add("AnotherColumn");

DataRow dr = dt22.NewRow();
dr[0] = "task1";
dr[1] = "c1";
dt22.Rows.Add(dr);

DataRow dr2 = dt22.NewRow();
dr2[0] = "test2";
dr2[1] = "c2";
dt22.Rows.Add(dr2);

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dt22.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt22.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

File.WriteAllText(@"c:\Tasks.csv", sb.ToString());

打印:

Tasks,AnotherColumn
task1,c1
test2,c2