无法在Windows应用程序c#中向CSV文件写入可数据值
本文关键字:文件 数据 CSV 中向 Windows 应用程序 | 更新日期: 2023-09-27 18:17:51
我正在尝试插入可数据内容到csv文件和下面的代码不工作。
代码:
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Property", typeof(string));
System.Data.DataRow newRow = dt.NewRow();
newRow["Name"] = str1;
newRow["Property"] = str2;
dt.Rows.Add(newRow);
dt.WriteToCsvFile("../../Data.csv");
public static void WriteToCsvFile(this DataTable dataTable, string filePath)
{
StringBuilder fileContent = new StringBuilder();
foreach (var col in dataTable.Columns)
{
fileContent.Append(col.ToString() + ",");
}
fileContent.Replace(",", System.Environment.NewLine, fileContent.Length - 1, 1);
foreach (DataRow dr in dataTable.Rows)
{
foreach (var column in dr.ItemArray)
{
fileContent.Append("'"" + column.ToString() + "'",");
}
fileContent.Replace(",", System.Environment.NewLine, fileContent.Length - 1, 1);
}
System.IO.File.WriteAllText(filePath, fileContent.ToString());
}
Data.csv
文件被放置在一个项目文件夹中,值没有被添加。
非常感谢大家的建议。
Edit1:
dt.CreateCSVFile("../../Data.csv");
public static void CreateCSVFile(this DataTable dt, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, true);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
我不知道你为什么可以用
dt.WriteToCsvFile("../../Data.csv");
在DataTable中应该没有这个方法,如果您想将DataTable转换为CVS,您可以尝试以下操作:
public static string DataTableToCSV(this DataTable datatable, char seperator)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < datatable.Columns.Count; i++)
{
sb.Append(datatable.Columns[i]);
if (i < datatable.Columns.Count - 1)
sb.Append(seperator);
}
sb.AppendLine();
foreach (DataRow dr in datatable.Rows)
{
for (int i = 0; i < datatable.Columns.Count; i++)
{
sb.Append(dr[i].ToString());
if (i < datatable.Columns.Count - 1)
sb.Append(seperator);
}
sb.AppendLine();
}
return sb.ToString();
}
你可以在这里找到更多
调用该方法后,您可以将字符串保存到file中,如下所示:
var contents = dt.DataTableToCSV(',');
System.IO.File.WriteAllText (@"D:'exportfile.cvs", contents);
这里我将DataTable转换为CSV列表。你可以把它保存到文件。
代码:
public static IEnumerable<string> ToCsvList(this DataTable table)
{
List<string> returnList = new List<string>();
string colmn = string.Join(",", table.Columns.OfType<DataColumn>().Select(c => c.ColumnName));
returnList.Add(colmn);
foreach (DataRow row in table.Rows)
{
returnList.Add(string.Join(",", row.ItemArray));
}
return returnList;
}
希望这能帮助你抱歉没有重写你的代码
using (MemoryStream output = new MemoryStream())
{
using (StreamWriter writer = new StreamWriter(Server.MapPath("~/CSV/export.csv")))
{
writer.WriteLine(" OVERVIEW");
writer.WriteLine("");
writer.WriteLine("date : " + DateTimeOffset.Parse(DateTimeOffset.Now.ToString(), CultureInfo.CurrentCulture).ToString("yyyy-MM-dd"));
writer.WriteLine("Name");
writer.WriteLine("Property");
writer.WriteLine("");
writer.WriteLine("");
writer.WriteLine("Name,Property");
foreach (var item in List)
{
writer.WriteLine(string.Format("{0},{1}",
item.Name,
item.Property
));
}
writer.Flush();
output.Position = 0;
// var document=Server.MapPath("~/Content/ExportedFiles/Exported_Users.csv");
}
}