Openxml 读取指定列的数据

在这个示例中指定"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 C = new List();
            C.Add("A");
            C.Add("B");
            C.Add("C");
            Dictionary> result = new Dictionary>();
            result.Add("A", new List());
            result.Add("B", new List());
            result.Add("C", new List());
            using (SpreadsheetDocument sd = SpreadsheetDocument.Open(path, false))
            {
                WorkbookPart wp = sd.WorkbookPart;
                Sheet sheet = wp.Workbook.Descendants()
                    .Where(s => s.Name == "Sheet1").FirstOrDefault();
                WorksheetPart wsp = wp.GetPartById(sheet.Id) as WorksheetPart;
                SharedStringTablePart sstp = wp.GetPartsOfType()
                    .FirstOrDefault();
                SharedStringTable sst = sstp.SharedStringTable;
                List alph = sst.Descendants()
                    .ToList();
                if (wsp != null)
                {
                    Worksheet ws = wsp.Worksheet;
                    SheetData sda = ws.Descendants().FirstOrDefault();
                    List rows = sda.Descendants().ToList();
                    foreach (Row row in rows)
                    {
                        List cells = row.Descendants().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 list)
        {
            foreach (object i in list)
            {
                Console.WriteLine(string.Format("{0}", i));
            }
        }

        private static object GetValue(Cell cell, ref List 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;
        }
    }
} 
  


你可能感兴趣的:(OpenXml,Office,Development,Spreadsheet,Document,C#)