Created
December 16, 2019 08:08
-
-
Save dazfuller/de5a3af11833dc27913f55e1a6c91ced to your computer and use it in GitHub Desktop.
Adding multiple tables to a worksheet using EPPlus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using OfficeOpenXml; | |
namespace dotnet_excel_demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using var package = new ExcelPackage(); | |
var worksheet = package.Workbook.Worksheets.Add("Demo"); | |
var table1 = worksheet.Tables.Add(worksheet.Cells["A1:C5"], "table1"); | |
table1.Columns[0].Name = "A"; | |
table1.Columns[1].Name = "B"; | |
table1.Columns[2].Name = "C"; | |
table1.ShowFilter = true; | |
for (var row = 2; row <= 5; row++) | |
{ | |
for (var col = 1; col <= 3; col++) | |
{ | |
worksheet.Cells[row, col].Value = row * col; | |
} | |
} | |
var table2 = worksheet.Tables.Add(worksheet.Cells["A7:C11"], "table2"); | |
table2.Columns[0].Name = "D"; | |
table2.Columns[1].Name = "E"; | |
table2.Columns[2].Name = "F"; | |
table2.ShowFilter = true; | |
for (var row = 8; row <= 11; row++) | |
{ | |
for (var col = 1; col <= 3; col++) | |
{ | |
worksheet.Cells[row, col].Value = (float)row / (float)col; | |
} | |
} | |
package.SaveAs(new FileInfo("Demo.xlsx")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment