Get started with Documents for Excel in Windows, Mac, and Linux
This guide explains how to create a program that uses DsExcel, previously GcExcel, to generate and save to disk an Excel file.
Document Solutions for Excel assemblies are built for .NET Standard 2.0, and can be used with any target that supports it. In this short tutorial, we show how to add reference to GcExcel in .NET Core application. The tutorial also takes you through the steps required to do that in Visual Studio on Windows or MAC, or Visual Studio Code on Linux.
These videos cover installing Documents for Excel on Windows, Mac, and Linux and how to create basic Excel spreadsheet using DsExcel.
Get started with Documents for Excel on Windows
Get started with Documents for Excel on Mac
Get started with Documents for Excel on Linux
Step 1: Create an app in the IDE of your choice
Create an app using Visual Studio on Windows
1. Open Visual Studio for Windows.
2. Create a new .NET Core Console Application.
3. Right click the project in Solution Explorer and choose Manage NuGet Packages.
4. In Package source in top right, select nuget.org.
5. Click Browse tab in top left and enter "GrapeCity.Documents.Excel" as the search string. You should see several GrapeCity.Documents packages listed.
6. Select GrapeCity.Documents.Excel, and click Install. Accept the license agreement.
This will add the required references to your application. You can now jump to Add code to your application topic from where you can start learning how to use DsExcel features in detail.
Create an app using Visual Studio on MAC
1. Open Visual Studio for MAC.
2. Create a new .NET Core Console Application.
3. In the tree view on the left, right click Dependencies and choose Add Packages.
4. In the Search panel, type "GrapeCity.Documents.Excel".
5. From the list in the left panel, select GrapeCity.Documents.Excel and click Add Packages.
6. Accept the license agreement.
This will add the required references to your application. You can now jump to Add code to your application topic from where you can start learning how to use DsExcel features in detail.
Create an app using Visual Studio Code on Linux
1. In a terminal window (you may use the Terminal in Visual Studio Code), type the following commands:
$ mkdir ~/MyApp # create a directory for the application
$ cd ~/MyApp
$ dotnet new console # create a .NET Core application with MyApp.csproj and Program.cs files
2. Open Visual Studio Code.
3. If you haven't already done so, from Extensions install Nuget Package Manager, and activate it.
4. In Visual Studio Code, press Ctrl+P to open the file command box, type > in it, find "Nuget Package Manager: Add Package" in the list that opens, and click it.
5. In the search box that opens, type "GrapeCity.Documents.Excel" and press Enter. This should bring up GrapeCity.Documents.Excel package.
6. Select it. This will add a reference to that package to you .csproj file, which would now look like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GrapeCity.Documents.Excel" Version="1.5.0.1" />
</ItemGroup>
</Project>
7. In a terminal window, type the following commands to build and run the app:
$ cd ~/MyApp
$ dotnet restore # fetches referenced packages for MyApp.csproj
$ dotnet run # runs the default app
You can now jump to Add code to your application topic from where you can start learning how to use GcExcel features in detail.
Step 2: Add code to your application
Open Program.cs in Visual Studio or Visual Studio Code, and modify it so that it looks like this:
//Initialize the WorkBook
Workbook workbook = new Workbook();
//Create worksheet
IWorksheet worksheet = workbook.Worksheets[0];
//Add data
worksheet.Range["B3:C7"].Value = new object[,]
{
{ "ITEM", "AMOUNT" },
{ "Income 1", 2500 },
{ "Income 2", 1000 },
{ "Income 3", 250 },
{ "Other", 250 },
};
worksheet.Range["B10:C23"].Value = new object[,]
{
{ "ITEM", "AMOUNT" },
{ "Rent/mortgage", 800 },
{ "Electric", 120 },
{ "Gas", 50 },
{ "Cell phone", 45 },
{ "Groceries", 500 },
{ "Car payment", 273 },
{ "Auto expenses", 120 },
{ "Student loans", 50 },
{ "Credit cards", 100 },
{ "Auto Insurance", 78 },
{ "Personal care", 50 },
{ "Entertainment", 100 },
{ "Miscellaneous", 50 },
};
worksheet.Range["B2:C2"].Merge();
worksheet.Range["B2"].Value = "MONTHLY INCOME";
worksheet.Range["B9:C9"].Merge();
worksheet.Range["B9"].Value = "MONTHLY EXPENSES";
worksheet.Range["E2:G2"].Merge();
worksheet.Range["E2"].Value = "PERCENTAGE OF INCOME SPENT";
worksheet.Range["E5:G5"].Merge();
worksheet.Range["E5"].Value = "SUMMARY";
worksheet.Range["E3:F3"].Merge();
worksheet.Range["E9"].Value = "BALANCE";
worksheet.Range["E6"].Value = "Total Monthly Income";
worksheet.Range["E7"].Value = "Total Monthly Expenses";
//Set Row/Column Height, Width
worksheet.StandardHeight = 26.25;
worksheet.StandardWidth = 8.43;
worksheet.Range["2:24"].RowHeight = 27;
worksheet.Range["A:A"].ColumnWidth = 2.855;
worksheet.Range["B:B"].ColumnWidth = 33.285;
worksheet.Range["C:C"].ColumnWidth = 25.57;
worksheet.Range["D:D"].ColumnWidth = 1;
worksheet.Range["E:F"].ColumnWidth = 25.57;
worksheet.Range["G:G"].ColumnWidth = 14.285;
//Add Table
ITable incomeTable = worksheet.Tables.Add(worksheet.Range["B3:C7"], true);
incomeTable.Name = "tblIncome";
incomeTable.TableStyle = workbook.TableStyles["TableStyleMedium4"];
ITable expensesTable = worksheet.Tables.Add(worksheet.Range["B10:C23"], true);
expensesTable.Name = "tblExpenses";
expensesTable.TableStyle = workbook.TableStyles["TableStyleMedium4"];
//Add Formula
worksheet.Names.Add("TotalMonthlyIncome", "=SUM(tblIncome[AMOUNT])");
worksheet.Names.Add("TotalMonthlyExpenses", "=SUM(tblExpenses[AMOUNT])");
worksheet.Range["E3"].Formula = "=TotalMonthlyExpenses";
worksheet.Range["G3"].Formula = "=TotalMonthlyExpenses/TotalMonthlyIncome";
worksheet.Range["G6"].Formula = "=TotalMonthlyIncome";
worksheet.Range["G7"].Formula = "=TotalMonthlyExpenses";
worksheet.Range["G9"].Formula = "=TotalMonthlyIncome-TotalMonthlyExpenses";
//Add Conditional Formatting
IDataBar dataBar = worksheet.Range["E3"].FormatConditions.AddDatabar();
dataBar.MinPoint.Type = ConditionValueTypes.Number;
dataBar.MinPoint.Value = 1;
dataBar.MaxPoint.Type = ConditionValueTypes.Number;
dataBar.MaxPoint.Value = "=TotalMonthlyIncome";
dataBar.BarFillType = DataBarFillType.Gradient;
dataBar.BarColor.Color = Color.Red;
dataBar.ShowValue = false;
//Add Style
IStyle currencyStyle = workbook.Styles["Currency"];
currencyStyle.IncludeAlignment = true;
currencyStyle.HorizontalAlignment = HorizontalAlignment.Left;
currencyStyle.VerticalAlignment = VerticalAlignment.Bottom;
currencyStyle.NumberFormat = "$#,##0.00";
IStyle heading1Style = workbook.Styles["Heading 1"];
heading1Style.IncludeAlignment = true;
heading1Style.HorizontalAlignment = HorizontalAlignment.Center;
heading1Style.VerticalAlignment = VerticalAlignment.Center;
heading1Style.Font.Name = "SegoeUI";
heading1Style.Font.Bold = true;
heading1Style.Font.Size = 11;
heading1Style.Font.Color = Color.White;
heading1Style.IncludeBorder = false;
heading1Style.IncludePatterns = true;
heading1Style.Interior.Color = Color.FromRGB(51, 0, 102);
//Assign Style to the worksheet Ranges
worksheet.SheetView.DisplayGridlines = false;
worksheet.Range["C4:C7, C11:C23, G6:G7, G9"].Style = currencyStyle;
worksheet.Range["B2, B9, E2, E5"].Style = heading1Style;
//Save the workbook
workbook.Save(@"SimpleBudget.xlsx");
Step 3: Run the application
-
On Windows or MAC, just click the "start debugging" button in Visual Studio.
-
On Linux, enter the following in a terminal window:
$ cd ~/MyApp
$ dotnet run # runs MyApp
That's all it takes to generate an Excel Spreadsheet file using DsExcel. The 'SimpleBudget.xlsx' should now be in your project directory. If you'd like to generate an Excel file with more content and with multiple sheets, you can read How to generate Excel spreadsheets in code with Documents for Excel.