简体   繁体   English

如何使用OpenXML C#将特定于Excel的行和工作表读取到数据表中

[英]How to read file excel row and sheet specific using openXML C# into data table

如何使用打开的XML C#将Excel特定于行和表的数据读取到数据表中?

OK you need to implement two things: 好的,您需要实现两件事:

  1. Get the desired worksheet object 获取所需的工作表对象
  2. Get the desired cell ie B31 to read the data from 获取所需的单元格,即B31以从中读取数据

Here is code snippet to get the worksheet object by name, and cell object by address: 这是按名称获取工作表对象和按地址获取单元格对象的代码段:

Private Function GetWorksheet(ByRef document As SpreadsheetDocument, ByVal worksheetName As String) As Worksheet
    Dim sheets As IEnumerable(Of Sheet) = document.WorkbookPart.Workbook.Descendants(Of Sheet)().Where(Function(s) s.Name = worksheetName)
    If (sheets.Count = 0) Then
        ' The specified worksheet does not exist.
        Return Nothing
    End If
    Dim worksheetPart As WorksheetPart = CType(document.WorkbookPart.GetPartById(sheets.First.Id), WorksheetPart)

    Return worksheetPart.Worksheet
End Function

Private Function GetCellByAdrress(ByRef ws As Worksheet, addressName As String) As Cell

    Dim sheetData As SheetData = ws.GetFirstChild(Of SheetData)()
    Dim cell As Cell = Nothing

    Dim rowNumber As UInt32 = GetRowIndex(addressName)
    Dim row As Row = GetRow(sheetData, rowNumber)

    ' If the cell you need already exists, return it.
    ' If there is not a cell with the specified column name, insert one.  
    Dim refCell As Cell = row.Elements(Of Cell)().Where(Function(c) c.CellReference.Value = addressName).FirstOrDefault()
    'refCell.Remove()
    If refCell IsNot Nothing Then
        cell = refCell
    Else
        cell = CreateCell(row, addressName)
    End If
    Return cell
End Function

Private Function GetRowIndex(ByVal address As String) As UInt32
    Dim rowPart As String
    Dim l As UInt32
    Dim result As UInt32 = 0

    For i As Integer = 0 To address.Length - 1
        If UInt32.TryParse(address.Substring(i, 1), l) Then
            rowPart = address.Substring(i, address.Length - i)
            If UInt32.TryParse(rowPart, l) Then
                result = l
                Exit For
            End If
        End If
    Next
    Return result
End Function

 Private Function GetRow(ByRef wsData As SheetData, ByVal rowIndex As UInt32) As Row
    Dim row = wsData.Elements(Of Row)().Where(Function(r) r.RowIndex.Value = rowIndex).FirstOrDefault()
    If row Is Nothing Then
        row = New Row()
        row.RowIndex = rowIndex
        wsData.Append(row)
    End If
    Return row
End Function

Private Function CreateCell(row As Row, address As [String]) As Cell
    Dim cellResult As Cell
    Dim refCell As Cell = Nothing

    ' Cells must be in sequential order according to CellReference. 
    ' Determine where to insert the new cell.
    For Each cell As Cell In row.Elements(Of Cell)()
        If String.Compare(cell.CellReference.Value, address, True) > 0 Then
            refCell = cell
            Exit For
        End If
    Next

    cellResult = New Cell()
    cellResult.CellReference = address

    row.InsertBefore(cellResult, refCell)

    Return cellResult
End Function

Code to consume the functions: 使用这些功能的代码:

Function ReadCellValue(address as string, worksheetName as string)    


    dim worksheet = GetWorksheet(docObj, worksheetName)

    dim cellObj as Cell = GetCellByAdrress(worksheet, "B31")
    if cellObj isnot Nothing
    dim value  = cellObj.CellValue
    end if

 End Function

