0

我需要读取一个 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;
    }
}
4

1 回答 1

0

当您遍历单个单元格时,您需要查看CellReference属性(它是XML 文件中单元格r的属性)。这包含 A1 格式的单元格地址,其中 A 是列,1 是行。

例如,第 68 行上的第 233 列将是 HY68。您可能会发现这个问题对于如何生成列字母很有用。然后,您可以检查作为CellReference您正在检查的行的后代的每个单元格,提取列字母并使用switch语句来填充您的相关部分ItemDetail

我可以看到避免迭代单个单元格的唯一明显方法可能是使用 XPath 直接从底层 XML/.xslx 文件中提取它们。不知道如何在 C# 中使用SpreadsheetDocument对象及其后代来做到这一点。

您还可以通过CellReference属性对现有查询之一进行排序,这将使单元格按顺序排列,但可能会导致空单元格出现问题(即返回的第 10 个单元格可能不是第 10 列中的单元格)


编辑:处理丢失的单元格/空引用只需要您在访问.InnerText属性之前检查引用。

Cell theCell = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(1) + row.RowIndex.ToString()).FirstOrDefault();

String theCellValue = "";

if (theCell != null)
{
  theCellValue = theCell.InnerText;
}

将很多包装在一个函数中可能会更整洁,该函数将 theCell作为参数并返回String包含 theInnerText或空字符串

于 2012-10-05T00:46:15.633 回答