2010-05-19 127 views
2

嗨,我試圖使一些代碼顏色數據透視表。 它可以很好地爲單元格着色,但如果刷新表格,所有的顏色都會消失,就好像顏色沒有正確粘貼到數據透視表一樣。C#VSTO:着色可移動單元格

我有以下代碼(這是從更大的代碼剪斷):

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個Excels,而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

@tomboz:下面的答案是否回答你的問題?如果沒有,你可以發佈更多的代碼/細節,我會盡力提供更詳細的分析。 – 2010-06-08 05:57:30

回答

0

嘗試使用十六進制值代替,如0xFFFFFF

如果還是不行,請嘗試使用XlRgbColor顏色常量像((Range)Globals.ThisWorkbook.Application.Selection).Interior.Color = Excel.XlRgbColor.rgbCornflowerBlue;

這裏的限制是,你只能使用Excel的調色板 - 如果你想的那個之外的顏色,你就必須改變調色板可以是programaticallymanually。如果您的顏色不在調色板中,Excel將選擇最接近的匹配項。

+0

終於回到了這個項目中。 無法使用十六進制工作:( – user344858 2010-06-10 11:56:34

+0

@tomboz:另外一個選項供您嘗試,如上所述 – 2010-06-10 16:31:06

1

如果您使用的是C#VSTO,請不要使用Selection.Interior.Color。改爲使用Selection.Interior.ColorIndex。 Excel使用56色調色板,並且您在C#中指定的任何顏色都被「翻譯」爲其中一種調色板顏色。有效的ColorIndex值介於1和56之間。另請查看調色板和Excel中的此有用參考。

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

+0

這也不起作用...它確實爲單元格着色,但刷新了數據透視表使細胞再次變白... 這證明了一個難以解決的問題 – user344858 2010-06-10 11:57:09

0

我發現了一個完全不同的解決了這一功能,所以我不再需要在數據透視表上色任何=)

我做了一個完全重寫,因此而不是保存數據透視表的DATABSE並重新生成它們,我只需將數據透視表保存爲xlsx文件並從那裏恢復它們。

相關問題