主要来源:

博客: https://www.cnblogs.com/rumeng/p/3785748.html      

官网:  http://epplus.codeplex.com/

教程:  https://riptutorial.com/zh-CN/epplus/topic/8070/%E5%BC%80%E5%A7%8B%E4%BD%BF%E7%94%A8epplus

 

            FileInfo newFile = new FileInfo(@"F:\mynewfile.xlsx");
            using (ExcelPackage xlPackage = new ExcelPackage(newFile))//如果mynewfile.xlsx存在,就打开它,否则就在该位置上创建
            {
                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets.Add("Tinned Goods");
                worksheet.Cells[1, 1].Value = "Product";
                worksheet.Cells[2, 1].Value = "Broad Beans";
                worksheet.Cells[3, 1].Value = "String Beans";
                worksheet.Cells[4, 1].Value = "Peas";
                worksheet.Cells[5, 1].Value = "Total";

                worksheet.Cells[1, 2].Value = "Tins Sold";//给单元格赋值             

                ExcelRange cell = worksheet.Cells[2, 2];
                cell.Value = 15;//另一种方式给单元格赋值

                string calcStartAddress = cell.Address;

                worksheet.Cells[3, 2].Value = 32;
                worksheet.Cells[4, 2].Value = 65;

                string calcEndAddress = worksheet.Cells[4, 2].Address;

                worksheet.Cells[5, 2].Formula = string.Format("SUM({0}:{1})", calcStartAddress, calcEndAddress);//使用公式计算值,并赋值给单元格


                worksheet.Column(1).Width = 15;//设置列宽

                xlPackage.Workbook.Properties.Title = "Sample 1";//设置excel的一些属性
                xlPackage.Workbook.Properties.Author = "John Tunnicliffe";
                xlPackage.Workbook.Properties.SetCustomPropertyValue("EmployeeID", "1147");

                xlPackage.Save();//保存Excel表格
简单使用(单元格赋值、使用公式等)

相关文章: