C#操作EXCEL基类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Systool.com.util;
using Systool.com.constant;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;


namespace Systool.com.bases
{
    class BaseOfficeExcelUtil
    {
        protected Excel.Application app = new Excel.ApplicationClass();
        //模板sheet
        protected Excel.Worksheet module_ws = null;
        //新打开的工作簿
        protected Excel.Workbook new_wb = null;
        //新创建的sheet
        protected Excel.Worksheet new_ws = null;
               
       
        protected bool OpenExcelFile(String path, String fileName)
        {
            String wholeFileName = this.getWholePath(path, fileName);                     
            try
            {
                if (!this.isFileExist(path, fileName))
                {
                    String msg = StringDealUtil.PreMessage(MsgConsts.MSG_010, new String[2] { path, fileName });
                    MessageBox.Show(msg);
                    return false;
                }               
                //打开Excel文件
                this.new_wb = app.Workbooks.Open(wholeFileName, missing, missing, missing, missing, missing,
                           missing, missing, missing, missing, missing, missing, missing, missing, missing);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                String msg = StringDealUtil.PreMessage(MsgConsts.MSG_012, new String[2] { path, fileName });
                MessageBox.Show(msg);
                return false;
            }
           
        }
        protected bool CopySheet(String FileName, String sSheetName, String oSheetName)
        {
            Excel.Workbook fileWB = null;
            Excel.Worksheet fileWS = null;
            try
            {               
                //查找工作簿
                if (new_wb != null && new_wb.Name == FileName)
                {
                    fileWB = new_wb;
                }
                else
                {
                    Excel.Workbook ewb = null;
                  
                    for (int i =1; i <=app.Workbooks.Count;i++ )
                    {
                        ewb = app.Workbooks[i];
                        if (ewb.Name == FileName)
                        {
                            new_wb = ewb;
                            fileWB = ewb;
                            break;
                        }
                    }
                }
                //查找模板sheet
                if (module_ws != null && module_ws.Name == sSheetName)
                {
                    fileWS = module_ws;
                }
                else
                {
                    Excel.Worksheet ews = null;
                   
                    for (int i = 1; i <=fileWB.Worksheets.Count;i++ )
                    {
                        ews = (Excel.Worksheet)fileWB.Worksheets[i];
                        if (ews.Name == sSheetName)
                        {
                            module_ws = ews;
                            fileWS = ews;
                            break;
                        }
                    }
                }

                //复制sheet
                fileWS.Copy(missing, fileWS);

                new_ws = (Excel.Worksheet)fileWS.Next;
                new_ws.Name = oSheetName;

                return true;
            }
            catch (Exception)
            {
                String msg = StringDealUtil.PreMessage(MsgConsts.MSG_011, new String[1] { sSheetName });
                MessageBox.Show(msg);
                return false;
            }

        }
        protected bool CopyFile(String sPath,String sFileName,String oPath,String oFileName)
        {
            String sourceFileName=this.getWholePath(sPath,sFileName);
            String destFileName=this.getWholePath(oPath,oFileName);
            try
            {
                if (!this.isFileExist(sPath, sFileName))
                {                   
                    String msg = StringDealUtil.PreMessage(MsgConsts.MSG_010, new String[2] { sPath, sFileName });
                    MessageBox.Show(msg);
                    return false;
                }
                //ファイルをコピーする
                File.Copy(sourceFileName, destFileName);

                return true;
            }
            catch(Exception)
            {
                String msg = StringDealUtil.PreMessage(MsgConsts.MSG_011, new String[1] { sFileName });
                MessageBox.Show(msg);
                return false;
            }

        }
       
        protected bool isFileExist(String path, String fileName)
        {
            try
            {
                String WholeFilePath = this.getWholePath(path, fileName);

                return File.Exists(WholeFilePath);
            }
            catch (Exception)
            {
                return false;
            }

        }
        protected String getWholePath(String path, String fileName)
        {
            if (path == String.Empty || fileName == String.Empty)
            {
                return path;
            }
            if (path.EndsWith("//"))
            {
                return path + fileName;
            }
            else
            {
                return path + "//" + fileName;
            }
        }

        protected  object missing =Missing.Value;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值