Odczyt pliku Excel przy użyciu Microsoft Office Interop Excel C#: oraz szybszy sposób tworzenia plików Excel bez pakietu Office
If you've ever tried to read Excel file using Microsoft Office Interop Excel C#, you already know the frustration: version mismatches, COM object leaks, and the hard requirement that Microsoft Office be installed on every machine that runs your code. In this article, we'll walk you through the traditional Interop approach, explains where it falls apart, and then shows how IronXL lets you create, read, and write Excel files without any of those headaches.
!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101000101111101010011010101000100000101010010010101000100010101000100010111110101011101001001010100010010000101111101010000010100100100111101000100010101010100001101010100010111110101010001010010010010010100000101001100010111110100001001001100010011110100001101001011--}
How Does the Interop Library Create an Excel Workbook?
The Microsoft.Office.Interop.Excel namespace exposes the same COM object model that Excel uses internally. To read or create Excel files through this interop library, you first instantiate an Application object, add a new workbook to its Workbooks collection, and then access individual worksheet cells. The following example demonstrates a minimal console application using Microsoft.Office.Interop.Excel to create an XLSX file and write values into cells.
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
// Create the Excel Application COM object
Excel.Application excelApp = new Excel.Application();
if (excelApp == null)
{
Console.WriteLine("Error: Excel is not properly installed.");
return;
}
// Add a new workbook and access the default worksheet
Excel.Workbook xlWorkBook = excelApp.Workbooks.Add(Type.Missing);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
// Write data into specific cells
xlWorkSheet.Cells[1, 1] = "Product";
xlWorkSheet.Cells[1, 2] = "Price";
xlWorkSheet.Cells[2, 1] = "Widget";
xlWorkSheet.Cells[2, 2] = "29.99";
// Save as an XLSX file and clean up COM objects
string filepath = @"C:\Reports\products.xlsx";
xlWorkBook.SaveAs(filepath);
xlWorkBook.Close(false);
excelApp.Quit();
// Release every COM object to prevent orphaned Excel processes
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(excelApp);
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
// Create the Excel Application COM object
Excel.Application excelApp = new Excel.Application();
if (excelApp == null)
{
Console.WriteLine("Error: Excel is not properly installed.");
return;
}
// Add a new workbook and access the default worksheet
Excel.Workbook xlWorkBook = excelApp.Workbooks.Add(Type.Missing);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
// Write data into specific cells
xlWorkSheet.Cells[1, 1] = "Product";
xlWorkSheet.Cells[1, 2] = "Price";
xlWorkSheet.Cells[2, 1] = "Widget";
xlWorkSheet.Cells[2, 2] = "29.99";
// Save as an XLSX file and clean up COM objects
string filepath = @"C:\Reports\products.xlsx";
xlWorkBook.SaveAs(filepath);
xlWorkBook.Close(false);
excelApp.Quit();
// Release every COM object to prevent orphaned Excel processes
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(excelApp);
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
' Create the Excel Application COM object
Dim excelApp As Excel.Application = New Excel.Application()
If excelApp Is Nothing Then
Console.WriteLine("Error: Excel is not properly installed.")
Return
End If
' Add a new workbook and access the default worksheet
Dim xlWorkBook As Excel.Workbook = excelApp.Workbooks.Add(Type.Missing)
Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.ActiveSheet, Excel.Worksheet)
' Write data into specific cells
xlWorkSheet.Cells(1, 1) = "Product"
xlWorkSheet.Cells(1, 2) = "Price"
xlWorkSheet.Cells(2, 1) = "Widget"
xlWorkSheet.Cells(2, 2) = "29.99"
' Save as an XLSX file and clean up COM objects
Dim filepath As String = "C:\Reports\products.xlsx"
xlWorkBook.SaveAs(filepath)
xlWorkBook.Close(False)
excelApp.Quit()
' Release every COM object to prevent orphaned Excel processes
Marshal.ReleaseComObject(xlWorkSheet)
Marshal.ReleaseComObject(xlWorkBook)
Marshal.ReleaseComObject(excelApp)
The above code creates an Excel workbook, populates a worksheet with row and cell values, saves the file, and then manually releases each COM object. That cleanup at the end isn't optional — skip it, and you'll find ghost EXCEL.EXE processes consuming memory on your system. Notice, too, that the code checks whether the Application object is null; if Microsoft Office isn't installed, every line after that will throw an error. The same version of the Office interop library must match the version of Office on the machine, or you'll face FileNotFoundException and similar runtime errors that are notoriously difficult to debug.
Visual Basic developers encounter the same challenges when using Excel Microsoft.Office.Interop.Excel, because the underlying COM infrastructure is identical across .NET languages.
What Problems Does the Interop Approach Cause on Servers and in the Cloud?
Microsoft explicitly discourages using Office Interop on server environments. The interop library launches a full instance of the Excel application behind the scenes, that means every request on a web server or cloud app spins up a hidden Windows desktop process. On a Linux server, it won't work at all. Connection strings to databases, file system paths, and user permissions all become additional points of failure when Office isn't installed or when different versions of Microsoft Office conflict with each other.
Developers frequently post about these issues on Stack Overflow and Microsoft Q&A, searching for ways to read Excel files and create Excel files without having Excel installed. The table below summarizes the key differences.
| Funkcja | Office Interop | IronXL | | --- | --- | --- | | Requires Excel installed | Yes — only the developer machine and every production server | Nie | | Supported Excel file formats | XLS, XLSX | XLS, XLSX, CSV, TSV, JSON, XML | | Cross-platform (.NET Core, Linux, macOS) | Tylko dla systemu Windows | Windows, Linux, macOS, Azure | | COM object cleanup required | Yes — manual Marshal.ReleaseComObject | Nie | | Server / cloud deployment | Not recommended by Microsoft | W pełni obsługiwane | | Wydajność | Slower — launches hidden Excel process | Better performance — managed .NET code | | .NET Framework & .NET Core support | Tylko .NET Framework | .NET Framework, .NET Core, .NET 5–10 |
How Can Excel Files Be Created Without Microsoft Office Installed?
IronXL is a .NET Excel library that reads, writes, and creates Excel files entirely in managed code. There's no dependency on Microsoft Office, no COM object to release, and no version conflicts to debug. Install it via NuGet in Visual Studio using the Solution Explorer, search for IronXl.Excel, and you're ready to go. It works fine across .NET Framework 4.6.2+, .NET Core, and .NET 5 through .NET 10 on all major operating systems.
using IronXL;
// Create a new Excel workbook — default value is XLSX format
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet and populate cells with data
WorkSheet workSheet = workBook.CreateWorkSheet("Sales");
workSheet["A1"].Value = "Product";
workSheet["B1"].Value = "Price";
workSheet["A2"].Value = "Widget";
workSheet["B2"].Value = 29.99;
// Apply basic formatting
workSheet["A1"].Style.Font.Bold = true;
workSheet["B1"].Style.Font.Bold = true;
// Save the XLSX file
workBook.SaveAs(@"C:\Reports\products.xlsx");
using IronXL;
// Create a new Excel workbook — default value is XLSX format
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet and populate cells with data
WorkSheet workSheet = workBook.CreateWorkSheet("Sales");
workSheet["A1"].Value = "Product";
workSheet["B1"].Value = "Price";
workSheet["A2"].Value = "Widget";
workSheet["B2"].Value = 29.99;
// Apply basic formatting
workSheet["A1"].Style.Font.Bold = true;
workSheet["B1"].Style.Font.Bold = true;
// Save the XLSX file
workBook.SaveAs(@"C:\Reports\products.xlsx");
Imports IronXL
' Create a new Excel workbook — default value is XLSX format
Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a worksheet and populate cells with data
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("Sales")
workSheet("A1").Value = "Product"
workSheet("B1").Value = "Price"
workSheet("A2").Value = "Widget"
workSheet("B2").Value = 29.99
' Apply basic formatting
workSheet("A1").Style.Font.Bold = True
workSheet("B1").Style.Font.Bold = True
' Save the XLSX file
workBook.SaveAs("C:\Reports\products.xlsx")
Excel File Created with IronXL

