c#中合并excel表格的方法示例

c#中合并excel表格的方法示例,第1张

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

有多个结构一样的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);    }      }   public  class MergeExcel  {        Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();     //保存目标的对象    Excel.Workbook bookDest = null ;    Excel.Worksheet sheetDest = null ;     //读取数据的对象     Excel.Workbook bookSource = null ;    Excel.Worksheet sheetSource = null ;     string [] _sourcefiles = null ;     string _destfile = string .Empty;     string _columnEnd = string .Empty;     int _headerRowCount = 1;     int _currentRowCount = 0;     public MergeExcel( string [] sourcefiles,string destfile,string columnEnd,int headerRowCount)    {            bookDest = (Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);      sheetDest = bookDest.Worksheets.Add(Missing.Value,Missing.Value,Missing.Value) as Excel.Worksheet;      sheetDest.name = "Data" ;      _sourcefiles = sourcefiles;      _destfile = destfile;      _columnEnd = columnEnd;      _headerRowCount = headerRowCount;    }     /// <summary>     /// 打开工作表     /// </summary>     /// <param name="filename"></param>     voID OpenBook( string filename)    {      bookSource = app.Workbooks._Open(filename,Missing.Value);      sheetSource = bookSource.Worksheets[1] as Excel.Worksheet;    }     /// <summary>     /// 关闭工作表     /// </summary>     voID CloseBook()    {      bookSource.Close( false,Missing.Value);    }     /// <summary>     /// 复制表头     /// </summary>     voID copyheader()    {      Excel.Range range = sheetSource.get_Range( "A1",_columnEnd + _headerRowCount.ToString());      range.copy(sheetDest.get_Range( "A1",Missing.Value));      _currentRowCount += _headerRowCount;    }     /// <summary>     /// 复制数据     /// </summary>     voID copyData()    {       int sheetRowCount = sheetSource.UsedRange.Rows.Count;      Excel.Range range = sheetSource.get_Range( string .Format( "A{0}",_headerRowCount + 1),_columnEnd + sheetRowCount.ToString());      range.copy(sheetDest.get_Range( string .Format( "A{0}",_currentRowCount + 1),Missing.Value));      _currentRowCount += range.Rows.Count;    }     /// <summary>     /// 保存结果     /// </summary>     voID Save()    {      bookDest.Saved = true ;      bookDest.SavecopyAs(_destfile);    }     /// <summary>     /// 退出进程     /// </summary>     voID Quit()    {      app.Quit();    }     /// <summary>     /// 合并     /// </summary>     voID DoMerge()    {       bool b = false ;       foreach ( string strfile in _sourcefiles)      {        OpenBook(strfile);         if (b == false )        {          copyheader();          b = true ;        }        copyData();        CloseBook();      }      Save();      Quit();    }     /// <summary>     /// 合并表格     /// </summary>     /// <param name="sourcefiles">源文件</param>     /// <param name="destfile">目标文件</param>     /// <param name="columnEnd">最后一列标志</param>     /// <param name="headerRowCount">表头行数</param>     public  static  voID DoMerge( string [] sourcefiles,int headerRowCount)    {       new MergeExcel(sourcefiles,destfile,columnEnd,headerRowCount).DoMerge();    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的c#中合并excel表格的方法示例全部内容,希望文章能够帮你解决c#中合并excel表格的方法示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存