我正在下载数据列表,但有些我想忽略一些列,有什么方法可以删除它们,这就是我的数据库看起来:
ID Name Sname MobileUsage NoBrought
1 test test 12mb 1
2 test1 test1 23mb 8
3 test2 test2 20mb 2
这是我下载.csv文件时得到的内容
ID Name Sname MobileUsage NoBrought
1 test 12mb
2 test1 23mb
3 test2 20mb
有什么方法可以删除Sname和NoBrought,因为我不使用,我的输出应该是
ID Name MobileUsage
1 test 12mb
2 test1 23mb
3 test2 20mb
这就是我所做的,我假设这个程序必须寻找列标题,例如Sname& NoBrought,删除整个列并向左移动。当用户单击按钮时会发生此事件,因此在单击下载按钮时应执行删除/编辑。
protected void Page_Load(object sender, EventArgs e)
{
CsvExport<l> csv = new CsvExport<l>(getList());
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=test.csv");
Response.ContentType = "text/plain";
Response.Output.Write(csv.Export());
Response.End();
}
public List<l> getList()
{
List<l> n = new List<l>();
List<string> nl = new List<string>();
SqlConnection conn = new SqlConnection(Connection goes here...);
SqlCommand command = new SqlCommand("select ID, Name, MobileUsage FROM TableName");
conn.Open();
SqlDataReadData ReadData = command.ExecuteReader();
while (ReadData.Read())
{
l data = new l();
data.ID = ReadData["ID"].ToString();
data.Name = ReadData["Name"].ToString();
etc...
n.Add(data);
}
conn.Close();
return n;
}
答案 0 :(得分:1)
我不会详细介绍如何获取数据。所以我们假设你得到一个csv文件。
//class to strongly type our results
public class csvClass
{
public csvClass(string name; string mu)
{
this.Name = name; this.MobileUsage = mu;
}
public string Name { get; set; }
public string MobileUsage { get; set; }
}
//just load your csv from wherever you need
var csvData = from row in File.ReadLines(@"Path/to/file.csv")
// data is still in one line. Split by delimiter
let column = row.Split(';')
//strongly type result
select new csvClass
{
//Ingore column 2 and 4
//Take first column
Name = column[0],
//Take third column
MobileUsage = column[2]
};
在此之后你应该有一个只有你想要的2列的IEnumerable<csvClass>()
,你可以写到任何你想要的地方 - 新的csv文件,数据库......