Hi,
I'm finding it difficult to get to grips with OpenXML.
All I need to do it read the rows and cells into an object[][].
here is what I have:
SpreadsheetDocument doc = SpreadsheetDocument.Open(new MemoryStream(file), false);
List<Product> products = new List<Product>();
foreach (OpenXmlWorksheetSelector sheet in doc.WorkbookPart.WorksheetParts.Select((ws, i) => OpenXmlWorksheetSelector.Create(doc, ws, i)).Where(worksheetSelector))
{
object[][] cells =
sheet.Worksheet.Descendants<Row>()
.Select(r => r.Descendants<Cell>().Select(c => c.InnerText).ToArray())
.ToArray();
var p = Products(columnDetailSelectors, cells);
if (p != null)
products.AddRange(p);
}
The error is "The part has been destroyed"
I split out the query and found that sheet.Worksheet.Descendants<row>() throws the error and in fact most properties of 'sheet' are the same >_<
I know the select looks odd, so I'll break that down too:
OpenXmlWorksheetSelector.Create(doc, ws, i) inherits WorksheetPart to make the selection of worksheets to be processed easier:
public class OpenXmlWorksheetSelector : WorksheetPart
{
public string WorkBookName { get; set; }
public string Name { get; set; }
public int Index { get; set; }
public static OpenXmlWorksheetSelector Create(SpreadsheetDocument doc, WorksheetPart part, int index)
{
return new OpenXmlWorksheetSelector
{
Index = index,
Name = doc.WorkbookPart.Workbook.Descendants<Sheet>().ElementAt(index).Name
};
}
}
Originally I was using Interop but I had issues when I tried to use is on my web server. The code has evolved from there so I probably have tunnel vision for my approach. I am also using "ExcelDataReader" for older files but I haven't even begun to test that yet.
Any help or advice is much appreciated
Thanks
Andy ^_^