如何将特定列转储到csv [英] How do I dump a specific column to the csv
本文介绍了如何将特定列转储到csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到这个代码示例允许将listview转储到csv。
I saw this code example which allows for dumping listview to csv.
private void Button1_Click(object sender, EventArgs e)
{
//declare new SaveFileDialog + set it's initial properties
{
SaveFileDialog sfd = new SaveFileDialog {
Title = "Choose file to save to",
FileName = "example.csv",
Filter = "CSV (*.csv)|*.csv",
FilterIndex = 0,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
//show the dialog + display the results in a msgbox unless cancelled
if (sfd.ShowDialog() == DialogResult.OK) {
string[] headers = ListView1.Columns
.OfType<ColumnHeader>()
.Select(header => header.Text.Trim())
.ToArray();
string[][] items = ListView1.Items
.OfType<ListViewItem>()
.Select(lvi => lvi.SubItems
.OfType<ListViewItem.ListViewSubItem>()
.Select(si => si.Text).ToArray()).ToArray();
string table = string.Join(",", headers) + Environment.NewLine;
foreach (string[] a in items)
{
//a = a_loopVariable;
table += string.Join(",", a) + Environment.NewLine;
}
table = table.TrimEnd('\r', '\n');
System.IO.File.WriteAllText(sfd.FileName, table);
}
}
}
但我怎么能用它来转储特定的列。我已尝试通过注释或更改几行来修改代码的不同变体,但它要么不执行任何操作,要么只会引发更多错误。
But how could I use it to dump a specific column. I have tried different variation of modifying the code by commenting out or changing a few lines, but it either does nothing or just throws more errors.
推荐答案
我修复了它:
I fixed it:
string[] apString = new string[lstvResults.Items.Count];
if (dlgExportFile.ShowDialog() == DialogResult.OK)
{
// Count Row Indicies
for (int i = 0; i < lstvResults.Items.Count-1; i++)
{
// Get Row Index
ListViewItem Col_Get = lstvResults.Items[i];
// Element in Array Gets MD5 SubItem
apString[i] = Col_Get.SubItems[2].Text;
}
}
string join = string.Join(",\n", apString);
join = join.TrimEnd('\r');
System.IO.File.WriteAllText(dlgExportFile.FileName, join);
这篇关于如何将特定列转储到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文