Openxml 读取指定列的数据

本文提供了一个使用C#语言通过OpenXml库读取并解析Excel文件的示例,重点关注如何读取指定列的数据,并通过正则表达式匹配单元格引用来获取所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这个示例中指定"A","B","C"为需要读取的列。

using System.Windows.Forms;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Text.RegularExpressions;

namespace ConsoleApplication13
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel Document|*.xlsx";
            ofd.Multiselect = false;
            ofd.ShowDialog();
            string path = ofd.FileName;
            List<string> C = new List<string>();
            C.Add("A");
            C.Add("B");
            C.Add("C");
            Dictionary<string, List<object>> result = new Dictionary<string, List<object>>();
            result.Add("A", new List<object>());
            result.Add("B", new List<object>());
            result.Add("C", new List<object>());
            using (SpreadsheetDocument sd = SpreadsheetDocument.Open(path, false))
            {
                WorkbookPart wp = sd.WorkbookPart;
                Sheet sheet = wp.Workbook.Descendants<Sheet>()
                    .Where(s => s.Name == "Sheet1").FirstOrDefault();
                WorksheetPart wsp = wp.GetPartById(sheet.Id) as WorksheetPart;
                SharedStringTablePart sstp = wp.GetPartsOfType<SharedStringTablePart>()
                    .FirstOrDefault();
                SharedStringTable sst = sstp.SharedStringTable;
                List<SharedStringItem> alph = sst.Descendants<SharedStringItem>()
                    .ToList();
                if (wsp != null)
                {
                    Worksheet ws = wsp.Worksheet;
                    SheetData sda = ws.Descendants<SheetData>().FirstOrDefault();
                    List<Row> rows = sda.Descendants<Row>().ToList();
                    foreach (Row row in rows)
                    {
                        List<Cell> cells = row.Descendants<Cell>().ToList();
                        foreach (Cell cell in cells)
                        {
                            Regex rege = new Regex("([A-Z]{1,3})");
                            MatchCollection Matchs = rege
                                .Matches(cell.CellReference.Value);
                            switch (Matchs[0].Value)
                            {
                                case "A":
                                    result["A"].Add(GetValue(cell, ref alph));
                                    break;
                                case "B":
                                    result["B"].Add(GetValue(cell, ref alph));
                                    break;
                                case "C":
                                    result["C"].Add(GetValue(cell, ref alph));
                                    break;
                            }
                        }
                    }
                    Console.WriteLine("=====================A=================");
                    ShowValue(result["A"]);
                    Console.WriteLine("=====================B=================");
                    ShowValue(result["B"]);
                    Console.WriteLine("=====================C=================");
                    ShowValue(result["C"]);
                    Console.ReadKey();
                }
            }
        }

        private static void ShowValue(List<object> list)
        {
            foreach (object i in list)
            {
                Console.WriteLine(string.Format("{0}", i));
            }
        }

        private static object GetValue(Cell cell, ref List<SharedStringItem> alph)
        {
            object result = null;
            if (cell.DataType != null && cell.DataType == CellValues.SharedString)
            {
                result = alph[int.Parse(cell.CellValue.Text)].Text.Text;
            }
            else
            {
                result = cell.CellValue.Text;
            }
            return result;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值