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

Notifications

Announcements

No record found.

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
    237,795 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 549 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans