Workbook.Workbook

Workbook()

Initializes a new instance of the Workbook class.

public Workbook()

Remarks

The default file format type is Xlsx. If you want to create other types of files, please use Workbook(FileFormatType).

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorkbookMethodCtorDemo
    {
        public static void Run()
        {
            // Create a new workbook using the constructor
            Workbook workbook = new Workbook();
            
            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];
            
            // Add some data to cells
            worksheet.Cells["A1"].PutValue("Hello");
            worksheet.Cells["B1"].PutValue("World");
            
            // Save the workbook
            workbook.Save("WorkbookCtorDemo.xlsx", SaveFormat.Xlsx);
            
            Console.WriteLine("Workbook created and saved successfully.");
        }
    }
}

See Also


Workbook(FileFormatType)

Initializes a new instance of the Workbook class.

public Workbook(FileFormatType fileFormatType)
ParameterTypeDescription
fileFormatTypeFileFormatTypeThe new file format.

Remarks

The default file format type is Excel97To2003.

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorkbookMethodCtorWithFileFormatTypeDemo
    {
        public static void Run()
        {
            // Create a new workbook with XLSX file format
            Workbook workbook = new Workbook(FileFormatType.Xlsx);
            
            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];
            
            // Put some data in cell A1
            worksheet.Cells["A1"].PutValue("Hello World!");
            
            // Save the workbook
            workbook.Save("output.xlsx", SaveFormat.Xlsx);
        }
    }
}

See Also


Workbook(LoadOptions)

Initializes a new empty instance of the Workbook class with options

public Workbook(LoadOptions loadOptions)
ParameterTypeDescription
loadOptionsLoadOptionsThe options.

See Also


Workbook(string)

public Workbook(string file)

See Also


Workbook(Stream)

Initializes a new instance of the Workbook class and open a stream.

public Workbook(Stream stream)
ParameterTypeDescription
streamStreamThe stream.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;
    using System.IO;

    public class WorkbookMethodSharpctorWithStreamDemo
    {
        public static void Run()
        {
            // Create a sample Excel file in memory
            MemoryStream sampleStream = new MemoryStream();
            Workbook sampleWorkbook = new Workbook();
            sampleWorkbook.Worksheets[0].Cells["A1"].Value = "Sample Data";
            sampleWorkbook.Save(sampleStream, SaveFormat.Xlsx);
            sampleStream.Position = 0; // Reset stream position for reading

            try
            {
                // Create a new Workbook instance using the Stream constructor
                Workbook workbook = new Workbook(sampleStream);

                // Access the first worksheet
                Worksheet worksheet = workbook.Worksheets[0];

                // Display the loaded data
                Console.WriteLine($"Loaded data from stream: {worksheet.Cells["A1"].StringValue}");

                // Save the workbook to a file
                workbook.Save("WorkbookFromStream.xlsx");
                Console.WriteLine("Workbook saved successfully from stream.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error loading workbook from stream: {ex.Message}");
            }
            finally
            {
                sampleStream.Dispose();
            }
        }
    }
}

See Also


Workbook(string, LoadOptions)

Initializes a new instance of the Workbook class and open a file.

public Workbook(string file, LoadOptions loadOptions)
ParameterTypeDescription
fileStringThe file name.
loadOptionsLoadOptionsThe load options

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class WorkbookMethodCtorWithStringLoadOptionsDemo
    {
        public static void Run()
        {
            // Create load options without LightCellsDataHandler
            LoadOptions loadOptions = new LoadOptions();

            // Initialize workbook with file path and load options
            Workbook workbook = new Workbook("sample.xlsx", loadOptions);

            // Demonstrate workbook is loaded by accessing worksheet count
            Console.WriteLine("Number of worksheets: " + workbook.Worksheets.Count);
        }
    }
}

See Also


Workbook(Stream, LoadOptions)

Initializes a new instance of the Workbook class and open stream.

public Workbook(Stream stream, LoadOptions loadOptions)
ParameterTypeDescription
streamStreamThe stream.
loadOptionsLoadOptionsThe load options

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using System;
    using System.IO;

    public class WorkbookMethodSharpctorWithStreamLoadOptionsDemo
    {
        public static void Run()
        {
            try
            {
                // Create a sample Excel file in memory
                byte[] sampleExcelData = GetSampleExcelData();
                MemoryStream stream = new MemoryStream(sampleExcelData);

                // Create LoadOptions with specific settings
                LoadOptions loadOptions = new LoadOptions(LoadFormat.Xlsx);
                loadOptions.MemorySetting = MemorySetting.MemoryPreference;

                // Create a Workbook instance using the #ctor with Stream and LoadOptions
                Workbook workbook = new Workbook(stream, loadOptions);

                // Display basic information about the loaded workbook
                Console.WriteLine("Workbook loaded successfully!");
                Console.WriteLine("Number of worksheets: " + workbook.Worksheets.Count);
                Console.WriteLine("File format: " + workbook.FileFormat);

                // Access the first worksheet
                Worksheet worksheet = workbook.Worksheets[0];
                Console.WriteLine("First worksheet name: " + worksheet.Name);

                // Display some cell content if available
                if (worksheet.Cells["A1"].Value != null)
                {
                    Console.WriteLine("Cell A1 value: " + worksheet.Cells["A1"].Value.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error loading workbook: {ex.Message}");
            }
        }

        private static byte[] GetSampleExcelData()
        {
            // Create a simple Excel file in memory for demonstration
            using (MemoryStream ms = new MemoryStream())
            {
                Workbook tempWorkbook = new Workbook();
                tempWorkbook.Worksheets[0].Cells["A1"].PutValue("Sample Data");
                tempWorkbook.Worksheets[0].Cells["B1"].PutValue("Created for demo");
                tempWorkbook.Save(ms, SaveFormat.Xlsx);
                return ms.ToArray();
            }
        }
    }
}

See Also