Free .NET Library for Creating Spreadsheet Documents
Read, Write, Manipulate & Convert Excel files via Open Source .NET API.
NetOffice API allows .NET developers to Read, Write, Manipulate & Convert Excel files via open source .NET API. The API allows automating Microsoft Excel Spreadsheets and develop Microsoft Excel Add-ins. Using the API, the developer will use all the options enclosed in MS Office versions 2000, 2002, 2003, 2007, 2010, 2013, and 2016. The API relies on COM-architecture where you retrieve COM proxy objects in your application.
In order to work with Microsoft Excel documents, you need ExcelApi.dll with OfficeApi.ddl, VBIDEApi.dll, and NetOffice.dll as dependencies. All Office applications use types that are defined in other components/type libraries. These dependent type libraries are therefore given as an independent assembly. Each assembly also requires the NetOffice.dll assembly.
Getting Started with NetOffice
First of all, you require to have .NET Framework 4.5 or above. After that, please download the repository manually from GitHub or install it from NuGet.
Installation NetOffice from NuGet
Install-Package NetOfficeFw.Excel
Add Shapes in Excel using Free C# API
NetOffice allows .NET programmers to add shapes in Microsoft Excel Spreadsheets programmatically. In order to add shapes in excel file first, you need to initialize an Excel.Application and turn off message boxes. After your excel application is started you can add a new document to it using ExcelApplication.Workbooks.Add() method. You can insert text in your newly created excel file using workSheet.Cells[1, 1].Value property and add shape in the file using WorkSheet.Shapes.AddShape(MsoAutoShapeType.msoShape32pointStar, 10, 50, 200, 20) method.
Insert Shapes to Excel Spreadsheet File via C#
// start excel and turn off msg boxes
Excel.Application excelApplication = new Excel.Application();
excelApplication.DisplayAlerts = false;
// create a utils instance, not need for but helpful to keep the lines of code low
CommonUtils utils = new CommonUtils(excelApplication);
// add a new workbook
Excel.Workbook workBook = excelApplication.Workbooks.Add();
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
workSheet.Cells[1, 1].Value = "NetOffice Excel Example 04";
// create a star
Excel.Shape starShape = workSheet.Shapes.AddShape(MsoAutoShapeType.msoShape32pointStar, 10, 50, 200, 20);
// create a simple textbox
Excel.Shape textBox = workSheet.Shapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, 10, 150, 200, 50);
textBox.TextFrame.Characters().Text = "text";
textBox.TextFrame.Characters().Font.Size = 14;
// create a wordart
Excel.Shape textEffect = workSheet.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect14, "WordArt", "Arial", 12,
MsoTriState.msoTrue, MsoTriState.msoFalse, 10, 250);
// save the book
string workbookFile = utils.File.Combine(HostApplication.RootDirectory, "Example04", DocumentFormat.Normal);
workBook.SaveAs(workbookFile);
// close excel and dispose reference
excelApplication.Quit();
excelApplication.Dispose();
/ show end dialog
HostApplication.ShowFinishDialog(null, workbookFile);
Create a Chart in Excel using C#
NetOffice allows .NET programmers to add charts in Microsoft Excel File programmatically. In order to add charts in Excel file; first, you need to initialize an Excel.Application and turn off message boxes and add new worksheet using xcelApplication.Workbooks.Add() method. You can insert charts in your newly created excel file by initializing Excel.ChartObject and set it using ((Excel.ChartObjects)workSheet.ChartObjects()).Add(70, 100, 375, 225) method You can set the data source of your newly created chart using Chart.SetSourceData() method
Add Chart to Excel Worksheet via C# API
// start excel and turn off msg boxes
Excel.Application excelApplication = new Excel.Application();
excelApplication.DisplayAlerts = false;
// create a utils instance, no need for but helpful to keep the lines of code low
CommonUtils utils = new CommonUtils(excelApplication);
// add a new workbook
Excel.Workbook workBook = excelApplication.Workbooks.Add();
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];
// we need some data to display
Excel.Range dataRange = PutSampleData(workSheet);
// create a nice diagram
Excel.ChartObject chart = ((Excel.ChartObjects)workSheet.ChartObjects()).Add(70, 100, 375, 225);
chart.Chart.SetSourceData(dataRange);
// save the book
string workbookFile = utils.File.Combine(HostApplication.RootDirectory, "Example05", DocumentFormat.Normal);
workBook.SaveAs(workbookFile);
// close excel and dispose reference
excelApplication.Quit();
excelApplication.Dispose();
// show end dialog
HostApplication.ShowFinishDialog(null, workbookFile);