我有一个将数据表导出到csv文件的函数。并且在每隔一行之后添加一个空白行。
Assets|Profit Center|Plant|Asset Class|Depr Account|Cost Center|Inventory Number|Location|Cap Date|Last Inventoried|Deactivation Date|Asset Description|Acquis Value|Monthly Depriciation|O dep posted|Accum Dep|Book Value|Serial|Inventory Note|Inventory Comments|LFAC|Room|Supplier Name|Manufacturer of Asset|EFO|Asset Main Text|EGM|Person Responsible|TOP Sider|Type Name & Eccn Number|Std|Class|Inventory Status|Inventoried By|Prior Room|Comments
1007309|DAHQ|DAHQ|32752|3375|DAHQB00760|QJQX0290E1|P229|3/25/1990|4/30/2013|0|CTC, West Wing Land Improvements|0| - |0| - | - |0|0|-|Thompson Jennifer|CTC|CTCWBLDG|X / X|0 / 0 / X / Huntzinger,Joe|LAND IMPROVEMENTS|-|-|-|0|3275|Land Improvements|NOT FOUND|||
1007313|DAHQ|DAHQ|32752|3375|DAHQB00760|QJQX0890E1|P229|9/25/1990|4/30/2013|0|CTC, West Wing Land Improvements|0| - |0| - | - |0|0|-|Thompson Jennifer|CTC|CTCWBLDG|X / X|0 / 0 / X / Huntzinger,Joe|LAND IMPROVEMENTS|-|-|-|0|3275|Land Improvements|NOT FOUND|||
try
{
data.Tables[tableMaster].Columns.Remove("ID");
}
catch
{
}
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = data.Tables[tableMaster].Columns
.Cast<DataColumn>()
.Select(column => column.ColumnName);
sb.AppendLine(string.Join("|", columnNames));
foreach (DataRow row in data.Tables[tableMaster].Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join("|", fields));
}
File.WriteAllText("test.txt", sb.ToString());
如何避免空行?
答案 0 :(得分:0)
这是一个使用字符串列表而不是字符串构建器的解决方案:
List<String> strlst = new List<String>();
strlst.Add(String.Join("|", data.Tables[tableMaster].Columns.Cast<DataColumn>().Select(x => x.ColumnName)));
foreach (DataRow row in data.Tables[tableMaster].Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
strlst.Add(string.Join("|", fields));
}
File.WriteAllLines("test.txt", strlst);