颜色不连续的单元格在一个Excel工作表 [英] Color non-consecutive cells in an Excel sheet
问题描述
这是会发生什么:
xlValues
设置为 Excel.Range $ C $ 。C>对象
我曾尝试以下为好,全部给我同样的错误:
I have tried the following as well, all giving me the same error:
//xlValueRange = xlSheet...
.get_Range("A1:A5,A15:A25,A50:A65");
.UsedRange.Range["A1:A5,A15:A25,A50:A65"];
.Range["A1:A5,A15:A25,A50:A65"];
xlApp.ActiveWorkbook.ActiveSheet.Range["A1:A5,A15:A25,A50:A65"];
//I have also tried these alternatives with ".Select()" after the brackets and
//", Type.Missing" inside the brackets
//This works though...
xlSheet.Range["A1:A5"];
我试图重新着色在特定的细胞Excel工作表,我已经找到了解决方案,通过使用两个循环,但它只是太慢了。 。贯穿30个细胞的一列只需要几分钟
I'm trying to recolor specific cells in an excel sheet, I have found a solution by using two loops but it's simply too slow. Running through a column of 30 000 cells takes minutes.
我以前从来没有做过这样的事,我用的本教程让我开始。
I have never done anything like this before and I used this tutorial to get me started.
此解决方案使用与被着色设置为true细胞的BOOL数组。(重新着色)
This solution uses a bool array with cells to be colored set to true.(recolored)
//using Excel = Microsoft.Office.Interop.Excel;
xlApp = new Excel.Application();
xlApp.Visible = true;
xlBook = xlApp.Workbooks.Add(Type.Missing);
xlSheet = (Excel.Worksheet)xlBook.Sheets[1];
for (int i = 1; i < columns + 1; i++)
{
for (int j = 1; j < rows + 1; j++)
{
if (recolored[j, i])
xlSheet.Cells[j+1, i+1].Interior.Color = Excel.XlRgbColor.rgbRed;
}
}
}
我想怎么办是这样的:
What I would like to do is something like this:
Excel.XlRgbColor[,] color;
//Loop to fill color with Excel.XlRgbColor.rgbRed at desired cells.
var startCell = (Excel.Range)xlSheet.Cells[1, 1];
var endCell = (Excel.Range)xlSheet.Cells[rows, columns];
var xlRange = xlSheet.Range[startCell, endCell];
xlRange.Interior.Color = color;
这人会想到让我在最后一行的错误;
This one gives me an error on the final line though;
类型不匹配。 (从HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))
我的第一个猜测是,使
RangeObject.Interior.Color = Excel.XlRgbColor.rgbRed;
我不知道是否有可能使一个 Excel.Range
有这样的差距对象虽然,我可以用这一个一定的帮助。
I don't know if it's possible to make an Excel.Range
object with gaps like that though, I could use some help on this one.
推荐答案
我有同样的问题,事实证明,这是一个不好的列表分隔符 - 在我的情况,而不是逗号应该有一个分号
I had the same problem and it turned out that it was a bad list separator - in my case instead of comma there should be a semicolon.
因此,而不是
.Range["A1:A5,A15:A25,A50:A65"];
尝试:
try:
private string listSep = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
.Range["A1:A5" + listSep + "A15:A25" + listSep + "A50:A65"];
这篇关于颜色不连续的单元格在一个Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!