嗨,我正在尝试为数据透视表制作一些代码颜色。 它可以很好地对它应该的单元格进行着色,但如果我刷新表格,所有颜色都会消失,就像颜色没有正确地附加到数据透视表一样。
我有以下代码(这是来自更大代码的剪辑):
myPivotTable.PivotSelect("'" + item["Name"].ToString() + "'[All;Total]", XlPTSelectionMode.xlDataAndLabel, true);
((Range)Globals.ThisWorkbook.Application.Selection).Interior.Color = 15962653;
我尝试在VB中用Excel做一个宏,当它运行时,它工作得很好,所以我不明白为什么C#VSTO不起作用......
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'ItemName'[All;Total]", xlDataAndLabel, True
Selection.Interior.Color = 15962653
非常感谢帮助:)
修改
这里有更多的代码。 BaseVars.GlobalWB是一个变量,它引用活动工作簿(Globals.ThisWorkBook)。这使得可以同时使用2个Excel,而不会在错误的工作簿上运行VSTO代码。
foreach (DataRow item in myPivotTableFields.Tables[0].Rows)
{
// Field name from data sheet
myPivotField = (PivotField)myPivotFields.Item(item["Name"].ToString());
// Field name in the pivot table
myPivotField.Caption = item["Caption"].ToString();
// Their subtotal value
myPivotField.set_Subtotals(Type.Missing, GenerateSubTotalArray(item["SubTotal"].ToString()));
#region Attribs
//Include new items in manual filter
if (item["Attrib01"].ToString() == "True")
{
myPivotField.IncludeNewItemsInFilter = true;
}
else
{
myPivotField.IncludeNewItemsInFilter = false;
}
// Show items labels in outline form
if (item["Attrib02"].ToString() == "Outline")
{
myPivotField.LayoutForm = XlLayoutFormType.xlOutline;
}
else
{
myPivotField.LayoutForm = XlLayoutFormType.xlTabular;
}
// Display labels from the next field in the same column
if (item["Attrib03"].ToString() == "True")
{
myPivotField.LayoutCompactRow = true;
}
else
{
myPivotField.LayoutCompactRow = false;
}
// Display subtotals at the top of each group
if (item["Attrib04"].ToString() == "AtBottom")
{
myPivotField.LayoutSubtotalLocation = XlSubtototalLocationType.xlAtBottom;
}
else
{
myPivotField.LayoutSubtotalLocation = XlSubtototalLocationType.xlAtTop;
}
// Insert blank line after each item label
if (item["Attrib05"].ToString() == "True")
{
myPivotField.LayoutBlankLine = true;
}
else
{
myPivotField.LayoutBlankLine = false;
}
// Show items with no data
if (item["Attrib06"].ToString() == "True")
{
myPivotField.ShowAllItems = true;
}
else
{
myPivotField.ShowAllItems = false;
}
// Insert page break after each item
if (item["Attrib07"].ToString() == "True")
{
myPivotField.LayoutPageBreak = true;
}
else
{
myPivotField.LayoutPageBreak = false;
}
#endregion
// Set up the pivot table selection
if (item["Selection"].ToString() != "(blank)")
{
myItems = new List<string>();
myItems = GlobalFunc.Explode(item["Selection"].ToString());
SetUpPivotTableSelection(myPivotTable, item["Name"].ToString(), myItems);
}
else if (item["Selection"].ToString() == "(blank)" && item["Orientation"].ToString() == "Filter")
{
myPivotField.ClearAllFilters();
myPivotField.CurrentPage = "(All)";
}
try
{
myPivotField.ClearValueFilters();
myPivotField.ShowDetail = true;
}
catch (Exception ex)
{
GlobalFunc.DebugWriter("Error during Pivot Table Reset: " + ex.Message);
}
try
{
myPivotTable.PivotSelect("'" + item["Name"].ToString() + "'[All;Total]", XlPTSelectionMode.xlDataAndLabel, true);
// Set up the fields borders if it has any
myRange = BaseVars.GlobalWB.Application.get_Range(BaseVars.GlobalWB.Application.Selection, BaseVars.GlobalWB.Application.Selection);
myRange.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = (XlLineStyle)InsertLineStyle(item["Attrib12"].ToString());
myRange.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = (XlLineStyle)InsertLineStyle(item["Attrib13"].ToString());
myRange.Borders[XlBordersIndex.xlEdgeRight].LineStyle = (XlLineStyle)InsertLineStyle(item["Attrib14"].ToString());
myRange.Borders[XlBordersIndex.xlEdgeTop].LineStyle = (XlLineStyle)InsertLineStyle(item["Attrib15"].ToString());
}
catch (Exception ex)
{
GlobalFunc.DebugWriter("<LI>Error occured: " + ex.Message + "</LI>");
}
// Insert the colors of the field, gradient or solid
if (item["Color_Total2"].ToString() != null && item["Color_Total2"].ToString() != "")
{
Base.InsertGradient(myRange, int.Parse(item["Color_Total1"].ToString().Replace("0x", ""), System.Globalization.NumberStyles.HexNumber), int.Parse(item["Color_Total2"].ToString().Replace("0x", ""), System.Globalization.NumberStyles.HexNumber), false);
}
else if (item["Color_Total1"].ToString() != null && item["Color_Total1"].ToString() != "")
{
BaseVars.GlobalWB.Application.get_Range(BaseVars.GlobalWB.Application.Selection, BaseVars.GlobalWB.Application.Selection).Interior.Color = int.Parse(item["Color_Total1"].ToString().Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
}
}
答案 0 :(得分:1)
如果您使用的是C#VSTO,请不要使用Selection.Interior.Color。请改用Selection.Interior.ColorIndex。 Excel使用56色调色板,您在C#中指定的任何颜色都“翻译”为其中一种调色板颜色。有效的ColorIndex值介于1和56之间。另请参阅调色板和Excel上的此有用参考。
答案 1 :(得分:0)
尝试使用十六进制值,例如0xFFFFFF
。
如果不起作用,请尝试使用((Range)Globals.ThisWorkbook.Application.Selection).Interior.Color = Excel.XlRgbColor.rgbCornflowerBlue;
此处的限制是您只能使用Excel的调色板 - 如果您想要颜色之外的颜色,则必须更改调色板programatically或manually。如果您的颜色不在调色板中,Excel将选择最接近的匹配。
答案 2 :(得分:0)
我找到了一个完全不同的解决方案,因此我不再需要在数据透视表中为任何颜色设置颜色=)
我做了一个完全重写,所以不是将数据透视表保存到数据库并重新生成它们,我只是将数据透视表保存到xlsx文件并从那里恢复它们。