2017-08-09 73 views
-2

Excel Cells background colered image sampleC#計算Excel單元

我想計數,隨着以方法和用於每個五列傳遞的值相匹配希望黃背彩色單元把計數值在最後一個空單元格中。

例如:A53至E53三個值匹配,並繪所以希望把(3)中至F53然後G53(3)K53到L53和M53(2)在以R53

這裏是如果整數值與單元格值匹配,則代碼繪製背景。然而在比賽結束後,我也想計算泛黃的細胞,並在每完成5次計數後將計數值放入最後一個細胞。也請看圖像爲excel視圖。

private void ExcelFindCellValue(string[] number, string cnums, string mnumber, bool bmega) 
    { 
     string fc = ""; 
     string sc = ""; 
     Excel.Workbook xlWorkBook; 
     objexcel = new Excel.Application(); 
     object misValue = System.Reflection.Missing.Value; 
     if (!bmega) 
     { 
      if (cnums == "1") 
      { fc = "A3"; sc = "E202"; } 
      if (cnums == "2") 
      { fc = "H3"; sc = "L202"; } 
      if (cnums == "3") 
      { fc = "O3"; sc = "S202"; } 
     } 
     else if (bmega) 
     { 
      if (cnums == "1") 
      { fc = "F3"; sc = "F202"; } 
      if (cnums == "2") 
      { fc = "M3"; sc = "M202"; } 
      if (cnums == "3") 
      { fc = "T3"; sc = "T202"; } 
      number[0] = mnumber; 
      number[1] = ""; number[2] = ""; number[3] = ""; number[4] = ""; 
     } 
     objexcel.DisplayAlerts = false; 
     xlWorkBook = objexcel.Workbooks.Open(filePath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, true, 0, true, 1, 0); 
     xlWorkBook.CheckCompatibility = false; 

     for (int i = 0; i < number.Length; i++) 
     { 
      Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
      Excel.Range currentFind = null; 
      Excel.Range firstFind = null; 

      Excel.Range exRange = (Excel.Range)xlWorkSheet.get_Range(fc, sc); 
      // You should specify all these parameters every time you call this method, 
      // since they can be overridden in the user interface. 

      currentFind = exRange.Find(number[i], misValue, 
      Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, 
      Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, true, 
      misValue, misValue); 

      while (currentFind != null) 
      { 
       // Keep track of the first range you find. 
       if (firstFind == null) 
       { 
        firstFind = currentFind; 
       } 

       // If you didn't move to a new range, you are done. 
       else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) 
         == firstFind.get_Address(Excel.XlReferenceStyle.xlA1)) 
       { 
        break; 
       } 
       if (!bmega) 
       { 
        currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
        currentFind.Font.Bold = true; 

        currentFind.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
       } 
       else if (bmega) 
       { 
        currentFind.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
        currentFind.Font.Bold = true; 

        currentFind.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
       } 

       currentFind = exRange.FindNext(currentFind); 
      } 
      releaseObject(xlWorkSheet); 

     } 
     xlWorkBook.SaveAs(@filePath, misValue, 
      misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlNoChange, 
      misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook = objexcel.Workbooks.Add(misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     objexcel.Quit(); 
     releaseObject(xlWorkBook); 
    } 
+0

你有任何的代碼?你到目前爲止嘗試了什麼?請參見[如何創建最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve)。我們不是在這裏爲您編寫代碼,我們在這裏幫助您調試代碼。 – jmoon

+0

同上。請更加明確 – SCramphorn

+0

謝謝指教,我很感激。我添加了我的示例代碼。 –

回答

0

我解決了這個問題。這是一個醜陋但工作的代碼完成這項工作。當然,我會在稍後清理並改進此代碼。然而問題解決了!

private void FantasyExcelCalculateCellValue(string[] number, string cnums, string filePath) 
    { 
     string fc = ""; 
     string sc = ""; 
     int ifounded = 0; 
     Excel.Workbook xlWorkBook; 
     objexcel = new Excel.Application(); 
     object misValue = System.Reflection.Missing.Value; 
     if (cnums == "1") 
     { fc = "A2"; sc = "F2"; } 
     if (cnums == "2") 
     { fc = "G2"; sc = "L2"; } 
     if (cnums == "3") 
     { fc = "M2"; sc = "R2"; } 
     string sr1 = ""; 
     string fr1 = ""; 
     string ov1 = fc.Remove(0, 1); 
     string sr2 = ""; 
     string fr2 = ""; 
     string ov2 = sc.Remove(0, 1); 
     objexcel.DisplayAlerts = false; 
     objexcel.AutoCorrect.AutoExpandListRange = false; 
     xlWorkBook = objexcel.Workbooks.Open(filePath, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, true, 0, true, 1, 0); 
     xlWorkBook.CheckCompatibility = false; 
     Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     Excel.Range exRange = (Excel.Range)xlWorkSheet.get_Range(fc, sc); 
     xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     // Put another loop i from 2 to 500 and for each i increase range like A2 + 1 (A3) F2 + 1 (F3) 
     for (int i = 2; i < 502; i++) 
     { 
      sr1 = i.ToString(); 
      sr2 = i.ToString(); 
      fr1 = fc.Replace(ov1, sr1); 
      fr2 = sc.Replace(ov2, sr2); 
      exRange = (Excel.Range)xlWorkSheet.get_Range(fr1, fr2); 
      int mj = 0; 
      ifounded = 0; 

      for (int m = 0; m < number.Length; m++) 
       { 
        Excel.Range currentFind = null; 
        Excel.Range firstFind = null; 
        // You should specify all these parameters every time you call this method, 
        // since they can be overridden in the user interface. 

        currentFind = exRange.Find(number[m], misValue, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, true, misValue, misValue); 
        while (currentFind != null) 
        { 
         // Keep track of the first range you find. 
         if (firstFind == null) 
         { 
          firstFind = currentFind; 
         } 

         // If you didn't move to a new range, you are done. 
         else if (currentFind.get_Address(Excel.XlReferenceStyle.xlA1) == firstFind.get_Address(Excel.XlReferenceStyle.xlA1)) 
         { 
          break; 
         } 


        int p = 0; 
        if (cnums == "1") 
        { 
         p = 6; 
        } 
        if (cnums == "2") 
        { 
         p = 12; 
        } 
        if (cnums == "3") 
        { 
         p = 18; 
        } 
        string icolor = currentFind.Interior.Color.ToString(); 
        if (icolor == "65535") 
        { 
         ifounded++; 
         if (ifounded > 3) 
         { 
          xlWorkSheet.Cells[i, p] = ifounded.ToString(); 
          Excel.Range range = xlWorkSheet.get_Range(fr2, fr2) as Excel.Range; 
          range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 
          range.Font.Bold = true; 
          range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
         } 
        } 
        currentFind = exRange.FindNext(currentFind); 
        } 
        mj++; 
      } 
     } 
     xlWorkBook.SaveAs(@filePath, misValue, 
      misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlNoChange, 
      misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook = objexcel.Workbooks.Add(misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     objexcel.Quit(); 
     releaseObject(xlWorkBook); 
    }