获得行的Excel范围的最快方法(Fastest way to get an Excel Range

2019-06-28 04:23发布

在VSTO C#项目,我想从一组行索引获得一定范围内的行。

行索引可以是例如像“7,8,9,12,14”。

然后我想的范围内“7:9,12,14”行。

我现在这样做:

Range rng1 = sheet.get_Range("A7:A9,A12,A14", Type.Missing);
rng1  = rng1.EntireRow;

但它是由于字符串范围指定处理有点效率低下。

sheet.Rows["7:9"]

作品,但我不能给这个

sheet.Rows["7:9,12,14"] // Fails

Answer 1:

试试这个:

Sheet.Range("7:9,12:12,14:14")

编辑:很抱歉,如果在C#中使用VSTO它应该是:

sheet.get_Range("7:9,12:12,14:14", Type.Missing)


Answer 2:

这里是你正在寻找的代码:

int startRow, endRow, startCol, endCol, row,col;
var singleData = new object[col];
var data = new object[row,col];
//For populating only a single row with 'n' no. of columns.
var startCell = (Range)worksheet.Cells[startRow, startCol];
startCell.Value2 = singleData;
//For 2d data, with 'n' no. of rows and columns.
var endCell = (Range)worksheet.Cells[endRow, endCol];
var writeRange = worksheet.Range[startCell, endCell];
writeRange.Value2 = data;

可以有整个范围,无论是1维或2细胞的二维阵列。

此方法是同时在整个excel工作表循环和填充数据在必要时以及当特别有帮助。



Answer 3:

我不是在C#中的专家,但据我所知,你必须使用EntireRow,你在上面所做的。 你正在寻找能够从实现的字符串.Address属性。 例如

    private void button1_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        Microsoft.Office.Interop.Excel.Range xlRange;

        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();

        xlWorkBook = xlexcel.Workbooks.Add();

        // Set Sheet 1 as the sheet you want to work with
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlRange = xlWorkSheet.get_Range("A7:A9,A12,A14", misValue);

        MessageBox.Show(xlRange.EntireRow.Address);

        xlRange = xlWorkSheet.get_Range(xlRange.EntireRow.Address, misValue);

        MessageBox.Show(xlRange.Address);
    }

所以,你可以写在上面

    private void button1_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        Microsoft.Office.Interop.Excel.Range xlRange;

        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();

        xlWorkBook = xlexcel.Workbooks.Add();

        // Set Sheet 1 as the sheet you want to work with
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        xlRange = xlWorkSheet.get_Range("$7:$9,$12:$12,$14:$14", misValue);

        MessageBox.Show(xlRange.Address);
    }

见部分

    xlRange = xlWorkSheet.get_Range("$7:$9,$12:$12,$14:$14", misValue);


Answer 4:

Reafidy的编辑答案是一个很好的开始 ,但我想它扩大超过我能在注释做。 sheet.get_Range(rangeselect)比再逐行快得多,但有一件事我还没有看到提及却又是该get_Range参数有255个字符限制。

要解决这个限制,构建一组范围的,如“8:8,10:13,14:55”为正常,然后使用此代码的变体:

string rangeSelectPart;
while (rangeSelect.Length >= 255)
{
    rangeSelectPart = rangeSelect.Substring(0, rangeSelect.Substring(0,255).LastIndexOf(','));
    Range multiRangePart = sheet.get_Range(rangeSelectPart, Type.Missing);

    //do something with the range here using multiRangePart 

    rangeSelect= rangeSelect.Substring(rangeSelectPart.Length + 1);
}
Range multiRange = sheet.get_Range(rangeSelect, Type.Missing);
// do the same something with the last part of the range using multiRange 
// now that the remaining rows are described in less than 255 characters

这将是比这样做对个别行的操作显著较快,但与大非连续的行集介绍也不会失败。


需要注意的是SutharMonil的回答是更快的方式在连续的矩形范围IFF设定值。 瓶颈从C#要优于通常是通过创建和更新,同时阻断COM对象的一再呼吁,他的回答很好地整合了电话。

不幸的是在我的测试,到目前为止,试图用它来与为string类型不是导致了错误类型非字符串属性的作用。 例如:

object[,] colors;
//use C# to set appropriate colors to each location in array...
for(int i = 0; i < colors.get_Length(0); i++){
    for(int j = 0; j < colors.get_Length(1); j++){
        colors[i,j] = XlThemeColor.xlThemeColorAccent6;
    }
}

//below causes a type error
formatRange.Interior.ThemeColor = color;

我会尽量记得更新,如果我得到它的工作。


最后一个重复的操作设定Globals.ThisAddIn.Application.ScreenUpdating = false; 然后将其设置为true时,即可大功告成。 没有这一点,Excel停止每组范围的属性被更新后更新屏幕,并可以大量的时间增加了操作。



Answer 5:

此代码分配颜色以基于标准的范围内的细胞:

 using Microsoft.Office.Interop.Excel; (...) var excel = new Application { Visible = true }; Workbook workbook = excel.Workbooks.Add(XlSheetType.xlWorksheet); Worksheet sheet = workbook.Sheets[1]; var i = 2; foreach (Data d in this.Datos) { sheet.Cells[i, 1].Value = d.Fecha; sheet.Cells[i, 2].Value = d.Ubicacion; sheet.Cells[i, 3].Value = d.Lote; sheet.Cells[i, 4].Value = d.ArticuloId; sheet.Cells[i, 5].Value = d.Articulo; sheet.Cells[i, 6].Value = d.ColorId; sheet.Cells[i, 7].Value = d.Color; sheet.Cells[i, 8].Value = d.StockOriginal; sheet.Cells[i, 9].Value = d.Diferencia; sheet.Cells[i, 10].Value = d.StockFinal; if (d.BackGroundColor == "#FFA061") sheet.Range[sheet.Cells[i, 1], sheet.Cells[i, 10]].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(255, 160, 97)); i++; } 



文章来源: Fastest way to get an Excel Range of Rows