zoukankan      html  css  js  c++  java
  • C#合并多个结构一样的Excel

    有多个结构一样的Excel,带复杂表头需要合并为一个,且去掉多余的表头数据,可以用COM组件来读取每个Excel表格的Range来合并到一个新的表格中。样例如图


    有很多相同格式的表格,合并代码如下:

    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.      public   class  MergeExcel
    30.     {
    31.         
    32.         Excel.Application app =  new  Microsoft.Office.Interop.Excel.ApplicationClass();
    33.          //保存目标的对象
    34.         Excel.Workbook bookDest =  null ;
    35.         Excel.Worksheet sheetDest =  null ;
    36.          //读取数据的对象 
    37.         Excel.Workbook bookSource =  null ;
    38.         Excel.Worksheet sheetSource =  null ;
    39.          string [] _sourceFiles =  null ;
    40.          string  _destFile =  string .Empty;
    41.          string  _columnEnd =  string .Empty;
    42.          int  _headerRowCount = 1;
    43.          int  _currentRowCount = 0;
    44.          public  MergeExcel( string [] sourceFiles, string  destFile, string  columnEnd, int  headerRowCount)
    45.         {
    46.             
    47.             bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);
    48.             sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value)  as  Excel.Worksheet;
    49.             sheetDest.Name =  "Data" ;
    50.             _sourceFiles = sourceFiles;
    51.             _destFile = destFile;
    52.             _columnEnd = columnEnd;
    53.             _headerRowCount = headerRowCount;
    54.         }
    55.          /// <summary>
    56.          /// 打开工作表
    57.          /// </summary>
    58.          /// <param name="fileName"></param>
    59.          void  OpenBook( string  fileName)
    60.         {
    61.             bookSource = app.Workbooks._Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value
    62.                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value
    63.                 , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    64.             sheetSource = bookSource.Worksheets[1]  as  Excel.Worksheet;
    65.         }
    66.          /// <summary>
    67.          /// 关闭工作表
    68.          /// </summary>
    69.          void  CloseBook()
    70.         {
    71.             bookSource.Close( false , Missing.Value, Missing.Value);
    72.         }
    73.          /// <summary>
    74.          /// 复制表头
    75.          /// </summary>
    76.          void  CopyHeader()
    77.         {
    78.             Excel.Range range = sheetSource.get_Range( "A1" , _columnEnd + _headerRowCount.ToString());
    79.             range.Copy(sheetDest.get_Range( "A1" ,Missing.Value));
    80.             _currentRowCount += _headerRowCount;
    81.         }
    82.          /// <summary>
    83.          /// 复制数据
    84.          /// </summary>
    85.          void  CopyData()
    86.         {
    87.              int  sheetRowCount = sheetSource.UsedRange.Rows.Count;
    88.             Excel.Range range = sheetSource.get_Range( string .Format( "A{0}" , _headerRowCount + 1), _columnEnd + sheetRowCount.ToString());
    89.             range.Copy(sheetDest.get_Range( string .Format( "A{0}" , _currentRowCount + 1), Missing.Value));
    90.             _currentRowCount += range.Rows.Count;
    91.         }
    92.          /// <summary>
    93.          /// 保存结果
    94.          /// </summary>
    95.          void  Save()
    96.         {
    97.             bookDest.Saved =  true ;
    98.             bookDest.SaveCopyAs(_destFile);
    99.         }
    100.          /// <summary>
    101.          /// 退出进程
    102.          /// </summary>
    103.          void  Quit()
    104.         {
    105.             app.Quit();
    106.         }
    107.          /// <summary>
    108.          /// 合并
    109.          /// </summary>
    110.          void  DoMerge()
    111.         {
    112.              bool  b =  false ;
    113.              foreach  ( string  strFile  in  _sourceFiles)
    114.             {
    115.                 OpenBook(strFile);
    116.                  if  (b ==  false )
    117.                 {
    118.                     CopyHeader();
    119.                     b =  true ;
    120.                 }
    121.                 CopyData();
    122.                 CloseBook();
    123.             }
    124.             Save();
    125.             Quit();
    126.         }
    127.          /// <summary>
    128.          /// 合并表格
    129.          /// </summary>
    130.          /// <param name="sourceFiles">源文件</param>
    131.          /// <param name="destFile">目标文件</param>
    132.          /// <param name="columnEnd">最后一列标志</param>
    133.          /// <param name="headerRowCount">表头行数</param>
    134.          public   static   void  DoMerge( string [] sourceFiles,  string  destFile,  string  columnEnd,  int  headerRowCount)
    135.         {
    136.              new  MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge();
    137.         }
    138.     }
    139. }
  • 相关阅读:
    ATM+购物车
    subprocess,re,logging模块
    json,pickle,collections,openpyxl模块
    time,datatime,random,os,sys,hashlib模块
    1.内置函数剩余部分 map reduce filter 2.函数递归 3.模块
    生成器,面向过程编程,三元表达式,列表生成式,生成器表达式,匿名函数,内置函数
    Ajax数据对接出问题了?ThingJS解决方法在这里
    测试
    简单低成本的物联网开发平台-ThingJS
    用ThingJS之CityBuilder快搭3D场景,可视化开发必备
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6204886.html
Copyright © 2011-2022 走看看