C# 中EPPlus基本功能测试-(1)

 几年前就使用过EPPlus来读写Excel,后来没有这方面的需求,没有再更新该组件来测试,最近整理测试代码,顺便更新了最新的组件,不得不佩服EPPlus开发团队,功能更加强大了。

官网:https://www.epplussoftware.com/zh/

Github: https://github.com/EPPlusSoftware/EPPlus


网上又很多帮助文档,可供参考。我仅仅是测试验证EEPLUS5的功能。

创建项目,NuGet安装EPPlus5

当前版本5.8.3


完成后添加了依赖的组件如下:


用于非商业测试,所以需要声明为非商业应用:

在代码中声明

ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

或在Config中声明


测试创建Excel数据表

创建一个简单的Excel,创建表,创建一些测试数据。

public static void Created()

        {

            FileInfo fi = new FileInfo("MySample.xlsx");

            using (var package = new ExcelPackage(fi))

            {

                var worksheet = package.Workbook.Worksheets.Add("Test");

                //Add the headers

                worksheet.Cells[1, 1].Value = "ID";

                worksheet.Cells[1, 2].Value = "TestPointName";

                worksheet.Cells[1, 3].Value = "TestValue";

                //add Value

                worksheet.Cells["A2"].Value = 12001;

                worksheet.Cells["B2"].Value = "VoltageTest";

                worksheet.Cells["C2"].Value = 3.4;

                worksheet.Cells["A3"].Value = 12002;

                worksheet.Cells["B3"].Value = "CurrentTest";

                worksheet.Cells["C3"].Value = 52;

                worksheet.Cells["A4"].Value = 12003;

                worksheet.Cells["B4"].Value = "Temperature";

                worksheet.Cells["C4"].Value = 19.02;

                worksheet.Cells.AutoFitColumns(0);  //Autofit columns for all cells

                package.Save();

            }

在查询执行目录下创建了Excel文件:MySample.xlsx


内容如下:




Github完整代码如下:

using (var package = new ExcelPackage())

            {

                //Add a new worksheet to the empty workbook

                var worksheet = package.Workbook.Worksheets.Add("Inventory");

                //Add the headers

                worksheet.Cells[1, 1].Value = "ID";

                worksheet.Cells[1, 2].Value = "Product";

                worksheet.Cells[1, 3].Value = "Quantity";

                worksheet.Cells[1, 4].Value = "Price";

                worksheet.Cells[1, 5].Value = "Value";

                //Add some items...

                worksheet.Cells["A2"].Value = 12001;

                worksheet.Cells["B2"].Value = "Nails";

                worksheet.Cells["C2"].Value = 37;

                worksheet.Cells["D2"].Value = 3.99;

                worksheet.Cells["A3"].Value = 12002;

                worksheet.Cells["B3"].Value = "Hammer";

                worksheet.Cells["C3"].Value = 5;

                worksheet.Cells["D3"].Value = 12.10;

                worksheet.Cells["A4"].Value = 12003;

                worksheet.Cells["B4"].Value = "Saw";

                worksheet.Cells["C4"].Value = 12;

                worksheet.Cells["D4"].Value = 15.37;

                //Add a formula for the value-column

                worksheet.Cells["E2:E4"].Formula = "C2*D2";

                //Ok now format the values

                using (var range = worksheet.Cells[1, 1, 1, 5])

                {

                    range.Style.Font.Bold = true;

                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;

                    range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);

                    range.Style.Font.Color.SetColor(Color.White);

                }

                worksheet.Cells["A5:E5"].Style.Border.Top.Style = ExcelBorderStyle.Thin;

                worksheet.Cells["A5:E5"].Style.Font.Bold = true;

                worksheet.Cells[5, 3, 5, 5].Formula = string.Format("SUBTOTAL(9,{0})", new ExcelAddress(2, 3, 4, 3).Address);

                worksheet.Cells["C2:C5"].Style.Numberformat.Format = "#,##0";

                worksheet.Cells["D2:E5"].Style.Numberformat.Format = "#,##0.00";

                //Create an autofilter for the range

                worksheet.Cells["A1:E4"].AutoFilter = true;

                worksheet.Cells["A2:A4"].Style.Numberformat.Format = "@";  //Format as text

                //There is actually no need to calculate, Excel will do it for you, but in some cases it might be useful.

                //For example if you link to this workbook from another workbook or you will open the workbook in a program that hasn't a calculation engine or

                //you want to use the result of a formula in your program.

                worksheet.Calculate();

                worksheet.Cells.AutoFitColumns(0);  //Autofit columns for all cells

                // Lets set the header text

                worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\" Inventory";

                // Add the page number to the footer plus the total number of pages

                worksheet.HeaderFooter.OddFooter.RightAlignedText =

                    string.Format("Page {0} of {1}", ExcelHeaderFooter.PageNumber, ExcelHeaderFooter.NumberOfPages);

                // Add the sheet name to the footer

                worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName;

                // Add the file path to the footer

                worksheet.HeaderFooter.OddFooter.LeftAlignedText = ExcelHeaderFooter.FilePath + ExcelHeaderFooter.FileName;

                worksheet.PrinterSettings.RepeatRows = worksheet.Cells["1:2"];

                worksheet.PrinterSettings.RepeatColumns = worksheet.Cells["A:G"];

                // Change the sheet view to show it in page layout mode

                worksheet.View.PageLayoutView = true;

                // Set some document properties

                package.Workbook.Properties.Title = "Invertory Test";

                package.Workbook.Properties.Author = "Ritchie ";

                package.Workbook.Properties.Comments = "This sample demonstrates how to create an Excel workbook using EPPlus";

                // Set some extended property values

                package.Workbook.Properties.Company = "EPPlus Software AB TEST";

                // Set some custom property values

                package.Workbook.Properties.SetCustomPropertyValue("Checked by", "Ritchie");

                package.Workbook.Properties.SetCustomPropertyValue("AssemblyName", "EPPlus");

                var xlFile = FileUtil.GetCleanFileInfo("01-GettingStarted.xlsx");

                // Save our new workbook in the output directory and we are done!

                package.SaveAs(xlFile);

                return xlFile.FullName;

            }


创建的表格如下:


内容如下:

效果杠杠的!

你可能感兴趣的:(C# 中EPPlus基本功能测试-(1))