zoukankan      html  css  js  c++  java
  • CsvHelper

    写66666行两个数字 用CsvHelper里的ExcelSerializer 400ms SaveAs 200ms 共600ms 直接用StreamWriter 150ms
    行数可以超过65536
    worksheet reader

     class Program
        {
            static void Main(string[] args)
            {
                XLWorkbook workbook = new XLWorkbook("E://233.xlsx",XLEventTracking.Disabled);
                var sheet = workbook.Worksheets.First(p => p.Name == "ABC");
                var csv = new CsvReader(new ExcelParser(sheet));
                csv.Configuration.RegisterClassMap<MapA>();
                var records=csv.GetRecords<A>();
                foreach(var re in records)
                {
                    Console.WriteLine(re.ToString());
                }
                foreach (var header in csv.FieldHeaders)
                {
                    Console.WriteLine(header);
                }
            }
            public class MapA:CsvClassMap<A>
            {
                public MapA()
                {
                    Map(m=>m.a).Name("AAA");
                    Map(m=>m.b).Name("BBB");
                }   
            }
            public static List<A> list = new List<A> { new A(1, 2), new A(3, 4) };
            public class A
            {
                public int a
                {
                    get; set;
                }
                public int b
                {
                    get; set;
                }
                public A() { }
                public A(int a,int b)
                {
                    this.a = a;
                    this.b = b;
                }
                public override string ToString()
                {
                    return a + " " + b;
                }
            }
        }
    

    worksheet writer

     class Program
        {
            static void Main(string[] args)
            {
                XLWorkbook workbook = new XLWorkbook(XLEventTracking.Disabled);
                var sheet = workbook.AddWorksheet("ABC");
                var csv = new CsvWriter(new ExcelSerializer(sheet));
                csv.Configuration.RegisterClassMap<MapA>();
                csv.WriteHeader<A>();
                csv.WriteRecords(list);
                workbook.SaveAs("E://233.xlsx");
            }
            public class MapA:CsvClassMap<A>
            {
                public MapA()
                {
                    Map(m=>m.a).Name("AAA");
                    Map(m=>m.b).Name("BBB");
                }   
            }
            public static List<A> list = new List<A> { new A(1, 2), new A(3, 4) };
            public class A
            {
                public int a
                {
                    get; set;
                }
                public int b
                {
                    get; set;
                }
                public A() { }
                public A(int a,int b)
                {
                    this.a = a;
                    this.b = b;
                }
            }
        }
    

    Writer

    class Program
        {
            static void Main(string[] args)
            {
                FileStream file = new FileStream("E://123.csv", FileMode.Open);
                StreamWriter writer = new StreamWriter(file, Encoding.UTF8);
                var csv = new CsvWriter(writer);
                csv.Configuration.RegisterClassMap<MyClassMap>();
                csv.WriteHeader<A>();
                csv.WriteRecords(list);
                writer.Flush();
                writer.Close();
            }
            public class MyClassMap : CsvClassMap<A>
            {
                public MyClassMap()
                {
                    Map(m => m.a).Name("AAA");
                    Map(m => m.b).Name("BBB");
                    Map(m => m.c);
                }
            }
            public static List<A> list = new List<A>() { new A(1, 2, 3), new A(4, 5, 6) };
    
            public class A
            {
                public A() { }
                public int a
                {
                    get; set;
                }
                public int b
                {
                    get; set;
                }
                public int c
                {
                    get; set;
                }
                public A(int a,int b,int c)
                {
                    this.a = a;
                    this.b = b;
                    this.c = c;
                }
                public override string ToString()
                {
                    return a + " " + b + " " + c;
                }
            }
        }
    

    Reader

        class Program
        {
            static void Main(string[] args)
            {
                FileStream file = new FileStream("E://123.csv", FileMode.Open);
                StreamReader reader = new StreamReader(file, Encoding.UTF8);
                var csv = new CsvReader(reader);
                csv.Configuration.RegisterClassMap<MyClassMap>();
                var records=csv.GetRecords<A>();
                foreach(var re in records)
                {
                    //GetRecords在foreach里会自动调用Read(),GetRecord要手动调用Read()
                    Console.WriteLine(re.ToString());
                    Console.WriteLine(csv.GetField("BBB"));
                    Console.WriteLine(csv.GetField(1));
                }
                var headers = csv.FieldHeaders;//调用过Read()才能获取header
                reader.Close();
                file.Close();
            }
            public class MyClassMap : CsvClassMap<A>
            {
                public MyClassMap()
                {
                    Map(m => m.a).Name("AAA");
                    Map(m => m.b).Name("BBB");
                    Map(m => m.c);
                }
            }
            public static List<A> list = new List<A>() { new A(1, 2, 3), new A(4, 5, 6) };
    
            public class A
            {
                public A() { }
                public int a
                {
                    get; set;
                }
                public int b
                {
                    get; set;
                }
                public int c
                {
                    get; set;
                }
                public A(int a,int b,int c)
                {
                    this.a = a;
                    this.b = b;
                    this.c = c;
                }
                public override string ToString()
                {
                    return a + " " + b + " " + c;
                }
            }
        }
    
  • 相关阅读:
    LeetCode中等题(三)
    java之使用poi对excel的.xls和.xlsx访问
    JAVA编程-----------40、字符串排序
    JAVA编程------------38、写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度
    JAVA编程---------37、n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡是报到3的人退出圈子,问最后留下的是原来第几号的那位。
    JAVA编程--------------36、有n个整数,使其前面各数顺序向后移m个位置最后m个数变成前面m个数
    JAVA编程-------------24、将一组数的最大数放在第一位,最小的数放在最后一位
    JAVA编程------------33、打印杨辉三角
    JAVA编程--------------32、取一个整数的4-7位
    JAVA编程---------------31、将一个数组逆序输出
  • 原文地址:https://www.cnblogs.com/HaibaraAi/p/5488268.html
Copyright © 2011-2022 走看看