Pregunta

In my project I'm using an interop connection (pre-4.5 .NET WPF app) to convert 2 columns into easy to work with List 's. The problem here lies within the fact that one of both columns I'm reading into has a lot of empty values; mostly nulls; and the selection process decides to skip them instead of filling in an empty value, so now I am left with 2 uneven colums that no longer represent an 2 by X excel sheet (X is around 1500 here, so this method is fairly time efficient);

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(pathName, 0, true, 5,"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range range = xlWorkSheet.UsedRange;

range = xlWorkSheet.UsedRange.Columns[1];
System.Array myvals = (System.Array)range.Cells.Value;
xmlData.AddRange(myvals.OfType<object>().Select(O => O.ToString()).ToList());

range = xlWorkSheet.UsedRange.Columns[2];
myvals = (System.Array)range.Cells.Value;
xmlCode.AddRange(myvals.OfType<object>().Select(P => P.ToString()).ToList());

Any information on how to retain the null values in the xmlData | xmlCode Lists would be highly appreciated. As far as I've been able to point it down myself; it has something to do with the selection process. All the values are present in the System.Array cast of the range.

Thanks!

¿Fue útil?

Solución

try this:

xmlData.AddRange(myvals.Cast<object>().Select(O => O == null ? 
                                                      string.Empty : 
                                                      O.ToString()).ToList());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top