c# - 使用 Open XML SDK 2.0 检索每行和每列中的所有单元格值

标签 c# excel visual-studio-2010 openxml-sdk

我正在使用 Open XML SDK 打开一个 Excel 文件,以从工作表中包含数据的所有行和列中检索单元格值。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ReadingExcel
{
    class RetrieveListOfExcelValues
    {
        static void Main(string[] args)
        {    
            String fileName = @"C:\Users\Documents\TestReadingExcel.xlsx";
            // Comment one of the following lines to test the method separately.
            ReadExcelFileDOM(fileName);   // DOM
        }  //end Main

        static void ReadExcelFileDOM(string fileName)
        {    
            // Open the spreadsheet document for read-only access.
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
            {    
                // Retrieve a reference to the workbook part.
                WorkbookPart wbPart = document.WorkbookPart;
                //Worksheet name that is the the workbook
                string sheetName = "Sheet1";
                // Find the sheet with the supplied name, and then use that
                // Sheet object to retrieve a reference to the first worksheet.
                Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
                Where(s => s.Name == sheetName).FirstOrDefault();

                // Throw an exception if there is no sheet.
                if (theSheet == null)
                {    
                    throw new ArgumentException("sheetName");
                }

                // Retrieve a reference to the worksheet part.
                WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
                string parseCellValue;
                foreach (Row r in theSheet.Elements<Row>())
                {    
                    foreach (Cell c in r.Elements<Cell>())
                    {    
                        Cell theCell = wsPart.Worksheet.Descendants<Cell>().FirstOrDefault();
                        parseCellValue = GetCellValue(theCell, wbPart);

                        Console.Write(parseCellValue + " ");
                        //text = c.CellValue.Text;
                        //Console.Write(text + " ");
                    }    
                }

                Console.WriteLine();
                Console.ReadKey();
            }    
        }    //end ReadExcelFileDOMMethod

        public static string GetCellValue(Cell cell, WorkbookPart wbPart)
        {    
            string value = null;
            if (cell != null)
            {    
                value = cell.InnerText;

                // If the cell represents an integer number, you are done.
                // For dates, this code returns the serialized value that
                // represents the date. The code handles strings and
                // Booleans individually. For shared strings, the code
                // looks up the corresponding value in the shared string
                // table. For Booleans, the code converts the value into
                // the words TRUE or FALSE.
                if (cell.DataType != null)
                {    
                    switch (cell.DataType.Value)
                    {    
                        case CellValues.SharedString:
                            // For shared strings, look up the value in the
                            // shared strings table.
                            var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>()
                                .FirstOrDefault();

                            // If the shared string table is missing, something
                            // is wrong. Return the index that is in
                            // the cell. Otherwise, look up the correct text in
                            // the table.
                            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;
        }  
    }
}

文本没有显示在控制台上,我无法准确地找出我在这段代码中做错了什么。这是我第一次尝试使用 Open XML。我可能忽略了一些东西,或者可能需要移动几行代码。

如何从工作表中的行和列中获取所有单元格值以显示在控制台上?

Excel格式

A1        B1          C1

Fruit    Lot         Date

Banana      4          4/13/2014

Orange      6          5/01/2014

Apple       9          3/14/2014

最佳答案

value = cell.Cellvalue.InnerText;

试一试

关于c# - 使用 Open XML SDK 2.0 检索每行和每列中的所有单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769010/

相关文章:

c# - 使用 StackExchange.Redis 处理暂时性网络错误

c# - 将来自不同 MEF 插件的 ToolStripMenuItem 合并到一个 MenuStrip 中

c# - 发现两个时间间隔之间差异的问题

c# - 局域网中的公共(public)数据库

vba - 检查单元格是否在范围内,如果是,则在 Excel VBA 中返回单词 "Marginal"

VBA:在 Excel 中如何将 Word 文档另存为 PDF?

vba - With Sheets ("") .Range 的正确语法

asp.net - ExpectedResponseUrl 错误是 Visual Studio 2010 web 性能测试

c# - 使用 GhostscriptProcessor 创建 PDF/A

visual-studio-2010 - 创建需要客户端证书的 Web 服务