-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);
}
你有任何的代碼?你到目前爲止嘗試了什麼?請參見[如何創建最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve)。我們不是在這裏爲您編寫代碼,我們在這裏幫助您調試代碼。 – jmoon
同上。請更加明確 – SCramphorn
謝謝指教,我很感激。我添加了我的示例代碼。 –