Keywords: C# | Excel Generation | Open Source Libraries | File Formats | Data Processing
Abstract: This paper provides an in-depth analysis of various technical solutions for creating Excel files in C# environments without requiring Microsoft Office installation. Through comparative analysis of mainstream open-source libraries including ExcelLibrary, EPPlus, and NPOI, the article details their functional characteristics, applicable scenarios, and implementation approaches. It comprehensively covers the complete workflow from database data retrieval to Excel workbook generation, support for different Excel formats (.xls and .xlsx), licensing changes, and practical development considerations, offering developers comprehensive technical references and best practice recommendations.
Introduction
In modern software development, Excel file generation and processing represent common business requirements. However, traditional Microsoft Office interoperation methods require Office software installation on target machines, which is often impractical in server environments or distributed deployments. This paper systematically introduces several solutions for creating Excel files in C# without Microsoft Office installation.
Comparative Analysis of Mainstream Open-Source Libraries
The market currently features multiple mature C# Excel processing libraries, each with unique advantages and applicable scenarios. ExcelLibrary is a lightweight open-source library originally ported from PHP ExcelWriter, primarily supporting traditional .xls format. This library provides the DataSetHelper utility class, enabling convenient direct conversion of DataSet and DataTable to Excel workbooks, significantly simplifying development workflows.
The EPPlus library specifically targets Excel 2007/2010 format (.xlsx), offering comprehensive feature support including cell styling, chart generation, shape drawing, image insertion, named ranges, auto-filtering, and other advanced capabilities. It's important to note that EPPlus adopted the Polyform Noncommercial license starting from version 5, requiring commercial licensing for business use scenarios.
The NPOI library provides dual support for both .xls and .xlsx formats, operating under Apache license with excellent cross-platform compatibility. When selecting libraries for actual projects, developers must comprehensively consider file format requirements, functional complexity, licensing agreements, and community activity.
Practical Implementation Using ExcelLibrary
The following code example demonstrates how to use ExcelLibrary to read data from databases and generate Excel files:
// Create dataset and data table
DataSet ds = new DataSet("New_DataSet");
DataTable dt = new DataTable("New_DataTable");
// Set locale to ensure proper data formatting
ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
// Establish database connection
OleDbConnection con = new OleDbConnection(dbConnectionString);
con.Open();
// Execute query and populate data table
string sql = "SELECT Whatever FROM MyDBTable;";
OleDbCommand cmd = new OleDbCommand(sql, con);
OleDbDataAdapter adptr = new OleDbDataAdapter();
adptr.SelectCommand = cmd;
adptr.Fill(dt);
con.Close();
// Add data table to dataset
ds.Tables.Add(dt);
// Generate Excel file using ExcelLibrary
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", ds);The core advantage of this approach lies in its simplicity, where the DataSetHelper.CreateWorkbook method can convert entire datasets to Excel files, dramatically reducing code complexity.
Technical Implementation Details and Considerations
When implementing Excel file generation functionality, multiple technical details require consideration. First is file format compatibility: ExcelLibrary primarily supports .xls format, while EPPlus specializes in .xlsx format. Developers must make appropriate library selections based on target users' Excel versions.
Performance considerations are equally important. When processing large data volumes, memory management and streaming processing become critical. For large-scale data scenarios, batch processing approaches are recommended to prevent memory overflow.
Licensing agreements represent another significant factor. EPPlus versions 4 and below use LGPL licensing and can be used freely, while EPPlus 5 adopts a new licensing model requiring special attention to compliance in commercial environments.
Advanced Feature Extensions
Beyond basic data export functionality, these libraries support various advanced features. EPPlus provides pivot table support, although ExcelLibrary has known issues with this functionality. Regarding styling, developers can precisely control cell attributes including fonts, colors, borders, and backgrounds through code, achieving visual effects comparable to manually created files.
For complex reporting requirements, advanced features like chart generation, formula calculation, and conditional formatting can be combined to create fully functional Excel reporting systems.
Best Practice Recommendations
Based on practical project experience, we recommend: prioritizing EPPlus when only .xlsx format is needed with complex functional requirements; considering NPOI when simultaneous support for both .xls and .xlsx formats is required; and selecting ExcelLibrary for simple .xls file generation needs. In performance-sensitive scenarios, benchmark testing is advised to identify the most suitable solution.
Conclusion
By utilizing open-source libraries, C# developers can efficiently generate and process Excel files without relying on Microsoft Office installation. These libraries not only provide basic data export functionality but also support rich advanced features, meeting various complex business requirements. As these projects continue to evolve, their functionality and performance will further improve, providing enhanced Excel processing capabilities for the .NET ecosystem.