youngcm2 / CsvHelper.Excel

Apache License 2.0
65 stars 33 forks source link

MissingMethodException set_LeaveOpen #13

Open altmank opened 3 years ago

altmank commented 3 years ago

Trying to use ExcelWriter.WriteRecords and I get:

System.MissingMethodException: Method not found: 'Void CsvHelper.Configuration.CsvConfiguration.set_LeaveOpen(Boolean)'. at CsvHelper.Excel.ExcelWriter..ctor(Stream stream, String sheetName, CultureInfo culture, Boolean leaveOpen)

Versions (please complete the following information):

nefarius commented 3 years ago

I am facing the same issue; quick and dirty (well, very dirty) "solution" to get it going on my end by using reflection:

var records = new List<ReportRecord>();

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    NewLine = Environment.NewLine,
    Encoding = Encoding.UTF8,
    LeaveOpen = true // very important, or MemoryStream will be disposed
};

await using var ms = new MemoryStream();
// use private constructor to bypass faulty line
await using (var excelWriter = (ExcelWriter) Activator.CreateInstance(
    typeof(ExcelWriter),
    BindingFlags.Instance | BindingFlags.NonPublic,
    null,
    new object[] {ms, "activity_report_last14days", config},
    null,
    null
))
{
    excelWriter.Context.RegisterClassMap<ReportRecordMap>();
    await excelWriter.WriteRecordsAsync(records);
}

ms.Seek(0, SeekOrigin.Begin);
// use stream for whatever purpose

Disclaimer: this will most probably blow up sooner or later but I needed a working solution quickly 😅

Cheers

youngcm2 commented 2 years ago

@nefarius, @altmank Can you see if your issue is resolved? There was a change to leaveOpen

Released

nefarius commented 2 years ago

I currently have no active project I could test it with but judging by the changelog it should be good, thanks! Maybe @altmank can test it 😃