C#将一个excel文件中的某个表单带格式复制到另一个excel文件中(已废弃,新版见内容)

最新的解决方案位置C#带格式复制excel

这个解决方案非常强行的把excel变成了图片,所以如果源文件有格式问题的话,目标文件也会把源文件的格式问题一并复制过去,而且无法修改。其次,该解决方案进行复制的时候每复制一次就会添加一个新的sheet,非常蛋疼。实在是我的无奈之举。嘛,如果只是拿去做期末的课程设计的话说不定可以拿去糊弄老师。

 

using Microsoft.Office.Interop.Excel;//必须要调用的,如果在vs里搜索不到的话可以直接导入office的excel.exe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp2
{
    class ExcelCopy
    {

     /// <summary>
    /// C#将一个excel文件中的某个表单带格式复制到另一个excel文件中(非常强行,不想糊弄客户的话不要用)
    /// </summary>

  /// <param name="firstRow">含有数据的copyfile的首个单元格</param>

        /// <param name="lastRow">含有数据的copyfile的最后一个单元格</param>
    /// <param name="copyfilePosition">欲复制文件的位置</param>
    /// <param name="targetPosition">目标文件的位置</param>

        public void exCopy(string firstRow,string lastRow,string copyfilePosition, string targetPosition)
        {

      string maxRow = firstRow + ":" + lastRow;

            System.Windows.Forms.Clipboard.Clear();//清空剪贴板
            object missing = System.Reflection.Missing.Value;//设置空值
            ApplicationClass app1; 
            ApplicationClass app2;
            Worksheet ws2 = new Worksheet();
            app2 = new ApplicationClass();

            Workbook wb2 = app2.Workbooks.Open(copyfilePosition, false, false, missing, missing, missing, true, missing, missing, missing, missing, missing, missing, missing, missing);
            ws2 = (Worksheet)wb2.Sheets[1];
            Range r = ws2.Range[maxRow, Type.Missing];
            r.Copy();
            //var pointName1 = ws2 as Worksheet;
            app1 = new ApplicationClass();
            Workbook wb1 = app1.Workbooks.Open(targetPosition, false, false, missing, missing, missing, true, missing, missing, missing, missing, missing, missing, missing, missing);
            Worksheet wsadd = (Worksheet)wb1.Sheets.Add(missing, missing, missing, missing);
            Range r1 = wsadd.Range[maxRow, Type.Missing];
            r1.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteAll, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, System.Type.Missing, System.Type.Missing);

//下面是关闭excel进程并清空剪贴板以释放资源
            wb1.Save();
            wb2.Save();
            wb1.Close();
            wb2.Close();
            app1.Quit();
            app2.Quit();
            System.Windows.Forms.Clipboard.Clear();
            app2 = null;
            app1 = null;
        }
    }
}

 

调用举例:

 

static void Main(string[] args)
        {
            ExcelCopy ex = new ExcelCopy();
            string copyfilePosition = @"D:\1.xls";
            string targetPosition = @"D:\Temp\2.xls";
            string maxRow = @"A1:S26";
            string firstRow = "A1";
            string lastRow = "S26";
            ex.exCopy(firstRow,lastRow, copyfilePosition, targetPosition);
            Console.ReadKey();
        }

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值