C#Excel交互快速合并单元格
我想合并单元格,并多次执行它…我使用这个合并它的原因,因为我想要更多的一个表。 和每个表的不同数量的列和另一个列的不同大小…合并的数量和地点是未知的
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毫秒。 在我的实际程序中,我有更多的合并,超过5000毫秒。 这融合了大部分时间…
如果您一次执行合并,并让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();