C# Logo

C# Create and Open Excel and Text-Based Workbook Files SpreadsheetGear API Sample

Excel-Compatible Samples for .NET

Description

Create and open Excel-compatible workbook files from disk or network drive using the SpreadsheetGear.Factory.GetWorkbook(...) method which will return an IWorkbook object. Specify no path argument (e.g., GetWorkbook()) to create a new workbook and specify a path (e.g., GetWorkbook(string pathToFile)) to open an existing workbook.

Additional overloaded versions are available (GetWorkbook(CultureInfo cultureInfo) and GetWorkbook(string pathToFile, CultureInfo cultureInfo)) if you need to specify what regional settings (i.e., System.Globalization.CultureInfo) should be used for various locale-sensitive workbook content (e.g., NumberFormats for cells) when creating opening the workbook. If no CultureInfo object is specified, the "en-US" CultureInfo will be used.

If you have already created an IWorkbookSet object, you can also open workbook files from disk or network drive within your IWorkbookSet object by using the IWorkbookSet.Workbooks.Open(string pathToFile) method.

To open password-protected Excel workbook files use the overloaded IWorkbookSet.Workbooks.Open(string pathToFile, string password) method that accepts a password as the second argument. See also the Workbook Encryption (Password Protected) SpreadsheetGear API Sample for a demo of this API.

SpreadsheetGear supports opening the following Excel-compatible file formats:

  • Excel 2007-2025 Open XML (*.xlsx)
  • Excel 2007-2025 Macro-Enabled Open XML (*.xlsm)
  • Excel 97-2003 (*.xls)
  • ASCII CSV / Comma-Delimited (typically saved with *.csv extension)
  • Unicode Text Tab-Delimited (typically saved with *.txt extension)

Sample C# Source Code

// Create a new workbook
SpreadsheetGear.IWorkbook workbookNew = SpreadsheetGear.Factory.GetWorkbook();

// When opening existing files SpreadsheetGear will automatically detect the file format 
// and open it accordingly.

// Open an Excel Open XML (*.xlsx) file:
SpreadsheetGear.IWorkbook workbook1 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\workbook1.xlsx");

// Open an Excel "Macro Enabled" Open XML (*.xlsm) file:
SpreadsheetGear.IWorkbook workbook2 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\workbook2.xlsm");

// Open an Excel 97-2003 (*.xls) file:
SpreadsheetGear.IWorkbook workbook3 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\workbook3.xls");

// Open a text-based ASCII comma-delimited file (commonly saved with a *.csv file extension):
SpreadsheetGear.IWorkbook workbook4 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\data.csv");

// Open a text-based Unicode tab-delimited file (commonly saved with a *.txt file extension):
SpreadsheetGear.IWorkbook workbook5 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\data.txt");

// If you have already created an instance of an IWorkbookSet, use the 
// IWorkbookSet.Workbooks.Open(...) method to open a workbook within that workbook set object:
SpreadsheetGear.IWorkbook workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
SpreadsheetGear.IWorkbook workbook6 = workbookSet.Workbooks.Open(@"c:\path\to\workbook6.xlsx");

// Specify a second CultureInfo argument to control how locale-sensitive workbook content is 
// interpreted. For instance, a cell with a date NumberFormat could be formatted as "m/d/yyyy" if 
// opened with an "en-US" CultureInfo (this is the default if no CultureInfo is specified) or 
// "dd/mm/yyyy" if an "en-GB" CultureInfo is specified.
SpreadsheetGear.IWorkbook workbook7 = SpreadsheetGear.Factory.GetWorkbook(@"c:\path\to\workbook7.xlsx",
    System.Globalization.CultureInfo.GetCultureInfo("en-GB"));