C# 匯出Excel套件 EPPlus

 EPPlus

//非商業用

ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;

               //filePath:檔案路徑

                FileInfo FileInfoXLS = new FileInfo(filePath);

                using (ExcelPackage package = new ExcelPackage(FileInfoXLS))

                {

                    ExcelWorksheet ws = package.Workbook.Worksheets.Add("Data");


                    // 將DataTable資料塞到sheet中

                    ws.Cells["A1"].LoadFromDataTable(dtData, true);


                    // 設定Excel Header 樣式

                    using (ExcelRange rng = ws.Cells[1, dtData.Columns.Count])

                    {

                        rng.Style.Font.Bold = true;

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

                        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));

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

                    }


                    var stream = new MemoryStream();

                    package.Save();

                    package.Dispose();

                }

留言