The above code accomplishes the same result as the Interop example, but in half the lines and with zero external dependencies. WorkBook.Create() initializes a new workbook in the specified Excel file format (XLSX by default), and CreateWorkSheet adds a named worksheet. Cell values are set through a clean indexer syntax, and SaveAs handles the file extension automatically. Nie Quit(), no Marshal.ReleaseComObject, no hidden process. The same API also supports saving as XLS, CSV, JSON, or XML with a single method call, see the export documentation for all available options.
IronXL also integrates cleanly with System.Data objects, so data flowing from database connection strings through a DataTable can be written directly to a worksheet without manual row-by-row iteration.
How Can Worksheet Data Be Read and Written Across Rows and Cells?
Reading existing Excel files is just as straightforward. The WorkBook.Load method accepts a string filepath to any XLS, XLSX, or CSV file. From there, each worksheet exposes its rows and cells as iterable collections, making it simple to read data, modify values, or upload results to a database or server endpoint.
using IronXL;
// Load an existing Excel file
WorkBook workBook = WorkBook.Load(@"C:\Reports\products.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Read and display each row of data
foreach (var row in workSheet.Rows)
{
foreach (var cell in row.ToArray())
{
Console.Write(cell.Value + "\t");
}
Console.WriteLine();
}
// Update a specific cell and save
workSheet["B2"].Value = 34.99;
workBook.SaveAs(@"C:\Reports\products_updated.xlsx");
using IronXL;
// Load an existing Excel file
WorkBook workBook = WorkBook.Load(@"C:\Reports\products.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Read and display each row of data
foreach (var row in workSheet.Rows)
{
foreach (var cell in row.ToArray())
{
Console.Write(cell.Value + "\t");
}
Console.WriteLine();
}
// Update a specific cell and save
workSheet["B2"].Value = 34.99;
workBook.SaveAs(@"C:\Reports\products_updated.xlsx");
Imports IronXL
' Load an existing Excel file
Dim workBook As WorkBook = WorkBook.Load("C:\Reports\products.xlsx")
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet
' Read and display each row of data
For Each row In workSheet.Rows
For Each cell In row.ToArray()
Console.Write(cell.Value & vbTab)
Next
Console.WriteLine()
Next
' Update a specific cell and save
workSheet("B2").Value = 34.99
workBook.SaveAs("C:\Reports\products_updated.xlsx")
Reading Excel File with IronXL

This example shows how to read Excel files by iterating over the Rows collection and accessing each cell's Value property. The nested loop pattern — var row followed by var cell — mirrors how you'd think about a spreadsheet naturally. After reading the data, the code updates a single cell and saves the workbook to a new XLSX file, preserving the original. IronXL recalculates any Excel formulas automatically when a sheet is modified, which is a feature that the interop library also offers but at the cost of spinning up a full Excel instance.
For more advanced scenarios such as formulas, conditional formatting, merging cells, or working with multiple sheets, the IronXL API reference covers every available method and property. The library also supports access from ASP.NET controllers, making it straightforward to generate and download Excel files from a web app or API endpoint without needing Office installed on the server.
What's the Best Path Forward for .NET Excel Automation?
If a project is locked to a legacy Windows desktop environment where Microsoft Office is already installed and the same version will remain stable, the interop library can work fine for basic tasks. But for everything else, server deployments, cloud apps, cross-platform builds, or any scenario where you want better performance and fewer runtime errors, a standalone .NET Excel library is the clear choice.
IronXL handles every common Excel operation (creating workbooks, reading Excel spreadsheet data, writing to cells, saving across multiple file extension types including XLS and XLSX) through a clean, modern API that runs everywhere .NET runs. It supports both C# and Visual Basic projects, and the same code works on Windows, Linux, and macOS without modification.
Start a free 30-day trial to test it in your own project, or explore licensing options when you're ready to deploy.
Często Zadawane Pytania
Jakie są ograniczenia korzystania z Microsoft Office Interop Excel w języku C#?
Korzystanie z Microsoft Office Interop Excel w języku C# może prowadzić do problemów, takich jak niezgodności wersji, wycieki obiektów COM oraz konieczność zainstalowania pakietu Microsoft Office na każdym komputerze, na którym uruchamiany jest kod.
W jaki sposób IronXL usprawnia proces obsługi plików Excel w języku C#?
IronXL pozwala tworzyć, odczytywać i zapisywać pliki Excel bez konieczności korzystania z pakietu Microsoft Office, eliminując problemy takie jak niezgodności wersji i wycieki obiektów COM.
Czy IronXL może być używany na serwerze bez zainstalowanego pakietu Microsoft Office?
Tak, IronXL jest bezpieczny dla serwerów i nie wymaga instalacji pakietu Microsoft Office, co czyni go rozwiązaniem wieloplatformowym do obsługi plików Excel.
Jakie są zalety korzystania z IronXL w porównaniu z Excel Interop?
IronXL zapewnia bardziej niezawodny i wydajny sposób obsługi plików Excel, ponieważ nie opiera się na pakiecie Microsoft Office, co zmniejsza ryzyko typowych problemów, takich jak niezgodności wersji i wycieki obiektów COM.
Czy IronXL jest kompatybilny ze wszystkimi wersjami plików Excel?
IronXL obsługuje szeroki zakres formatów plików Excel, w tym XLSX, XLS, CSV i TSV, dzięki czemu jest wszechstronnym narzędziem do różnych operacji na plikach Excel.
Czy IronXL wymaga jakiejś specjalnej konfiguracji, aby działać na komputerze?
IronXL nie wymaga instalacji pakietu Microsoft Office i można go łatwo zintegrować z projektami C# bez specjalnej konfiguracji.
Czy IronXL może efektywnie obsługiwać duże pliki Excel?
Tak, IronXL został zaprojektowany do wydajnego odczytu i zapisu dużych plików Excel, zapewniając wysoką wydajność bez ograniczeń Interop.
Jakie platformy obsługuje IronXL?
IronXL jest wieloplatformowy i obsługuje systemy Windows, Linux oraz MacOS, co sprawia, że jest elastycznym wyborem do pracy z plikami Excel w różnych srodowiskach.
W jaki sposób IronXL obsługuje tworzenie plików Excel?
IronXL zapewnia proste API do programowego tworzenia plików Excel w języku C#, oferując funkcje formatowania komórek, zarządzania arkuszami i wiele innych.
Czy istnieje społeczność lub wsparcie dla użytkowników IronXL?
Tak, użytkownicy IronXL mają dostęp do dokumentacji, samouczków i wsparcia społeczności, które pomogą im w przypadku wszelkich pytań dotyczących integracji lub użytkowania.




