如何在 C# 中创建 CSV 文件
How do I Create a CSV file in C#
我是文件系统新手。我需要创建一个简单的 csv 文件,并需要在文件中写入字符串并读回。
我在创建的文件中得到一些 unicode 值。我如何通过创建 csv 并从中读取来写入字符串值。
到目前为止我已经写完了。这里需要一点帮助。
下面是我的代码。
static void Main()
{
string folderName = @"D:\Data";
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
System.IO.Directory.CreateDirectory(pathString);
string fileName = System.IO.Path.GetRandomFileName();
pathString = System.IO.Path.Combine(pathString, fileName);
Console.WriteLine("Path to my file: {0}\n", pathString);
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
{
byte a = 1;
fs.WriteByte(a);
}
}
}
// Read and display the data from your file.
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
您可以使用 streamwriter 编写 csv 文件。您的文件将位于 bin/Debug(如果 运行 调试模式且未另行说明)。
var filepath = "your_path.csv";
using (StreamWriter writer = new StreamWriter(new FileStream(filepath,
FileMode.Create, FileAccess.Write)))
{
writer.WriteLine("sep=,");
writer.WriteLine("Hello, Goodbye");
}
您可以使用适当的扩展名创建特定的文件类型,在本例中为“.csv”。
下面是一个使用文件路径和随机字符串的简单示例。
using System;
using System.IO;
namespace CSVExample
{
class Program
{
static void Main(string[] args)
{
string data = "Col1, Col2, Col2";
string filePath = @"File.csv";
File.WriteAllText(filePath, data);
string dataFromRead = File.ReadAllText(filePath);
Console.WriteLine(dataFromRead);
}
}
}
我是文件系统新手。我需要创建一个简单的 csv 文件,并需要在文件中写入字符串并读回。
我在创建的文件中得到一些 unicode 值。我如何通过创建 csv 并从中读取来写入字符串值。
到目前为止我已经写完了。这里需要一点帮助。
下面是我的代码。
static void Main()
{
string folderName = @"D:\Data";
string pathString = System.IO.Path.Combine(folderName, "SubFolder");
System.IO.Directory.CreateDirectory(pathString);
string fileName = System.IO.Path.GetRandomFileName();
pathString = System.IO.Path.Combine(pathString, fileName);
Console.WriteLine("Path to my file: {0}\n", pathString);
if (!System.IO.File.Exists(pathString))
{
using (System.IO.FileStream fs = System.IO.File.Create(pathString))
{
{
byte a = 1;
fs.WriteByte(a);
}
}
}
// Read and display the data from your file.
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
您可以使用 streamwriter 编写 csv 文件。您的文件将位于 bin/Debug(如果 运行 调试模式且未另行说明)。
var filepath = "your_path.csv";
using (StreamWriter writer = new StreamWriter(new FileStream(filepath,
FileMode.Create, FileAccess.Write)))
{
writer.WriteLine("sep=,");
writer.WriteLine("Hello, Goodbye");
}
您可以使用适当的扩展名创建特定的文件类型,在本例中为“.csv”。 下面是一个使用文件路径和随机字符串的简单示例。
using System;
using System.IO;
namespace CSVExample
{
class Program
{
static void Main(string[] args)
{
string data = "Col1, Col2, Col2";
string filePath = @"File.csv";
File.WriteAllText(filePath, data);
string dataFromRead = File.ReadAllText(filePath);
Console.WriteLine(dataFromRead);
}
}
}