2013-06-08 9 views
49

Das Problem:Benutzerdefinierte Hintergrundfarbe einer Excel-Blattzelle mit epplus C#

Ich benutze EEPlus.

Ich bin stecken bei der Anwendung eines Hex-Farbcodes, z. #B7DEE8, für eine Zelle in meiner Excel-Tabelle.

Ich habe die folgenden (in Betrieb) Code:

ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid; 
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(Color.Gray); 

Aber ich brauche etwas wie folgt aus:

ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor("#B7DEE8"); 

Also meine Frage ist: ist es möglich, Hex Farbcodes mit EEPlus zu verwenden ? Wenn ja, wie kann ich das tun?

Antwort

67

Versuchen Sie, diese

Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B7DEE8"); 
ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid; 
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(colFromHex); 
+13

Vergessen Sie nicht zuerst die PatternType zu setzen, oder es wird nicht funktionieren – JumpingJezza

+0

Ich verwende Interop.Excel so musste ich range.Interior.Color verwenden, aber die Farbe von HTML-Funktion war es, was ich brauchte, . – StevenWhite

24

Dieses gut funktioniert.

Dim objExcel As New ExcelPackage 
Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName") 
Sheet.Cells["A1"].Style.Fill.PatternType = Style.ExcelFillStyle.Solid 
Sheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(170, 170, 170))