I have a requirement to read an excel document and place the data contained in the cells in the database. However, I problem I notice is that when I got to read the data out of the rows, they do not come out in the order they appear in the form. How do I work this out please?
public void getrowdata(){
IEnumerable<Row> dataRows = from row in s.worksheetpart.Worksheet.Descendants<Row>()
where row.RowIndex > 6
select row;
// extract the data in the row in order
foreach (Row row in dataRows)
{
var cellValues = from cell in row.Descendants<Cell>()
select ((cell.CellValue != null && cell.DataType != null && cell.DataType.HasValue)
&& (sharedString.HasChildren && int.Parse(cell.CellValue.InnerText) < sharedString.ChildElements.Count)
? sharedString.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText
: ((cell.CellValue != null && cell.CellValue.InnerText != null) ? cell.CellValue.InnerText : String.Empty));
//--cellValues.toArray() and then access each cell via index in array
}
public void ReadDSheetsToBuffer()
{
try
{
//Open the Excel workbook.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(file.FullName, true))
{
//References to the workbook and Shared String Table.
workBook = document.WorkbookPart.Workbook;
workSheets = workBook.Descendants<Sheet>();
sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
ExtractSheetstoMemory2(document);
}
}
catch (Exception ex)
{
throw ex.GetBaseException();
}
}
Sample File found at Sample Excel File I read with the code
And below is the type of way I access the values stored up in the cells in the row. . .
if (values[228] != null)
itemdetail.Custom1 = rowvalues[228].Trim();
if (values[229] != null)
itemdetail.Custom2 = rowvalues[229].Trim();
if (values[230] != null)
itemdetail.Custom3 = rowvalues[230].Trim();
if (values[231] != null)
itemdetail.Custom4 = rowvalues[231].Trim();
if (values[232] != null)
itemdetail.Custom5 = rowvalues[232].Trim();
if (values[233] != null)
itemdetail.Custom6 = rowvalues[233].Trim();
My Attempt at using cell reference to access the cell innertext
foreach (Row row in dataRows)
{
if (row.RowIndex > 6)
{
String theCell = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(1) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell2 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(2) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell3 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(3) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell4 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(4) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell5 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(5) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell6 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(6) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell7 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(7) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell8 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(8) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell9 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(9) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell10 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(10) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell11 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(11) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell112 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(12) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell13 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(13) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell14 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(14) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell15 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(15) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
}
}