写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;
}
}
}
05-07 15:17