从导出的表中删除列

本文关键字:删除列 | 更新日期: 2023-09-27 18:16:16

对于我构建的应用程序,我制作了以下函数,允许将表导出到csv:

`protected void Export_Click(object sender, EventArgs e)
{
    DataTable dt = (DataTable)Session["datatable"];
    String names = (String)Session["names"];
    DataRow total = dt.Rows[dt.Rows.Count - 1]; 
    List<string> colSaved = new List<string>();
    for (int n = dt.Columns.Count -1 ; n >= 0; n--)
    {
        if (total[n].ToString() != "0")
        { 
            colSaved.Add(dt.Columns[n].ColumnName);
        }
    }
    string[] columns = colSaved.ToArray();
    DataTable newTable;
    newTable = dt.DefaultView.ToTable( false, columns);
    String[] content = new String[newTable.Rows.Count + 1];
    string temp1 = "";
    content[0] = names;
    for (int i =0; i<newTable.Rows.Count;i++)

{temp1=";

    for (int j = 0; j < newTable.Columns.Count; j++)
{
    temp1 += "'"" + newTable.Rows[i][j] + "'",";
    content[i + 1] = temp1;
}

}

    string name = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day +      "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "" +  DateTime.Now.Millisecond + "__" + ".csv";
   System.IO.File.WriteAllLines(Server.MapPath("~/export/" + name), content);

响应.重定向("~/export/"+名称(;

}`  

其中dt是一个数据透视表。问题是,在应用程序中,我删除了值为"0"的列,但在导出的文件中,它们确实出现了。我需要一个函数来从导出的表中删除这些列。有人能帮我吗?

从导出的表中删除列

假设您有一个列为a、B、C、D的DataTable,现在您想要一个不同的DataTable实例,该实例具有相同的行,但仅具有列a、B和D(C已根据您的需求删除(

DataTable dt = GetTable(); //This is the table with A, B, C and D columns....
string[] columns = new string[] {"A", "B", "D"};
DataTable newTable;   // This will have only A, B, and D
newTable = dt.DefaultView.ToTable("tempTableName", false, columns);

您可以看到的问题是要知道要从原始源中保留的列的名称。

MSDN on DataView.ToTable方法

然后准备字符串数组的循环可以用这种方式重写

StringBuilder sb = new StringBuilder();
sb.AppendLine(Session["names"].ToString());
foreach(DataRow r in newTable.Rows)
{
    sb.AppendLine("'"" + string.Join("'",", r.ItemArray) + "'"");
}
string name = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "" + DateTime.Now.Millisecond + "__" + ".csv";
System.IO.File.WriteAllText(Server.MapPath("~/export/" + name), sb.ToString());
Response.Redirect("~/export/" + name);

EDIT在进行上述更新后,获取要保存在新表中的列的名称相对容易

List<string> colSaved = new List<string>();
for (int n = dt.Columns.Count - 1; n >= 0; n--)
{
    if (total[n].ToString() != "0")
        colSaved.Add(dt.Columns[n].ColumnName);
}
.....
.....
string[] columns = colSaved.ToArray();

这将使原始表(dt和Session变量(保留原始列,而newTable将只包含保存的列的行。

请注意,如果您在集合上循环以从中移除某些元素,最好从集合的最后一个元素开始,然后向第一个元素循环,从集合的尾部移除元素(无需调整for indexer(

 protected void Export_Click(object sender, EventArgs e)
{

    DataTable dt = (DataTable)Session["datatable"];
    String names = (String)Session["names"];
    DataRow total = dt.Rows[dt.Rows.Count - 1]; //rreshti i fundit
    List<string> colSaved = new List<string>();
    for (int n = dt.Columns.Count-1; n >=0; n--)
    {
        if (total[n].ToString() != "0")  
            colSaved.Add(dt.Columns[n].ColumnName);

    }
string[] columns = colSaved.ToArray();
DataTable newTable;
newTable = dt.DefaultView.ToTable(false, columns);
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Join(",",columns.Reverse()));
foreach (DataRow r in newTable.Rows)
{
         sb.AppendLine(string.Join(",", r.ItemArray.Reverse()));
}
string name = DateTime.Now.Year + "" + DateTime.Now.Month + "" + DateTime.Now.Day + "" + DateTime.Now.Hour + "" + DateTime.Now.Minute + "" + DateTime.Now.Second + "" + DateTime.Now.Millisecond + "__" + ".csv";
System.IO.File.WriteAllText(Server.MapPath("~/export/" + name), sb.ToString());
Response.Redirect("~/export/" + name);

}

这是功能,它终于工作了!