亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 CsvHelper 從 excel 以動態方式寫入 csv 文件 C#

使用 CsvHelper 從 excel 以動態方式寫入 csv 文件 C#

C#
慕仙森 2023-08-13 15:44:33
我正在嘗試從 Excel 文件生成 CSV 文件。我有不同的 Excel 文件,我想讀取它們并生成 CSV 文件。我想這一定很容易,但我遇到了一些麻煩。我的想法是讀取 Excel 文件的第一行(例如標題),然后讀取其他行作為值,這樣我就可以編寫 CSV。
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

public class Program

{

    public static void Main()

    {

        string filePath = "C:\\Users\\{User}\\Desktop\\sample.xlsx";

        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

        List<List<object>> rows = new List<List<object>>();

        List<object> row = new List<object>();

        // Excel Reader

        using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))

        {

            using (var reader = ExcelReaderFactory.CreateReader(stream, new ExcelReaderConfiguration

            {

                FallbackEncoding = Encoding.GetEncoding(1252),


            }))

            {

                do

                {

                    while (reader.Read())

                    {

                        row.Clear();

                        for (int i = 0; i < reader.FieldCount; i++)

                        {

                            var field = reader.GetValue(i);

                            if (field is int valueInt)

                            {

                                row.Add(valueInt);

                            }

                            else if (field is bool valueBool)

                            {

                                row.Add(valueBool);

                            }

                            else if (field is DateTime valueDate)

                            {

                                //row.Add(valueDate); You can write any condition there

                                row.Add(valueDate.Year);

                            }

                            else if (field is TimeSpan valueTime)

                            {

                                row.Add(valueTime);

                            }

                            else if (field is string valueString)

                            {

                                row.Add(valueString);

                            }

                            else if (field is null)

                            {

                                row.Add(field);

                            }

                        }

                        if (row.Any())

                        {

                            rows.Add(row);

                        }

                    }

                } while (reader.NextResult());


            }

        }

        // CSV Writer

        using (var writer = new StreamWriter("C:\\Users\\{User}\\Desktop\\sample.csv"))

        using (var csv = new CsvWriter(writer))

        {

            foreach (var i in rows)

            {

                foreach (var field in i)

                {

                    csv.WriteField(field);

                }

                csv.NextRecord();

            }

        }

    }


}

該程序將 Excel (sample.xlsx) 文件轉換為 CSV(sample.csv) 文件。


查看完整回答
反對 回復 2023-08-13
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

創建一個類文件,其中包含與 Excel 列相同的屬性。解析您的 Excel 文件并將其加載到您的類的實例中。寫一個簡單的程序如下


void Main()

{

    var records = new List<Foo>

    {

        new Foo { Id = 1, Name = "one" },

    };


    using (var writer = new StreamWriter("path\\to\\file.csv"))

    using (var csv = new CsvWriter(writer))

    {

        csv.WriteRecords(records);

    }

}


public class Foo

{

    public int Id { get; set; }

    public string Name { get; set; }

}

其中 Foo 是類文件。希望這可以幫助


查看完整回答
反對 回復 2023-08-13
  • 2 回答
  • 0 關注
  • 262 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號