繁体   English   中英

用C#读取csv文件

[英]Reading csv file in C#

我正在开发一个用于生物识别出勤的项目。我有一台拇指打孔机,它以 Excel 格式导出每日报告,用于员工的进出时间。

报告格式:在此处输入图片说明

现在我想开发一个 C# 应用程序来读取这个文件并显示一些关于出勤的报告。我尝试将此文件转换为 csv 并使用下面的代码,但输出为空白。任何帮助将感谢指导如何阅读此文件(xls /csv)?

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var column1 = new List<string>();
                var column2 = new List<string>();
                using (var rd = new StreamReader("db.csv"))
                {
                    while (!rd.EndOfStream)
                    {
                        var splits = rd.ReadLine().Split(';');
                        column1.Add(splits[0]);
                        column2.Add(splits[1]);
                    }
                }
                // print column1
                Console.WriteLine("Column 1:");
                foreach (var element in column1)
                    Console.WriteLine(element);

                // print column2
                Console.WriteLine("Column 2:");
                foreach (var element in column2)
                    Console.WriteLine(element);
            }
            catch (Exception ae)
            {
                Console.WriteLine(ae);
            }
            finally
            {
                Console.WriteLine("ok");
            }
        }
    }
}

你可以找到很多这样做的方法

对于 CSV,您可以使用http://kbcsv.codeplex.com/

要读取 Excel,您需要将 Com 组件添加到您的项目中,即 Microsoft Excel 12.0 对象库,例如您可以参考从 C# 读取 Excel 文件

例如代码

string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties='Excel 8.0;HDR=Yes;'"
using(OleDbConnection connection = new OleDbConnection(con))
{
    connection.Open();
    OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection) 
    using(OleDbDataReader dr = command.ExecuteReader())
    {
         while(dr.Read())
         {
             var row1Col0 = dr[0];
             Console.WriteLine(row1Col0);
         }
    }
}

使用“LinqToExcel” - 那么你可以做这样的事情:

var book = new LinqToExcel.ExcelQueryFactory(@"C:\Path\File.csv");

var query =
    from row in book.Worksheet<MyObject>()
    where row.Status != "Disabled"
    select new
    {
        row.Date,
        row.Description,
    };

使用csv 助手库

var csv = new CsvReader( File.OpenRead( "file.csv" ) );
var myCustomObjects = csv.GetRecords<CustomObject>();

为此,我建议使用LightweightExcelReader 要阅读示例中的电子表格,您可以执行以下操作:


ExcelReader reader = new ExcelReader("path/to/ExcelFile.xlsx");
reader.ReadNext(); //for the purposes of this example we'll skip the first line

/*The code within the while loop will read the data in rows 2-7, then rows 8-13, etc.*/
while (reader.ReadNext())
{
    Console.WriteLine("Header Cell:");
    Console.WriteLine(reader.Value); //Writes the value of cell A2

    reader.ReadNext(); //Moves the reader to the next cell, e.g.. the start of row 3
    do
    {
        Console.WriteLine("Value at cell" + reader.Address);
        Console.WriteLine(reader.Value);
    } while(reader.ReadNextInRow()); //Do/while reads the values in row 3, i.e. "1", "2", "3"

    reader.ReadNext(); //Moves the reader to the start of row 4
    do
    {
        Console.WriteLine("Value at cell" + reader.Address);
        Console.WriteLine(reader.Value);
    } while(reader.ReadNextInRow()); //Reads the only value in row 4, e.g. "21:10"

    reader.ReadNext();
    while(reader.ReadNextInRow()); //Skips row 5    
}

免责声明:我是 LightweightExcelReader 的作者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM