c# 中WPF 如何读取EXCEL通过反射的方式

本文介绍使用C#和WPF通过反射读取Excel文件的方法。利用openxml sdk简化操作,示例代码展示了如何读取单元格和行数据,以及通过反射机制解析Excel属性。

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

c# 中WPF 如何读取EXCEL通过反射的方式

试试 openxml sdk 超级简单,但是要求你会点C#哦,虽然入门也就两天时间

1. 通过 NuGet 安装 DocumentFormat.OpenXml 库

2. 试试下面代码

void Main()
{
    string fileName = @"c:\path\to\my\file.xlsx";

    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
        {
            WorkbookPart workbookPart = doc.WorkbookPart;
            SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
            SharedStringTable sst = sstpart.SharedStringTable;

            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
            Worksheet sheet = worksheetPart.Worksheet;

            var cells = sheet.Descendants<Cell>();
            var rows = sheet.Descendants<Row>();

            Console.WriteLine("Row count = {0}", rows.LongCount());
            Console.WriteLine("Cell count = {0}", cells.LongCount());

            // One way: go through each cell in the sheet
            foreach (Cell cell in cells)
            {
                if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
                {
                    int ssid = int.Parse(cell.CellValue.Text);
                    string str = sst.ChildElements[ssid].InnerText;
                    Console.WriteLine("Shared string {0}: {1}", ssid, str);
                }
                else if (cell.CellValue != null)
                {
                    Console.WriteLine("Cell contents: {0}", cell.CellValue.Text);
                }
             }

             // Or... via each row
             foreach (Row row in rows)
             {
                 foreach (Cell c in row.Elements<Cell>())
                 {
                     if ((c.DataType != null) && (c.DataType ==           CellValues.SharedString))
                     {
                         int ssid = int.Parse(c.CellValue.Text);
                         string str = sst.ChildElements[ssid].InnerText;
                         Console.WriteLine("Shared string {0}: {1}", ssid, str);
                     }
                     else if (c.CellValue != null)
                     {
                         Console.WriteLine("Cell contents: {0}", c.CellValue.Text);
                     }
                 }
             }
         }
     }
 }

EPPLUS 4.X版本 方便易用 不操心。但是你说的反射是什么?

 public static List<T> Import<T>(string filePath)
        {
            List<T> rst = new List<T>();
            System.Type t = typeof(T);
            ExcelAttribute excAttr = t.GetCustomAttribute<ExcelAttribute>(false);
            List<ProperyColumnAttributes> propAttrs = new List<ProperyColumnAttributes>();
            foreach (var prop in t.GetProperties())
            {
                ExcelColumnAttribute attr = prop.GetCustomAttribute<ExcelColumnAttribute>(false);
                if (attr != null) propAttrs.Add(new ProperyColumnAttributes { Attribute = attr, Property = prop });
            }
            using (FileStream fs = File.OpenRead(filePath))
            {
                IWorkbook workBook = null;
                if (filePath.IndexOf(".xlsx") > 0)
                    workBook = new XSSFWorkbook(fs);    // 2007版本  
                else if (filePath.IndexOf(".xls") > 0)   // 2003版本  
                    workBook = new HSSFWorkbook(fs);
                else
                {
                    try { workBook = new XSSFWorkbook(fs); } catch { }
                    if (workBook == null)
                        workBook = new HSSFWorkbook(fs);
                }
                ISheet sheet;

你是想用这种反射吗?传入一个类,通过特性灵活获取excel的信息,不用为每一种款式的excel编写论坛发帖机代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值