码迷,mamicode.com
首页 > Windows程序 > 详细

C#使用EPPlus读写excel

时间:2020-12-02 11:57:52      阅读:14      评论:0      收藏:0      [点我收藏+]

标签:epp   mda   com   creat   inf   ack   new   str   omd   

1.输入授权

     //输入授权码
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

2.写入excel

  //写入excel
            DataTable dt = new DataTable("测试表");
            dt.Columns.Add(new DataColumn("ID"));
            dt.Columns.Add(new DataColumn("Name"));
            for (int i = 0; i < 10; i++)
            {
                var dr = dt.NewRow();
                dr[0] = i;
                dr[1] = "" + i + "个名称";
                dt.Rows.Add(dr);
            }

            using (ExcelPackage package = new ExcelPackage())
            {
                ExcelWorksheet sheet = package.Workbook.Worksheets.Add(dt.TableName);
                sheet.Cells.LoadFromDataTable(dt);
                sheet.Cells["C1"].Value = "我是C1";
                using (Stream stream = new FileStream("D:\\123.xlsx", FileMode.Create))
                {
                    package.SaveAs(stream);
                }
            }

3.读取excel

 //读取excel

            using (var package = new ExcelPackage(new FileInfo("D:\\123.xlsx")))
            {
                var sheet = package.Workbook.Worksheets[0];
                var colCount = sheet.Dimension.End.Column;
                var rowCount = sheet.Dimension.End.Row;
                 
                for (int r = sheet.Dimension.Start.Row; r < rowCount; r++)
                {
                    string rowInfo = "";
                    for (int c = sheet.Dimension.Start.Column; c <= colCount; c++)
                    {
                        var value = sheet.GetValue<string>(r, c);

                        rowInfo = rowInfo + "  "+value;
                    }  
                    Console.WriteLine("数据:"+rowInfo);
                }
            }

 

C#使用EPPlus读写excel

标签:epp   mda   com   creat   inf   ack   new   str   omd   

原文地址:https://www.cnblogs.com/lizhijian/p/14046667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!