Sadiqul
Sadiqul

Reputation: 145

Generate excel sheet using openxml from database

I need to generate excel sheet with multiple tabs or sheets using linq from database. How to do it dynamically. I mean is there any way to set column header name dynamically from database. I am using asp.net core. Need generate columns name and length dynamically from database table.

Upvotes: 3

Views: 11284

Answers (2)

Rena
Rena

Reputation: 36645

Be sure that you have installed DocumentFormat.OpenXml package.You could refer to here.

Here is a simple demo like below:

1.Model:

public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
}

2.Controller:

public class TestModelsController : Controller
{
    private readonly CreateexcelContext _context;

    public TestModelsController(CreateexcelContext context)
    {
        _context = context;
    }

    [Route("/excel")]
    public void WriteExcelFile()
    {
       var persons = _context.TestModel.ToList();
        DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (typeof(DataTable)));
        using (SpreadsheetDocument document = SpreadsheetDocument.Create("TestNewData.xlsx", SpreadsheetDocumentType.Workbook))
        {
            WorkbookPart workbookPart = document.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            var sheetData = new SheetData();
            worksheetPart.Worksheet = new Worksheet(sheetData);

            Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
            Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };

            sheets.Append(sheet);
            Row headerRow = new Row();
            List<String> columns = new List<string>();
            foreach (System.Data.DataColumn column in table.Columns)
            {
                columns.Add(column.ColumnName);
                Cell cell = new Cell();
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(column.ColumnName);
                headerRow.AppendChild(cell);
            }

            sheetData.AppendChild(headerRow);
            foreach (DataRow dsrow in table.Rows)
            {
                Row newRow = new Row();
                foreach (String col in columns)
                {
                    Cell cell = new Cell();
                    cell.DataType = CellValues.String;
                    cell.CellValue = new CellValue(dsrow[col].ToString());
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }
            workbookPart.Workbook.Save();
        }
    }
}

3.Result: enter image description here

Upvotes: 9

jrdevdba
jrdevdba

Reputation: 153

I found this example using C#. Maybe it can point you in the right direction.

Export DataTable to Excel with Open Xml SDK in c#

Upvotes: 0

Related Questions