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;
}
}