我想更改excel Xlsx文档的工作表标签颜色。我使用以下代码,但它没有设置表格颜色。当我设置工作表标签颜色时,我得到对象引用异常。
public static string filepath = @"C:\Test\Book1.xlsx";
private static void ChangeSheetcolor()
{
try
{
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filepath, false))
{
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
//my code
WorksheetPart worksheetPart =
GetWorksheetPartByName(spreadSheetDocument, "Sheet1");
if (worksheetPart != null)
{
// worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("Red");
worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("#CCCCCC");
// Save the worksheet.
worksheetPart.Worksheet.Save();
}
}
}
catch (Exception ex)
{
}
}
private static WorksheetPart
GetWorksheetPartByName(SpreadsheetDocument document,
string sheetName)
{
IEnumerable<Sheet> sheets =
document.WorkbookPart.Workbook.GetFirstChild<Sheets>().
Elements<Sheet>().Where(s => s.Name == sheetName);
if (sheets.Count() == 0)
{
//does not exist
return null;
}
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)
document.WorkbookPart.GetPartById(relationshipId);
return worksheetPart;
}
如何使用Open XML dll更改工作表标签颜色?
答案 0 :(得分:3)
就我所知,你似乎有3个问题。首先,您调用的Open
方法的第二个参数表示文件是否可编辑。如果您想编辑文件,则需要在此处传递true
,否则在保存工作表时会遇到以下异常:
System.IO.IOException:当访问为FileAccess.Read时,无法使用FileMode.Create,FileMode.CreateNew,FileMode.Truncate,FileMode.Append获取流。
其次,SheetProperties
或TabColor
可以为空。在XSD中,它们被定义为
<xsd:element name="sheetPr" type="CT_SheetPr" minOccurs="0" maxOccurs="1"/>
和
<xsd:element name="tabColor" type="CT_Color" minOccurs="0" maxOccurs="1"/>
请注意,两者都有minOccurs="0"
,因此可以为null。如果是,您只需创建新实例并将其分配给Worksheet
。
最后,Rgb
属性被称为documentation状态(强调我的):
标准 Alpha 红绿蓝颜色值( A RGB)。
此属性的可能值由ST_UnsignedIntHex简单类型定义 (§18.18.86)。
18.18.86接着说(再次强调我的):
这个简单类型的内容长度为正好为8个十六进制数字。
假设您想要纯色,可以将Alpha通道设置为FF。您也不应该包含前导#
,因此对于纯红色,您应该使用"FFFF0000"
。有关ARGB值的更多详细信息,请参见this question。
将所有代码整合在一起就像这样:
public static string filepath = @"C:\Test\Book1.xlsx";
private static void ChangeSheetcolor()
{
try
{
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(filepath, false))
{
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
//my code
WorksheetPart worksheetPart =
GetWorksheetPartByName(spreadSheetDocument, "Sheet1");
if (worksheetPart != null)
{
//create the SheetProperties if it doesn't exist
if (worksheetPart.Worksheet.SheetProperties == null)
worksheetPart.Worksheet.SheetProperties = new SheetProperties();
//create the TabColor if it doesn't exist
if (worksheetPart.Worksheet.SheetProperties.TabColor == null)
worksheetPart.Worksheet.SheetProperties.TabColor = new TabColor();
//this will set the tab color to Red
//note the value is an ARGB not an RGB
worksheetPart.Worksheet.SheetProperties.TabColor.Rgb = DocumentFormat.OpenXml.HexBinaryValue.FromString("FFFF0000");
// Save the worksheet.
worksheetPart.Worksheet.Save();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}