非常に簡単な解決策は、コンマ区切り形式でさまざまな領域を指定することです。
sheet.get_Range( "A1:B1,E1:G1");
プログラムによる範囲の組み合わせには、ExcelApplicationオブジェクトのUnion
andメソッドもあります。Intersection
これらは、多くのオプションのパラメーターがあるため、C#で使用するには少し不器用です。こちらをご覧ください
http://codeidol.com/csharp/c-sharp-in-office/Working-with-Excel-Objects/Working-with-the-Range-Object/
たとえば。
編集:いくつかの追加のヒント:
あなたの場合、最初に「ColumnsToKeep」の「ColumnsToSkip」を変換する必要があります。これは、あらゆる種類のセル結合に必要なものだからです。Linqソリューションは次のとおりです。
int[] ColumnsToKeep = Enumerable.Range(StartColumn, EndColumn -StartColumn + 1)
.Except(ColumnsToSkip)
.ToArray();
次に、この例の線に沿って何かを作成できます。
Excel.Range totalRange = null;
foreach(int col in ColumnsToKeep)
{
totalRange = Union(excelApp,totalRange,(Excel.Range)sh.Cells[row, col]);
}
ここで、「Union」は次のように定義されます。
static Excel.Range Union(Excel.Application app, Excel.Range r1, Excel.Range r2)
{
if (r1 == null && r2 == null)
return null;
if (r1 == null)
return r2;
if (r2 == null)
return r1;
return app.Union(r1, r2,
null, null, null, null, null, null,
null, null, null, null, null, null,
null, null, null, null, null, null,
null, null, null, null, null, null,
null, null, null, null);
}