Hja*_*r Z 5 c# excel office-interop
这是发生的事情:

xlValues被设置为Excel.Range对象.
我也试过以下,都给了我同样的错误:
//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"];
Run Code Online (Sandbox Code Playgroud)
我正在尝试重新着色excel表中的特定单元格,我已经通过使用两个循环找到了解决方案,但它太慢了.通过一个30 000个单元格的列需要几分钟.
我之前从未做过这样的事情,而且我用这个教程让我开始.
此解决方案使用bool数组,将要着色的单元格设置为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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的事情:
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;
Run Code Online (Sandbox Code Playgroud)
这个给了我最后一行的错误;
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
我的第一个猜测是创建一个Excel.Range覆盖我想要红色的单元格的对象,并使用该对象代替xlRange,如下所示:
RangeObject.Interior.Color = Excel.XlRgbColor.rgbRed;
Run Code Online (Sandbox Code Playgroud)
我不知道是否可以制作一个Excel.Range像这样的间隙的物体,但我可以在这个上使用一些帮助.
小智 1
我遇到了同样的问题,结果发现这是一个错误的列表分隔符 - 在我的例子中,应该用分号而不是逗号。
所以而不是
.Range["A1:A5,A15:A25,A50:A65"];
Run Code Online (Sandbox Code Playgroud)
尝试:
private string listSep = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
.Range["A1:A5" + listSep + "A15:A25" + listSep + "A50:A65"];
Run Code Online (Sandbox Code Playgroud)