C#合并多个结构一样的Excel

来源:互联网 发布:淘宝集市怎么进入 编辑:程序博客网 时间:2024/05/24 05:50
 合并代码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Reflection;
  5. using Excel = Microsoft.Office.Interop.Excel;
  6. namespace ConsoleApplication20
  7. {
  8.     //添加引用-COM-MicroSoft Excel 11.0 Object Libery
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //M为表格宽度标志(Excel中的第M列为最后一列),3为表头高度
  14.             MergeExcel.DoMerge(new string[] 
  15.             {
  16.                 @"E:/excel/类型A/公司A.xls"
  17.                 @"E:/excel/类型A/公司B.xls" 
  18.             },
  19.                 @"E:/excel/类型A/合并测试.xls""M", 3);
  20.             MergeExcel.DoMerge(new string[] 
  21.             {
  22.                 @"E:/excel/类型B/统计表A.xls"
  23.                 @"E:/excel/类型B/统计表B.xls" 
  24.             },
  25.                 @"E:/excel/类型B/合并测试.xls""I", 4);
  26.         }
  27.  
  28.         
  29.     }
  30.     public class MergeExcel
  31.     {
  32.         
  33.         Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();
  34.         //保存目标的对象
  35.         Excel.Workbook bookDest = null;
  36.         Excel.Worksheet sheetDest = null;
  37.         //读取数据的对象 
  38.         Excel.Workbook bookSource = null;
  39.         Excel.Worksheet sheetSource = null;
  40.         string[] _sourceFiles = null;
  41.         string _destFile = string.Empty;
  42.         string _columnEnd = string.Empty;
  43.         int _headerRowCount = 1;
  44.         int _currentRowCount = 0;
  45.         public MergeExcel(string[] sourceFiles,string destFile,string columnEnd,int headerRowCount)
  46.         {
  47.             
  48.             bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);
  49.             sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;
  50.             sheetDest.Name = "Data";
  51.             _sourceFiles = sourceFiles;
  52.             _destFile = destFile;
  53.             _columnEnd = columnEnd;
  54.             _headerRowCount = headerRowCount;
  55.         }
  56.         /// 
  57.         /// 打开工作表
  58.         /// 
  59.         /// 
  60.         void OpenBook(string fileName)
  61.         {
  62.             bookSource = app.Workbooks._Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value
  63.                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
  64.                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  65.             sheetSource = bookSource.Worksheets[1] as Excel.Worksheet;
  66.         }
  67.         /// 
  68.         /// 关闭工作表
  69.         /// 
  70.         void CloseBook()
  71.         {
  72.             bookSource.Close(false, Missing.Value, Missing.Value);
  73.         }
  74.         /// 
  75.         /// 复制表头
  76.         /// 
  77.         void CopyHeader()
  78.         {
  79.             Excel.Range range = sheetSource.get_Range("A1", _columnEnd + _headerRowCount.ToString());
  80.             range.Copy(sheetDest.get_Range("A1",Missing.Value));
  81.             _currentRowCount += _headerRowCount;
  82.         }
  83.         /// 
  84.         /// 复制数据
  85.         /// 
  86.         void CopyData()
  87.         {
  88.             int sheetRowCount = sheetSource.UsedRange.Rows.Count;
  89.             Excel.Range range = sheetSource.get_Range(string.Format("A{0}", _headerRowCount + 1), _columnEnd + sheetRowCount.ToString());
  90.             range.Copy(sheetDest.get_Range(string.Format("A{0}", _currentRowCount + 1), Missing.Value));
  91.             _currentRowCount += range.Rows.Count;
  92.         }
  93.         /// 
  94.         /// 保存结果
  95.         /// 
  96.         void Save()
  97.         {
  98.             bookDest.Saved = true;
  99.             bookDest.SaveCopyAs(_destFile);
  100.         }
  101.         /// 
  102.         /// 退出进程
  103.         /// 
  104.         void Quit()
  105.         {
  106.             app.Quit();
  107.         }
  108.         /// 
  109.         /// 合并
  110.         /// 
  111.         void DoMerge()
  112.         {
  113.             bool b = false;
  114.             foreach (string strFile in _sourceFiles)
  115.             {
  116.                 OpenBook(strFile);
  117.                 if (b == false)
  118.                 {
  119.                     CopyHeader();
  120.                     b = true;
  121.                 }
  122.                 CopyData();
  123.                 CloseBook();
  124.             }
  125.             Save();
  126.             Quit();
  127.         }
  128.         /// 
  129.         /// 合并表格
  130.         /// 
  131.         /// 源文件
  132.         /// 目标文件
  133.         /// 最后一列标志
  134.         /// 表头行数
  135.         public static void DoMerge(string[] sourceFiles, string destFile, string columnEnd, int headerRowCount)
  136.         {
  137.             new MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge();
  138.         }
  139.     }
  140. }