如何删除从.csv文件中某列 [英] How do I delete certain column from .csv file
问题描述
我下载的数据的列表中,但有些我想忽略某些列,有没有什么办法可以删除它们,这是怎么我的数据库查找:
ID名称SNAME MobileUsage NoBrought
1试验测试12MB 1
2 test1的test1的23MB 8
3 test2的test2的20MB 2
这是当我下载一个.csv文件
<$ p $什么我得到p>
ID名称SNAME MobileUsage NoBrought
1测试12MB
2 test1的23MB
3 test2的20MB
有什么办法,我可以,因为我不使用delete SNAME和NoBrought,我的输出应
ID名称MobileUsage
1测试12MB
2 test1的23MB
3 test2的20MB
这是我做了什么,我认为这个计划不得不寻找列标题如: SNAME&安培; NoBrought,删除整列和左移。当用户点击一个按钮,因此,当下载按钮,点击删除/编辑应该发生时发生此事件。
保护无效的Page_Load(对象发件人,EventArgs五)
{
CsvExport< L> CSV =新CsvExport&所述L个;(的GetList());
Response.Clear();
将Response.Buffer = TRUE;
Response.AddHeader(内容处置,附件;文件名= test.csv);
Response.ContentType =text / plain的;
Response.Output.Write(csv.Export());
到Response.End();
}
公开名单< L>的GetList()
{
名单,LT; L> N =新的List< L>();
名单,LT;字符串> NL =新的List<串GT;();
SqlConnection的康恩=新的SqlConnection(连接在这儿... ...);
的SqlCommand命令=新的SqlCommand(选择编号,名称,MobileUsage FROM表名);
conn.Open();
SqlDataReadData READDATA = Command.ExecuteReader却();
,而(ReadData.Read())
{
l数据=新L();
data.ID = READDATA [ID]的ToString();
data.Name = READDATA [名称]的ToString()。
等...
n.Add(数据);
}
conn.Close();
返回N;
}
我不会进入细节你如何让你的数据。所以让我们只是假设你得到一个CSV文件。
//类强类型我们的结果
公共类csvClass
{
公共csvClass(字符串名称,字符串亩)
{
this.Name =名称; this.MobileUsage =亩;
}
公共字符串名称{;组; }
公共字符串MobileUsage {搞定;组; }
}
//只是无论你需要File.ReadLines
VAR csvData =从行加载CSV(@路径/到/ FILE.CSV)
//数据仍然在一行。通过分隔符
分割让列= row.Split(';')
//强类型结果
选择新csvClass
{
//则会忽略列2和4
//取第一列
名称=柱[0],
//将第三列
MobileUsage =柱[2]
};
这之后,你应该有一个的IEnumerable< csvClass>()
与你想要的两列,您可以写信给任何你想要的 - 新的CSV文件,数据库......
I am downloading the list of data but some i would like to ignore some columns, is there any way i can remove them, this is how my database look:
ID Name Sname MobileUsage NoBrought
1 test test 12mb 1
2 test1 test1 23mb 8
3 test2 test2 20mb 2
This is what i am getting when i download a .csv file
ID Name Sname MobileUsage NoBrought
1 test 12mb
2 test1 23mb
3 test2 20mb
Is there any way i can delete Sname and NoBrought as i am not using, my output should be
ID Name MobileUsage
1 test 12mb
2 test1 23mb
3 test2 20mb
This is what i have done, i assume this program have to look for column heading e.g. Sname & NoBrought, delete the entire column and shift left. This event occurs when a user clicks a button, as a result the deleting/editing should happen when download button clicked.
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;
}
I won't go into detail on how you get your data. So lets just assume you get a csv file.
//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]
};
After this you should have an IEnumerable<csvClass>()
with just the 2 columns you want, which you can write to anywhere you want - new csv-file, database ...
这篇关于如何删除从.csv文件中某列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!