C# Logo

C# Excel and Text-Based Files SpreadsheetGear API Sample

Excel-Compatible Samples for .NET

Description

Open Excel-compatible workbook files from disk or network drive using the SpreadsheetGear.Factory.GetWorkbook(string pathToFile) method.

An overloaded version of this method is available (SpreadsheetGear.Factory.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 opening the file. 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

// 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"));