web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Set background Color for first row when exporting to excel x++

(0) ShareShare
ReportReport
Posted on by 190

Hello everybody, 

I am trying to export to excel a file, but i want the first row to be colored. So I have written the following code: 

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
using OfficeOpenXml.Style.ExcelFillStyle;
using System.Drawing;

public void export2Excel(MyTable mytable)
{

str filename = strFmt("File %1%2", myTable.CustAccount, '.xlsx');

MemoryStream memoryStream = new System.IO.MemoryStream();


using (var package = new ExcelPackage(memoryStream))
{


str worksheetname = strFmt("Estratto conto cliente %1", myTable.CustAccount);

var currentRow = 1;

var worksheets = package.get_Workbook().get_Worksheets();
var SalesTableWorksheet = worksheets.Add(worksheetname);
var cells = SalesTableWorksheet.get_Cells();

OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
cell.set_Value("@ElectronicReporting:Company");
cell.AutoFitColumns();

var style = cell.Style.Font;
style.Bold = true;

var paternType = cell.style.Fill;//.PatternType;
paternType .PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
var setColor = cell.Style.Fill.BackgroundColor;
setColor.SetColor(System.Drawing.Color::FromArgb(190,0,0));
cell = null;

cell = cells.get_Item(currentRow, 2);
cell.set_Value("@SYS1996");
cell.AutoFitColumns();
style = cell.Style.Font;
style.Bold = true;
cell = null;

currentRow ++;
cell = null;

cell = cells.get_Item(currentRow, 1);
cell.set_Value(curext());
cell = null;

cell = cells.get_Item(currentRow, 2);
cell.set_Value(myTable.CustAccount);


}
package.save();
file::SendFileToUser(memoryStream, filename);


}

The problem is that it shows me the following error

 OfficeOpenXml.Style.ExcelFillStyle is not valid for field Solid. 

Can anyone help, please? 

Thanks in advance.

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,660 Most Valuable Professional on at

    In X , you must use :: to access enum elements. Like this:

    OfficeOpenXml.Style.ExcelFillStyle::Solid

    By the way, please use Insert > Insert Code to paste source code. For example:

    using System.IO;
    using OfficeOpenXml;
    using OfficeOpenXml.Style;
    using OfficeOpenXml.Table;
    using OfficeOpenXml.Style.ExcelFillStyle;
    using System.Drawing;
    
    public void export2Excel(MyTable mytable)
    {
    	str filename = strFmt("File %1%2", myTable.CustAccount, '.xlsx');
    	MemoryStream memoryStream = new System.IO.MemoryStream();
    
    	using (var package = new ExcelPackage(memoryStream))
    	{
    		str worksheetname = strFmt("Estratto conto cliente %1", myTable.CustAccount);
    
    		var currentRow = 1;
    
    		var worksheets = package.get_Workbook().get_Worksheets();
    		var salesTableWorksheet = worksheets.Add(worksheetname);
    		var cells = salesTableWorksheet.get_Cells();
    
    		OfficeOpenXml.ExcelRange cell = cells.get_Item(currentRow, 1);
    		cell.set_Value("@ElectronicReporting:Company");
    		cell.AutoFitColumns();
    
    		var style = cell.Style.Font;
    		style.Bold = true;
    
    		var paternType = cell.style.Fill;//.PatternType;
    		paternType .PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
    		var setColor = cell.Style.Fill.BackgroundColor;
    		setColor.SetColor(System.Drawing.Color::FromArgb(190,0,0));
    		cell = null;
    
    		cell = cells.get_Item(currentRow, 2);
    		cell.set_Value("@SYS1996");
    		cell.AutoFitColumns();
    		style = cell.Style.Font;
    		style.Bold = true;
    		cell = null;
    
    		currentRow   ;
    		cell = null;
    
    		cell = cells.get_Item(currentRow, 1);
    		cell.set_Value(curext());
    		cell = null;
    
    		cell = cells.get_Item(currentRow, 2);
    		cell.set_Value(myTable.CustAccount);
    	}
    	
    	package.save();
    	File::SendFileToUser(memoryStream, filename);
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 612

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 592 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 586 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans