在 C# 中将数据写入 CSV 文件
本文关键字:CSV 文件 数据 | 更新日期: 2023-09-27 18:31:04
我正在尝试使用 C# 语言逐行写入csv
文件。这是我的函数
string first = reader[0].ToString();
string second=image.ToString();
string csv = string.Format("{0},{1}'n", first, second);
File.WriteAllText(filePath, csv);
整个函数在循环内运行,每一行都应写入csv
文件。就我而言,下一行会覆盖现有行,最后,我在csv文件中只得到一条记录,这是最后一行。如何写入csv
文件中的所有行?
更新
回到我天真的时代,我建议手动执行此操作(这是一个简单问题的简单解决方案),但是由于这变得越来越流行,我建议使用库 CsvHelper 来完成所有安全检查等。
CSV比问题/答案所暗示的要复杂得多。
原始答案
由于您已经有一个循环,请考虑这样做:
//before your loop
var csv = new StringBuilder();
//in your loop
var first = reader[0].ToString();
var second = image.ToString();
//Suggestion made by KyleMit
var newLine = string.Format("{0},{1}", first, second);
csv.AppendLine(newLine);
//after your loop
File.WriteAllText(filePath, csv.ToString());
或者类似的东西。我的理由是:您不需要为每个项目写入文件,您只需打开一次流,然后写入它。
您可以替换
File.WriteAllText(filePath, csv.ToString());
跟
File.AppendAllText(filePath, csv.ToString());
如果要将以前版本的 CSV 保留在同一文件中
C# 6
如果您使用的是 c# 6.0,则可以执行以下操作
var newLine = $"{first},{second}"
编辑
这是一个问题的链接,解释了Environment.NewLine
的作用。
我强烈建议你走更乏味的路线。特别是如果您的文件大小很大。
using(var w = new StreamWriter(path))
{
for( /* your loop */)
{
var first = yourFnToGetFirst();
var second = yourFnToGetSecond();
var line = string.Format("{0},{1}", first, second);
w.WriteLine(line);
w.Flush();
}
}
File.AppendAllText()
打开一个新文件,写入内容,然后关闭该文件。与将数据写入打开流相比,打开文件是一项资源密集型操作。打开/关闭循环中的文件将导致性能下降。
Johan 建议的方法通过将所有输出存储在内存中然后写入一次来解决这个问题。但是(在大文件的情况下),您的程序将消耗大量RAM,甚至崩溃OutOfMemoryException
我的解决方案的另一个优点是,您可以通过在输入数据中保存当前位置来实现暂停''恢复。
上。使用正确的位置
手动编写 csv 文件可能很困难,因为您的数据可能包含逗号和换行符。我建议您改用现有的库。
这个问题提到了几个选项。
C# 中是否有任何 CSV 读取器/编写器库?
我使用两个解析解决方案,因为它非常易于维护
// Prepare the values
var allLines = (from trade in proposedTrades
select new object[]
{
trade.TradeType.ToString(),
trade.AccountReference,
trade.SecurityCodeType.ToString(),
trade.SecurityCode,
trade.ClientReference,
trade.TradeCurrency,
trade.AmountDenomination.ToString(),
trade.Amount,
trade.Units,
trade.Percentage,
trade.SettlementCurrency,
trade.FOP,
trade.ClientSettlementAccount,
string.Format("'"{0}'"", trade.Notes),
}).ToList();
// Build the file content
var csv = new StringBuilder();
allLines.ForEach(line =>
{
csv.AppendLine(string.Join(",", line));
});
File.WriteAllText(filePath, csv.ToString());
与其每次都调用AppendAllText()
,不如考虑打开文件一次,然后写入整个内容一次:
var file = @"C:'myOutput.csv";
using (var stream = File.CreateText(file))
{
for (int i = 0; i < reader.Count(); i++)
{
string first = reader[i].ToString();
string second = image.ToString();
string csvRow = string.Format("{0},{1}", first, second);
stream.WriteLine(csvRow);
}
}
与其重新发明轮子,不如使用库。 CsvHelper
非常适合创建和读取 csv 文件。它的读取和写入操作是基于流的,因此也支持处理大量数据的操作。
您可以像下面这样编写 csv。
using(var textWriter = new StreamWriter(@"C:'mypath'myfile.csv"))
{
var writer = new CsvWriter(textWriter, CultureInfo.InvariantCulture);
writer.Configuration.Delimiter = ",";
foreach (var item in list)
{
writer.WriteField( "a" );
writer.WriteField( 2 );
writer.WriteField( true );
writer.NextRecord();
}
}
<小时 />由于库使用反射,它将采用任何类型并直接解析它。
public class CsvRow
{
public string Column1 { get; set; }
public bool Column2 { get; set; }
public CsvRow(string column1, bool column2)
{
Column1 = column1;
Column2 = column2;
}
}
IEnumerable<CsvRow> rows = new [] {
new CsvRow("value1", true),
new CsvRow("value2", false)
};
using(var textWriter = new StreamWriter(@"C:'mypath'myfile.csv")
{
var writer = new CsvWriter(textWriter, CultureInfo.InvariantCulture);
writer.Configuration.Delimiter = ",";
writer.WriteRecords(rows);
}
值 1,真
<小时 />值2,假
如果您想阅读有关库配置和可能性的更多信息,可以在此处阅读。
您可以改用AppendAllText
:
File.AppendAllText(filePath, csv);
正如WriteAllText
的文档所说:
如果目标文件已存在,则会覆盖它
另外,请注意,您当前的代码没有使用正确的新行,例如在记事本中,您将看到所有新行都是一长行。将代码更改为此代码以具有正确的新行:
string csv = string.Format("{0},{1}{2}", first, image, Environment.NewLine);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class CS : System.Web.UI.Page
{
protected void ExportCSV(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
foreach (DataColumn column in dt.Columns)
{
//Add the Header row for CSV file.
csv += column.ColumnName + ',';
}
//Add new line.
csv += "'r'n";
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "'r'n";
}
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
}
}
}
}
}
}
处理逗号
为了在使用string.Format(...)
时处理值内的逗号,以下内容对我有用:
var newLine = string.Format("'"{0}'",'"{1}'",'"{2}'"",
first,
second,
third
);
csv.AppendLine(newLine);
因此,将其与Johan的答案结合起来,它看起来像这样:
//before your loop
var csv = new StringBuilder();
//in your loop
var first = reader[0].ToString();
var second = image.ToString();
//Suggestion made by KyleMit
var newLine = string.Format("'"{0}'",'"{1}'"", first, second);
csv.AppendLine(newLine);
//after your loop
File.WriteAllText(filePath, csv.ToString());
返回 CSV 文件
如果您只是想返回文件而不是将其写入某个位置,这是我如何完成它的示例:
从存储过程
public FileContentResults DownloadCSV()
{
// I have a stored procedure that queries the information I need
SqlConnection thisConnection = new SqlConnection("Data Source=sv12sql;User ID=UI_Readonly;Password=SuperSecure;Initial Catalog=DB_Name;Integrated Security=false");
SqlCommand queryCommand = new SqlCommand("spc_GetInfoINeed", thisConnection);
queryCommand.CommandType = CommandType.StoredProcedure;
StringBuilder sbRtn = new StringBuilder();
// If you want headers for your file
var header = string.Format("'"{0}'",'"{1}'",'"{2}'"",
"Name",
"Address",
"Phone Number"
);
sbRtn.AppendLine(header);
// Open Database Connection
thisConnection.Open();
using (SqlDataReader rdr = queryCommand.ExecuteReader())
{
while (rdr.Read())
{
// rdr["COLUMN NAME"].ToString();
var queryResults = string.Format("'"{0}'",'"{1}'",'"{2}'"",
rdr["Name"].ToString(),
rdr["Address"}.ToString(),
rdr["Phone Number"].ToString()
);
sbRtn.AppendLine(queryResults);
}
}
thisConnection.Close();
return File(new System.Text.UTF8Encoding().GetBytes(sbRtn.ToString()), "text/csv", "FileName.csv");
}
从列表
/* To help illustrate */
public static List<Person> list = new List<Person>();
/* To help illustrate */
public class Person
{
public string name;
public string address;
public string phoneNumber;
}
/* The important part */
public FileContentResults DownloadCSV()
{
StringBuilder sbRtn = new StringBuilder();
// If you want headers for your file
var header = string.Format("'"{0}'",'"{1}'",'"{2}'"",
"Name",
"Address",
"Phone Number"
);
sbRtn.AppendLine(header);
foreach (var item in list)
{
var listResults = string.Format("'"{0}'",'"{1}'",'"{2}'"",
item.name,
item.address,
item.phoneNumber
);
sbRtn.AppendLine(listResults);
}
}
return File(new System.Text.UTF8Encoding().GetBytes(sbRtn.ToString()), "text/csv", "FileName.csv");
}
希望这是有帮助的。
这是一个关于使用 C# 创建 csv 文件的简单教程,您将能够对其进行编辑和扩展以满足您自己的需求。
首先,您需要创建一个新的 Visual Studio C# 控制台应用程序,需要遵循一些步骤来执行此操作。
示例代码将在您指定的位置创建一个名为 MyTest.csv的 csv 文件。文件的内容应为 3 个命名列,前 3 行包含文本。
https://tidbytez.com/2018/02/06/how-to-create-a-csv-file-with-c/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace CreateCsv
{
class Program
{
static void Main()
{
// Set the path and filename variable "path", filename being MyTest.csv in this example.
// Change SomeGuy for your username.
string path = @"C:'Users'SomeGuy'Desktop'MyTest.csv";
// Set the variable "delimiter" to ", ".
string delimiter = ", ";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Column 1 Name" + delimiter + "Column 2 Name" + delimiter + "Column 3 Name" + delimiter + Environment.NewLine;
File.WriteAllText(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is text for Column 1" + delimiter + "This is text for Column 2" + delimiter + "This is text for Column 3" + delimiter + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}
}
public static class Extensions
{
public static void WriteCSVLine(this StreamWriter writer, IEnumerable<string> fields)
{
const string q = @"""";
writer.WriteLine(string.Join(",",
fields.Select(
v => (v.Contains(',') || v.Contains('"') || v.Contains(''n') || v.Contains(''r')) ? $"{q}{v.Replace(q, q + q)}{q}" : v
)));
}
public static void WriteCSVLine(this StreamWriter writer, params string[] fields) => WriteCSVLine(writer, (IEnumerable<string>)fields);
}
这应该允许您非常简单地编写 csv 文件。用法:
StreamWriter writer = new ("myfile.csv");
writer.WriteCSVLine("A", "B"); // A,B
(免责声明:我即将推荐一个我参与的项目)
自 2015 年以来,我一直在维护一个开源 CSV 编写器库:https://github.com/jitbit/CsvExport
它与Excel兼容,转义逗号/引号/多行,以时区证明格式导出日期等,它是.NET标准2.0,适用于.NET Core和.NET Framework。
用法:
var myExport = new CsvExport();
myExport.AddRow();
myExport["Region"] = "Denver, USA";
myExport["Sales"] = 100000;
myExport["Date Opened"] = new DateTime(2003, 12, 31);
myExport.AddRow();
myExport["Region"] = "London '"in'" the UK";
myExport["Sales"] = 50000;
myExport["Date Opened"] = new DateTime(2005, 1, 1, 9, 30, 0);
//ASP.NET MVC action example
return File(myExport.ExportToBytes(), "text/csv", "results.csv");
在Nuget上可用:
Install-Package CsvExport
摆脱覆盖问题的一种简单方法是使用 File.AppendText
在文件末尾附加行
void Main()
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText("file.txt"))
{
string first = reader[0].ToString();
string second=image.ToString();
string csv = string.Format("{0},{1}'n", first, second);
sw.WriteLine(csv);
}
}
enter code here
字符串 string_value= 字符串。空;
for (int i = 0; i < ur_grid.Rows.Count; i++)
{
for (int j = 0; j < ur_grid.Rows[i].Cells.Count; j++)
{
if (!string.IsNullOrEmpty(ur_grid.Rows[i].Cells[j].Text.ToString()))
{
if (j > 0)
string_value= string_value+ "," + ur_grid.Rows[i].Cells[j].Text.ToString();
else
{
if (string.IsNullOrEmpty(string_value))
string_value= ur_grid.Rows[i].Cells[j].Text.ToString();
else
string_value= string_value+ Environment.NewLine + ur_grid.Rows[i].Cells[j].Text.ToString();
}
}
}
}
string where_to_save_file = @"d:'location'Files'sample.csv";
File.WriteAllText(where_to_save_file, string_value);
string server_path = "/site/Files/sample.csv";
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(server_path));
Response.WriteFile(server_path);
Response.End();
这是另一个可以轻松创建CSV文件的开源库,Cinchoo ETL
方法1:动态模型
List<dynamic> objs = new List<dynamic>();
dynamic rec1 = new ExpandoObject();
rec1.Id = 10;
rec1.Name = @"Mark";
rec1.JoinedDate = new DateTime(2001, 2, 2);
rec1.IsActive = true;
rec1.Salary = new ChoCurrency(100000);
objs.Add(rec1);
dynamic rec2 = new ExpandoObject();
rec2.Id = 200;
rec2.Name = "Tom";
rec2.JoinedDate = new DateTime(1990, 10, 23);
rec2.IsActive = false;
rec2.Salary = new ChoCurrency(150000);
objs.Add(rec2);
using (var parser = new ChoCSVWriter("emp.csv").WithFirstLineHeader())
{
parser.Write(objs);
}
方法2:POCO模型
List<Emp> objs = new List<Emp>();
Emp rec1 = new Emp();
rec1.Id = 10;
rec1.Name = @"Mark";
objs.Add(rec1);
Emp rec2 = new Emp();
rec2.Id = 200;
rec2.Name = "Tom";
objs.Add(rec2);
using (var parser = new ChoCSVWriter<Emp>("emp.csv").WithFirstLineHeader())
{
parser.Write(objs);
}
有关更多信息,请阅读有关用法的 CodeProject 文章。
只需要添加一个换行"'n'r"
。