Fastest way to get an Excel Range of Rows

前端 未结 5 530
青春惊慌失措
青春惊慌失措 2020-12-10 05:32

In a VSTO C# project I want to get a range of rows from a set of row indexes.

The row indexes can be for example like \"7,8,9,12,14\".

Then I want the range

5条回答
  •  失恋的感觉
    2020-12-10 06:13

    This code assign color to range cells based on criteria:

    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++;
    }

提交回复
热议问题