我需要读取一个 excel 文档并将包含在数据库中的单元格中的数据。但是,我注意到的问题是,当我从行中读取数据时,它们并没有按照它们在表单中出现的顺序出现。请问我该如何解决?
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();
}
}
在我用代码阅读的示例 Excel文件中找到的示例文件
下面是我访问存储在行中单元格中的值的方式类型。. .
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();
我尝试使用单元格引用来访问单元格内文
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;
}
}