using System;
using Office;
using System.Diagnostics;
namespace YSJ.PublicClass
{
/// <summary>
/// SExcel 的摘要说明。
/// </summary>
public class SExcel
{
Excel.Application Ex = null;
Excel.Workbook ExBook = null;
object oMissing = System.Reflection.Missing.Value;
SqlDataReader Dr;
TSqlDB Db=new TSqlDB();
DateTime dtapp1;
DateTime dtapp2;
public SExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void OpenFile(string strFileName)
{
dtapp1=DateTime.Now;
Ex=new Excel.ApplicationClass();
dtapp2=DateTime.Now;
ExBook=Ex.Application.Workbooks.Open(strFileName,oMissing,oMissing,oMissing,oMissing,oMissing,
oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
Ex.DisplayAlerts=false;
}
/// <summary>
/// 设置单元格内属性
/// </summary>
/// <param name="Row">行</param>
/// <param name="Column">列</param>
/// <param name="str">单元格的值</param>
public void SetCells(int Row,int Column,string str)
{
Ex.Cells[Row,Column]=str;
}
/// <summary>
/// 设置单元格内字体
/// </summary>
/// <param name="FontSize">字体大小</param>
public void SetFont(int FontSize)
{
Ex.Cells.Font.Size=FontSize;
}
/// <summary>
/// 杀死EXCEL进程
/// </summary>
public void KillProcess()
{
foreach(Process p in Process.GetProcessesByName("EXCEL"))
{
if(p.StartTime>=dtapp1&&p.StartTime<=dtapp2)
{
p.Kill();
}
}
}
public void Save(string strSavePath)
{
Ex.Application.ActiveWorkbook.SaveAs(strSavePath,oMissing,oMissing,oMissing,oMissing,oMissing,Excel.XlSaveAsAccessMode.xlNoChange,
oMissing,oMissing,oMissing,oMissing);
ExBook.Application.ActiveWorkbook.Close(false,oMissing, false);
//ExBook.Close(oMissing,oMissing,oMissing);
ExBook=null;
Ex.Application.Quit();
Ex.Quit();
Ex=null;
//GC.Collect();
KillProcess();
}
/// <summary>
/// 插入图片
/// </summary>
/// <param name="PhotoPath">图片路径</param>
/// <param name="left">左边长</param>
/// <param name="top">上</param>
/// <param name="width">图片的宽</param>
/// <param name="height">图片的高</param>
public void InsertPhoto(string PhotoPath,float left,float top,float width,float height)
{
if (System.IO.File.Exists(PhotoPath))
{
Excel.Worksheet xSheet=(Excel.Worksheet)ExBook.ActiveSheet;
//xSheet.Shapes.AddPicture(PhotoPath, MsoTriState.msoFalse ,MsoTriState.msoTrue,165,190,75,30);
xSheet.Shapes.AddPicture(PhotoPath, MsoTriState.msoFalse ,MsoTriState.msoTrue,left,top,width,height);
}
}
/// <summary>
/// 建立新工作簿
/// </summary>
/// <param name="SheetName">工作簿名称</param>
public void AddSheet(string SheetName)
{
Excel.Worksheet xSt;
xSt=(Excel.Worksheet)ExBook.Sheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
xSt.Name=SheetName;
}
}
}