2

こんにちは、コードの色をピボットテーブルにしようとしています。本来のセルに色を付けるだけでうまく機能しますが、テーブルを更新すると、色がピボットテーブルに適切に添付されていないかのように、すべての色が消えます。

私は次のコードを持っています (これはより大きなコードからの抜粋です):

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) を参照する変数です。これにより、間違ったブックで VSTO がコードを実行することなく、2 つの Excel を同時に操作することができます。

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);
                }
}
4

3 に答える 3

1

C# VSTO を使用している場合は、Selection.Interior.Color を使用しないでください。代わりに Selection.Interior.ColorIndex を使用してください。Excel は 56 色のパレットを使用し、C# で指定した色は、それらのパレットの色の 1 つに "変換" されます。有効な ColorIndex 値は 1 ~ 56 です。また、カラー パレットと Excel に関するこの役立つリファレンスも確認してください。

http://www.mvps.org/dmcritchie/excel/colors.htm

于 2010-06-01T14:11:07.857 に答える
1

のように、代わりに 16 進値を使用してみてください0xFFFFFF

それがうまくいかない場合は、次のようなXlRgbColor色定数を使用してみてください((Range)Globals.ThisWorkbook.Application.Selection).Interior.Color = Excel.XlRgbColor.rgbCornflowerBlue;

ここでの制限は、Excel のパレットしか使用できないことです。それ以外の色が必要な場合は、プログラムまたは手動でパレットを変更する必要があります。色がパレットにない場合、Excel は最も近い色を選択します。

于 2010-05-22T07:40:31.717 に答える
0

この機能に対するまったく別の解決策を見つけたので、ピボットテーブルで何も色を付ける必要がなくなりました =)

全面的に書き直したので、ピボットテーブルをデータベースに保存して再生成する代わりに、ピボットテーブルを xlsx ファイルに保存してそこから復元するだけです。

于 2010-06-17T08:05:45.970 に答える