| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c# >> C#合并多个结构一样的Excel C# he bing duo ge jie gou yi yang de Excel
 

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

2008-08-20 08:08:40 编辑來源:互联网 繁體版 评论
 
 
本文为【C#合并多个结构一样的Excel】的汉字拼音对照版显示拼音
  hebingdaimaruxia

  using System;

  using System.Collections.Generic;

  using System.Text;

  using System.Reflection;

  using Excel = Microsoft.Office.Interop.Excel;

  namespace ConsoleApplication20

  {

   //tianjiayinyong-COM-MicroSoft Excel 11.0 Object Libery

   class Program

   {

   static void Main(string[] args)

   {

   //MweibiaogekuandubiaozhiExcelzhongdediMlieweizuihouyilie3weibiaotougaodu

   MergeExcel.DoMerge(new string[]

   {

   @"E:\excel\leixingA\gongsiA.xls",

   @"E:\excel\leixingA\gongsiB.xls"

   },

   @"E:\excel\leixingA\hebingceshi.xls", "M", 3);

   MergeExcel.DoMerge(new string[]

   {

   @"E:\excel\leixingB\tongjibiaoA.xls",

   @"E:\excel\leixingB\tongjibiaoB.xls"

   },

   @"E:\excel\leixingB\hebingceshi.xls", "I", 4);

   }

  

   }

   public class MergeExcel

   {

  

   Excel.Application app = new Microsoft.Office.Interop.Excel.ApplicationClass();

   //baocunmubiaodeduixiang

   Excel.Workbook bookDest = null;

   Excel.Worksheet sheetDest = null;

   //duqushujudeduixiang

   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, Missing.Value) as Excel.Worksheet;

   sheetDest.Name = "Data";

   _sourceFiles = sourceFiles;

   _destFile = destFile;

   _columnEnd = columnEnd;

   _headerRowCount = headerRowCount;

   }

   /// <summary>

   /// dakaigongzuobiao

   /// </summary>

   /// <param name="fileName"></param>

   void OpenBook(string fileName)

   {

   bookSource = app.Workbooks._Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value

   , Missing.Value, Missing.Value, Missing.Value, Missing.Value

   , Missing.Value, Missing.Value, Missing.Value, Missing.Value);

   sheetSource = bookSource.Worksheets[1] as Excel.Worksheet;

   }

   /// <summary>

   /// guanbigongzuobiao

   /// </summary>

   void CloseBook()

   {

   bookSource.Close(false, Missing.Value, Missing.Value);

   }

   /// <summary>

   /// fuzhibiaotou

   /// </summary>

   void CopyHeader()

   {

   Excel.Range range = sheetSource.get_Range("A1", _columnEnd + _headerRowCount.ToString());

   range.Copy(sheetDest.get_Range("A1",Missing.Value));

   _currentRowCount += _headerRowCount;

   }

   /// <summary>

   /// fuzhishuju

   /// </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>

   /// baocunjieguo

   /// </summary>

   void Save()

   {

   bookDest.Saved = true;

   bookDest.SaveCopyAs(_destFile);

   }

   /// <summary>

   /// 退tuichujincheng

   /// </summary>

   void Quit()

   {

   app.Quit();

   }

   /// <summary>

   /// hebing

   /// </summary>

   void DoMerge()

   {

   bool b = false;

   foreach (string strFile in _sourceFiles)

   {

   OpenBook(strFile);

   if (b == false)

   {

   CopyHeader();

   b = true;

   }

   CopyData();

   CloseBook();

   }

   Save();

   Quit();

   }

   /// <summary>

   /// hebingbiaoge

   /// </summary>

   /// <param name="sourceFiles">yuanwenjian</param>

   /// <param name="destFile">mubiaowenjian</param>

   /// <param name="columnEnd">zuihouyiliebiaozhi</param>

   /// <param name="headerRowCount">biaotouxingshu</param>

   public static void DoMerge(string[] sourceFiles, string destFile, string columnEnd, int headerRowCount)

   {

   new MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge();

   }

   }

  }原文】【拼音
 
 
 
 
上一篇《微软.NET Visual Studio 2008 功能提升》
下一篇《C#代码与JavaScript函数的相互调用》
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
合并代码如下: 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, 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, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value); sheetSource = bookSource.Worksheets[1] as Excel.Worksheet; } /// <summary> /// 关闭工作表 /// </summary> void CloseBook() { bookSource.Close(false, Missing.Value, 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, string destFile, string columnEnd, int headerRowCount) { new MergeExcel(sourceFiles, destFile, columnEnd, headerRowCount).DoMerge(); } } }
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
华丽的模特拍摄(8)
华丽的模特拍摄(7)
华丽的模特拍摄(6)
华丽的模特拍摄(5)
八里沟
朝阳宫
天下峨眉 云上金顶
老家(四)
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有