有多个结构一样的Excel,带复杂表头需要合并为一个,且去掉多余的表头数据,可以用COM组件来读取每个Excel表格的Range来合并到一个新的表格中。样例如图
有很多相同格式的表格,合并代码如下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace ConsoleApplication20
- {
- //添加引用-COM-MicroSoft Excel 11.0 Object Libery
- class Program
- {
- static void Main(string[] args)
- {
- //M为表格宽度标志(Excel中的第M列为最后一列),3为表头高度
- MergeExcel.DoMerge(new string[]
- {
- @"E:\excel\类型A\公司A.xls",
- @"E:\excel\类型A\公司B.xls"
- },
- @"E:\excel\类型A\合并测试.xls", "M", 3);
- MergeExcel.DoMerge(new string[]
- {
- @"E:\excel\类型B\统计表A.xls",
- @"E:\excel\类型B\统计表B.xls"
- },
- @"E:\excel\类型B\合并测试.xls", "I", 4);
- }
- }