Question

First time working with strict type language and having some problems.

Data fetched from MySQL adapter fills a DataTable object.
Column types remains the same as in MySQL table.

Initiating NPOI, creating xls sheet. Next step is writing data in loop. And here is the problem.

http://pastebin.com/NzVqzWE0

Method SetCellValue has overloadings for all needed types, but can it guess for more appropriate overloading automaticly? Am i missing something?

Converting all values to strings cant help because i need number formating in sheet.
Technically this code works, im looking for right way of handling this.

Était-ce utile?

La solution

You can defer the overload resolution to the runtime by using the dynamic keyword:

foreach (DataColumn column in data.Columns)
{
    ICell cell = dataRow.CreateCell(column.Ordinal);
    cell.SetCellValue((dynamic)row[column]);
}

Please note that this will fail with a runtime exception if there is no matching overload.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top