为什么无法通过 OpenXML 获取 excel 文件的单元格值?

Why can't get cell value of excel file by OpenXML?

我必须阅读 excel 文件作为示例:

我的代码阅读文件:

        SPWeb web = SPContext.Current.Web;          
        SPFile file = web.GetFile(path);
        Stream dataStream = file.OpenBinaryStream();
        // Open the document .
        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(dataStream, true))
        {
            WorksheetPart worksheetPart = GetWorksheetPartByName(spreadSheet, "Sheet1");

            if (worksheetPart != null)
            {
                SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
                DataTable dt = new DataTable();
                dt.Columns.Add("Id");
                dt.Columns.Add("Title");
                dt.Columns.Add("Gender");
                dt.Columns.Add("Birthday");

                foreach (Row r in sheetData.Elements<Row>())
                {
                    if (r.RowIndex.Value > 1)
                    {
                        dt.Rows.Add();
                        int i = 0;
                        foreach (Cell c in r.Elements<Cell>())
                        {
                            dt.Rows[dt.Rows.Count - 1][i] = c.CellValue.Text;
                            i++;
                        }
                    }
                    
                }                  
                
                GridView1.DataSource = dt;
                GridView1.DataBind();                  

               
            }
        }

Gridview 结果只获得正确的第一列:

为什么不能通过 OpenXML 获取单元格的文本值?

您可以通过使用 DataTable

打开 XML 来完成此操作

//将数据表转换为excel字节内存流

                dte=DataTable();    //Your DataTable

                XLWorkbook wb = new XLWorkbook();
                wb.Worksheets.Add(dte, SheetName);

                var Excelbytes = new byte[0];
                using (var ms = new MemoryStream())
                {
                    wb.SaveAs(ms);
                    Excelbytes = ms.ToArray();    
                }

                MemoryStream memorystream = new MemoryStream();

//将内存流转换为excel

        using (MemoryStream memStream = new MemoryStream())
                    {
                        memStream.Write(byteArray, 0, byteArray.Length);
                        XLWorkbook wb = new XLWorkbook(memStream, XLEventTracking.Disabled);
          
                        string myName = Server.UrlEncode(FileName + ".xlsx");
                        MemoryStream stream = (MemoryStream)GetStream(wb);

                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment; filename=" + myName);
                        Response.ContentType = "application/vnd.ms-excel";
                        Response.BinaryWrite(stream.ToArray());
                        Response.End();
                    }

要读取单元格的文本值,我必须检查数据类型 SharedString, DateTime,并转换值:

int i = 0;
foreach (Cell c in r.Elements<Cell>())
{
    var value = c.CellValue.Text;
    if (c.DataType != null && c.DataType == CellValues.SharedString)
    {
        var stringId = Convert.ToInt32(c.InnerText); 
        value=workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(stringId).InnerText;
    }
    if (i == 3&& value!="")
    {
        value = DateTime.FromOADate(double.Parse(value)).ToString ("yyyy/MM/dd");
    }
    dt.Rows[dt.Rows.Count - 1][i] = value;
    i++;
}