我想合并单元格并多次执行... 我之所以使用它合并它的原因是因为我想要更多的一个表。 每个表的列数不同,另一个列的列大小不同...... 合并的数量和位置未知
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;
}
}
}
我只是想找到更好的方法来更快地完成它
所有这些示例程序合并大约需要1724 ms。在我的实际程序中,我有更多的合并,需要超过5000毫秒。 这合并了大部分时间......
答案 0 :(得分:2)
如果您立即执行合并并让Excel处理您需要合并的多个范围,它可能会表现得更好。
即。定义一个范围对象,它包含多个区域并在其上调用Merge
。它不会将所有内容合并到一个大的胖单元中,但会按照定义合并多个区域:
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();