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

loveless1994 2020-03-26 07:44:33
小弟是我一个JAVA程序员,公司疫情问题急需操作没办法赶鸭子上架了
如题!!希望前辈们指教一下,感激不尽!
...全文
323 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
loveless1994 2020-03-31
  • 打赏
  • 举报
回复
谢谢各位已经解决了
  • 打赏
  • 举报
回复
 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编写代码
艾克是大叔 2020-03-28
  • 打赏
  • 举报
回复
EPPLUS 4.X版本 方便易用 不操心。但是你说的反射是什么?
lindexi_gd 2020-03-27
  • 打赏
  • 举报
回复
担心你不会用,我写了一个呆魔,放在 github 请点击这个链接访问 https://github.com/lindexi/lindexi_gd/tree/21318ca39e614382a512ea354dfebf31b9fccf8e/Babukeelleneeoai
lindexi_gd 2020-03-27
  • 打赏
  • 举报
回复
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);
}
}
}
}
}
}

lindexi_gd 2020-03-27
  • 打赏
  • 举报
回复
试试 openxml sdk 超级简单,但是要求你会点C#哦,虽然入门也就两天时间

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