导出逗号分隔值文本文件以在Excel中使用

在我的C#winforms程序中,我想创build一个导出函数,它将创build逗号分隔的文本文件或csv。 我不确定如何做到这一点的逻辑是最好的方法。 我的导出文件将是这样的:

Family Name, First Name, Sex, Age Dekker, Sean, Male, 23 Doe, John, Male, 40 

所以第一行我想成为列的名称,其余的应该被视为值。 以后用这种方式可以吗? 或者我不应该包含列名?

很高兴听到你的经验!

肖恩,

对不起没有足够的特权点直接评论你的post。 我想你可能会在这里混淆CSV和Excel文件。 CSV只是一个文本文件,每个值用逗号分隔,没有特别的格式化等.Excel将显示CSV文件,因为它知道如何打开它们,但是您可以在记事本中轻松打开它们。

Excel .xslx文件是不同的,可以包含各种不同的格式,图表等。要格式化这些文件,重要的是要了解.xslx文件本质上是拉链。 所以首先要创build一个包含一些数据的excel文件,保存它,然后将扩展名重命名为.zip

打开现在创build的zip文件,您将看到许多不同的文件夹和文件,其中最重要的是您的目的是XL目录。 在这个文件夹中,您将看到一个共享stringxml文件和一个工作表文件夹。

让我们开始进入工作表文件夹并打开sheet1.xml。 找那个说“

如果在这个列中有文本,也就是说excel应该读作文本的数据,那么你将得到类似于0的东西。这表示单元格A1的types是stringt =“s”,并且该值将被作为第一个值在SharedStrings.xml文件中0

如果在单元格中有一个数字,那么你可能有类似于234的东西。在这种情况下,Excel知道在这个单元格中使用值234。

所以在你的情况下,你将需要做到以下几点:

1:在C#中创buildexcel文档 – 这里有一些库可用

2:以zipforms打开excel文件

3:在你的情况下修改样式和工作表的XML文件

4:保存文档

这绝对是好事(说明明显的…)。 Excel有一个小小的checkbox,允许用户导入使用第一行作为列标题,如果他们select它。

我也build议你在每个数据的开始处留出空格,这是没有必要的。

一般来说,包含列标题的最佳实践是,不这样做的唯一理由是外部程序,在这个外部程序中,你没有控制权访问你的数据,而没有意识到第一行是列标题,哪些不能被改变。

要创build导出function,应该这样工作:

 private static List<Person> people = new List<Person>(); static void Main(string[] args) { // add some people people.Add( new Person() { firstName = "John", familyName = "Smith", sex = Sex.Male, age = 12 } ); people.Add( new Person() { firstName = "Mary", familyName = "Doe", sex = Sex.Female, age = 25 } ); // write the data Write(); } static void Write() { using (TextWriter tw = new StreamWriter(@"c:\junk1\test.csv", false)) { // write the header tw.WriteLine("Family Name, First Name, Sex, Age"); // write the details foreach(Person person in people) { tw.WriteLine(String.Format("{0}, {1}, {2}, {3}", person.familyName, person.firstName, person.sex.ToString(), person.age.ToString())); } } } } /// <summary> /// Applicable sexes /// </summary> public enum Sex { Male, Female } /// <summary> /// holds details about a person /// </summary> public class Person { public string familyName { get; set; } public string firstName { get; set; } public Sex sex { get; set; } public int age { get; set; } } 

你可以使用数据集来做到这一点。

请参阅这里

  //Why not save the lines to a List<string> object, first List<sting>Object.Add//("your headers"), use the string.Join("," "your Header Array string[]" do not add //(+",") the .Join extension menthod will handle that for you. 

//这里是一个例子//如果您使用SQL Reader从数据库中检索标题值

  var reader = sqlcmdSomeQueryCommand.ExecuteReader(); var columns = new List<string>(); //get all the field names from the Columns var for (int intCounter = 0; intCounter < reader.FieldCount; intCounter++) { columns.Add(reader.GetName(intCounter)); } strarryTmpString = columns.ToArray(); string TmpFields = string.Join(", ", strarryTmpString); columns.Clear(); columns.Add(TmpFields); //you can save the TmpFieldList to later add the rest of your comma delimited fields write line by line in a foreach loop or use the List<string> object .foreach(delegate(string delString) { someStreamWriterObject.WriteLine(delString) });