CsvHelper.Excel icon indicating copy to clipboard operation
CsvHelper.Excel copied to clipboard

MissingMethodException set_LeaveOpen

Open altmank opened this issue 3 years ago • 3 comments

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):

  • CSVHelper: 27.0.2
  • CSVHelper.Excel.Core: 22.1.1

altmank avatar Apr 29 '21 16:04 altmank

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

nefarius avatar Jun 16 '21 18:06 nefarius

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

Released

youngcm2 avatar Jan 07 '22 23:01 youngcm2

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 😃

nefarius avatar Jan 15 '22 04:01 nefarius