@manhnguyenv

Welcome to my blog!

Work with Excel

March 15, 2018 21:49

EPPlus

https://github.com/JanKallman/EPPlus

Work with Excel in .NET

Tạo tệp Excel một cách nhanh nhất, ngắn gọn nhất sử dụng DocumentFormat.OpenXml

Install-Package DocumentFormat.OpenXml

This will create a file called Empty.xlsx in the directory you ran the code from. If you wanted to work from this code you could start by injecting your own data into the SheetData() constructor.

https://blogs.msdn.microsoft.com/chrisrae/2011/08/18/creating-a-simple-xlsx-from-scratch-using-the-open-xml-sdk/

Sample Code 1:

using DocumentFormat.OpenXml; //SpreadsheetDocumentType
using DocumentFormat.OpenXml.Packaging; //SpreadsheetDocument
using DocumentFormat.OpenXml.Spreadsheet; //Worksheet, SheetData, Workbook, Sheets, Sheet

namespace ConsoleApp4
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // Create a spreadsheet document by providing a file name.
            string fileName = @"Book1.xlsx";

            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(fileName, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Sheet1"
            };
            sheets.Append(sheet);
            Worksheet worksheet = new Worksheet();
            SheetData sheetData = new SheetData();
            Row row = new Row();
            Cell cell = new Cell()
            {
                CellReference = "A1",
                DataType = CellValues.String,
                CellValue = new CellValue("Microsoft")
            };
            row.Append(cell);
            sheetData.Append(row);
            worksheet.Append(sheetData);
            worksheetPart.Worksheet = worksheet;

            // Close the document.
            spreadsheetDocument.Close();
        }
    }
}

Sample Code 2:

using DocumentFormat.OpenXml; // Use for SpreadsheetDocumentType
using DocumentFormat.OpenXml.Packaging; // User for SpreadsheetDocument
using DocumentFormat.OpenXml.Spreadsheet; // User for Worksheet, SheetData, Workbook, Sheets, Sheet
using System.Linq;

namespace ConsoleApp4
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            CreateExcelFile();
        }

        private static void CreateExcelFile()
        {
            // By default AutoSave will be true and Editable = true, and Type = xlsx
            using (var doc = SpreadsheetDocument.Create("Empty.xlsx", SpreadsheetDocumentType.Workbook))
            {
                // Creates 4 things: WorkBookPart, WorkSheetPart, WorkSheet, SheetData
                doc.AddWorkbookPart().AddNewPart().Worksheet = new Worksheet(new SheetData());

                doc.WorkbookPart.Workbook =
                  new Workbook(
                    new Sheets(
                      new Sheet
                      {
                          // Id is used to create Sheet to WorksheetPart relationship
                          Id = doc.WorkbookPart.GetIdOfPart(doc.WorkbookPart.WorksheetParts.First()),
                          // SheetId and Name are both required
                          SheetId = 1,
                          Name = "Sheet1"
                      }));
            }
        }
    }
}

Categories

Recent posts