Create a spreadsheet document

OpenXML Excel

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

class Program
    {
        static void Main(string[] args)
        {
            CreateSpreadsheetWorkbook(@"E:\Dropbox\Personal\Aspose Vs OpenXML\Files\Create a spreadsheet document.xlsx");
        }
        private static void CreateSpreadsheetWorkbook(string filepath)
        {
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(filepath, 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>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

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

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

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

Aspose.Cells

using Aspose.Cells;

class Program
    {
        static void Main(string[] args)
        {
            CreateSpreadsheetWorkbook("Create a spreadsheet document.xlsx");
        }

        private static void CreateSpreadsheetWorkbook(string filepath)
        {
            //Instantiating a Workbook object
            Workbook workbook = new Workbook();

            //Adding a new worksheet to the Excel object
            Worksheet worksheet = workbook.Worksheets.Add("MySheet");

            //Saving the Excel file
            workbook.Save(filepath);
        }
    }

Download

Last edited Jan 8, 2014 at 10:28 AM by asposemarketplace, version 1