Hope this would help. 希望这会有所帮助。 Let me know in case of any ambiguity. 如果有任何歧义,请告诉我。

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

        private void readExcel(Stream file)
        {
            String sheetName = "Sheet2";
            String delimiter = ";";
            int startColumn = 2;// 2 convert to B 
            int endColumn = 6; // read until column 6
            int startRow = 31; // start read from row 31

            String columnRequest = "Request";
            DataTable dt = new DataTable();
            dt.Columns.Add(columnRequest);
            DataRow dr;
            String stringRequest = "";
            String stringNopek = "Init";
            String value = "";
            int indexRow = 0;
            using (SpreadsheetDocument myDoc = SpreadsheetDocument.Open(file, false))
            {
                WorkbookPart wbPart = myDoc.WorkbookPart;

                indexRow = startRow;
                while (!stringNopek.Equals(""))
                {
                    stringNopek = getCellValue(GetExcelColumnName(startColumn) + indexRow.ToString(), sheetName, wbPart).Trim();
                    stringRequest = stringNopek;
                    if (!stringNopek.Equals(""))
                    {
                        dr = dt.NewRow();
                        for (int i = startColumn + 1; i <= endColumn; i++)
                        {
                            value = getCellValue(GetExcelColumnName(i) + indexRow.ToString(), sheetName, wbPart).Trim();
                            stringRequest += delimiter + value;
                        }
                        dr[columnRequest] = stringRequest;
                        dt.Rows.Add(dr);
                    }
                    indexRow++;
                }
            }

            Session["DataTableRequest"] = dt;

            string output = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                output = output + dt.Rows[i][columnRequest].ToString();
                output += (i < dt.Rows.Count) ? Environment.NewLine : string.Empty;
            }

        }

private string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }

 private int ColumnIndex(string reference)
        {
            int ci = 0;
            reference = reference.ToUpper();
            for (int ix = 0; ix < reference.Length && reference[ix] >= 'A'; ix++)
                ci = (ci * 26) + ((int)reference[ix] - 64);
            return ci;
        }

private String getCellValue(String cellReference, String sheetName, WorkbookPart wbPart)
        {
            Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
              Where(s => s.Name == sheetName).FirstOrDefault();
            if (theSheet == null)
            {
                throw new ArgumentException(sheetName);
            }
            WorksheetPart wsPart =
                (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
            Cell theCell = wsPart.Worksheet.Descendants<Cell>().
              Where(c => c.CellReference == cellReference).FirstOrDefault();

            String value = "";

            if (theCell != null)
            {
                if (theCell.CellValue != null)
                {
                    value = theCell.CellValue.Text;
                }
                else
                {
                    value = value = theCell.InnerText;
                }
                if (theCell.DataType != null)
                {
                    switch (theCell.DataType.Value)
                    {
                        case CellValues.SharedString:

                            var stringTable =
                                wbPart.GetPartsOfType<SharedStringTablePart>()
                                .FirstOrDefault();
                            if (stringTable != null)
                            {
                                value =
                                    stringTable.SharedStringTable
                                    .ElementAt(int.Parse(value)).InnerText;
                            }
                            break;

                        case CellValues.Boolean:
                            switch (value)
                            {
                                case "0":
                                    value = "FALSE";
                                    break;
                                default:
                                    value = "TRUE";
                                    break;
                            }
                            break;
                    }
                }
            }

            return value;
        }

 protected void UploadControl_FileUploadComplete(object sender, FileUploadCompleteEventArgs e){
 Stream strm = e.UploadedFile.FileContent;
readExcel(strm);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何使用OpenXmlWriter(OpenXML SDK 2.5)将单元格附加到Excel工作表中的每一行 - C# How do I append a Cell to every Row in a Excel Sheet using OpenXmlWriter (OpenXML SDK 2.5) 使用OpenXML在列中读取Excel工作表数据 - Read excel sheet data in columns using OpenXML 如何使用 OpenXML C# 解析 Excel 表格中的数字 - How to parse numbers from excel sheet using OpenXML C# 如何使用 OpenXML C# 在 Excel 中隐藏工作表? - How to hide a sheet in Excel using OpenXML C#? 如何使用OpenXML和C#在Excel工作表中获取范围 - how to get range in excel sheet using openxml and c# 如何在C#中使用openXML刷新(刷新Excel工作表中的“全部”按钮)Excel工作表 - How to refresh (Refresh All button in excel sheet) the excel sheet using openXML in C# 如何使用OpenXML和C#在Excel中添加带有单元格数据和样式的新行 - How to add new row with cell data and styles in Excel using OpenXML and C# 如何使用c#从XML文件读取特定数据并将其写入到现有Excel工作表 - How to Read a particular Data from XML file and Write it to an Existing Excel Sheet using c# 如何使用带有c#的OpenXML SDK v2.0将新工作表添加到Excel .xlsx文件中? - How do I add a new sheet to an Excel .xlsx file using the OpenXML SDK v2.0 with c#? 使用 OpenXml 读取 C# 中的 docx 文件 - Read a docx file in C# using OpenXml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM