问题
I want to merge cells and to do it multiple times... The reason I'm using this merge its because I want more the one table. and a different number of columns for each table and different size of a column on the another... The number and the places of the merges are unknown
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp111
{
class Program
{
static void Main(string[] args)
{
string x = @"C:\Users\hed-b\source\repos\TESTUserFormFromExcel\ConsoleApp111\bin\Debug\TEST.xltm";
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application
{
DisplayAlerts = false,
Visible = false,
ScreenUpdating = false,
};
Workbook wb = xlApp.Workbooks.Open(x);
Worksheet ws = (Worksheet)wb.Worksheets[1];
if (ws == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
return;
}
Stopwatch stopWatch = Stopwatch.StartNew();
for (int i = 2; i <= 10; i++)
{
ws.get_Range("A"+i, "B"+i).Merge();
}
for (int i = 2; i <= 10; i++)
{
ws.get_Range("C" + i, "D" + i).Merge();
}
for (int i = 2; i <= 10; i++)
{
ws.get_Range("E" + i, "H" + i).Merge();
}
Console.WriteLine(stopWatch.ElapsedMilliseconds);
stopWatch = Stopwatch.StartNew();
ws.get_Range("A" + 2, "H" + 10).Value2 = new string[10, 5]{
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
{ "aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaa","","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
};
ws.get_Range("A" + 11, "H" + 15).Value2 = new string[5, 8]{
{ "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
{ "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
{ "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
{ "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"},
{ "bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb","bbbbb"}
};
Console.WriteLine(stopWatch.ElapsedMilliseconds);
xlApp.ScreenUpdating = true;
xlApp.Visible = true;
}
}
}
I just looking for better way to do it faster
All these example program merges take about 1724 ms. in my actual program I have much more merges and take more than 5000 ms. This merges its what take the most of the time...
回答1:
It'll probably perform better if you execute the merge at once and let Excel handle the multiple ranges you need to merge.
I.e. define a range object which holds the multiple areas and call the Merge
on it. It won't merge everything into one big fat cell, but will merge the multiple areas as defined:
var ranges = String.Empty();
for (int i = 2; i <= 10; i++)
{
ranges += $"A{i}:B{i},C{i}:D{i},E{i}:H{i},";
}
ws.get_Range(ranges.TrimEnd(',')).Merge();
来源:https://stackoverflow.com/questions/47667989/c-sharp-excel-interop-merge-cells-in-fast-way