对于我已经构建的应用程序,我已经使用了followig函数来允许表导出到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);
Response.Redirect(“〜/ export /”+ name);
}`
其中dt是一个旋转数据表。问题是在应用程序中我删除了值为“0”的列,但在导出的文件中,它们确实出现了。我还需要一个函数来从导出的表中删除这些列。有人可以帮助我吗?
答案 0 :(得分: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 methods
然后可以用这种方式重写准备字符串数组的循环
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);
编辑上述更新后,获取要保留在新表格中的列名称相对容易
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)
答案 1 :(得分:0)
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);
}
这是功能,它最终有效!