【发布时间】:2017-08-09 21:28:07
【问题描述】:
Excel Cells background colered image sample
我想计算与传入方法的值相匹配的黄色背面颜色的单元格,并且每五列想在最后一个空单元格中放置一个计数值。
例如: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);
}
【问题讨论】:
-
你有代码吗?到目前为止你有什么尝试?见How to create a Minimal, Complete, and Verifiable example。我们不是来为您编写代码的,我们是来帮助您调试代码的。
-
同上。请更明确
-
感谢您的建议,我很感激。我添加了我的示例代码。
-
如果您需要任何进一步的解释,请告诉我
标签: c# .net excel windows visual-studio