Excel Get_Range with multiple areas

后端 未结 3 1022

I\'m trying to get a range from Excel, which has multiple areas specified, essentially I\'ve got...

int StartColumn
int EndColumn
int[] ColumnsToSkip

3条回答
  •  佛祖请我去吃肉
    2020-12-19 03:31

    Try this:

    using Excel = Microsoft.Office.Interop.Excel;
    
    1. Collect your ranges into an array:
    Excel.Range[] ranges = new Excel.Range[] {yourRange1, yourRange2, ... };
    
    1. Create string-range with ranges addresses and get multi-range from it:
    string multiRangeStr = "";
    foreach (Excel.Range range in ranges)
    {
        string address = range.Address[true, true, Excel.XlReferenceStyle.xlA1];
        multiRangeStr += (multiRangeStr == "" ? "" : ";") + address;
    }
    //output: multiRangeStr: "A1:A3;B1:B3"
    
    Excel.Range multiRange = wsheet.Range(multiRangeStr);
    

提交回复
热议问题