您的位置:首页 > 编程语言 > C#

C# 操作Excel ——Excel获取数据、时间、图片

2011-12-23 10:27 585 查看
最近开发项目中,涉及到通过c#,向Excel中读取数据、插入图片、读取时间等操作。经过查找网上资料,终于完成相关操作,现将代码粘贴出来:

public class ExcelControl:ConvertObject
{
//Fields
private _Workbook _objBook=null ;
private Application _objExcel=null ;
private object _objOpt=Missing.Value ;
private Range _objRange=null ;
private _Worksheet _objWorkSheet=null ;
private Sheets _objSheets=null ;
private string _savePath;//文件保存路径
private int _sheetIndex=1;//工作表的索引
private string _templatePath;//文件模板路径

//Properties
public string SavePath
{
get { return _savePath; }
set { _savePath = value; }
}

public int SheetIndex
{
get { return _sheetIndex; }
set { _sheetIndex = value; }
}
public string TemplatePath
{
get { return _templatePath; }
set { _templatePath = value; }
}

//Methods

public ExcelControl()
{
this._objExcel = new ApplicationClass();
}

public void CellsAlignment(int startRow, int startColumn, int endRow, int endColumn, XlHAlign hAlign, XlVAlign vAlign)
{
Range range = this._objWorkSheet.get_Range(this._objWorkSheet.Cells[startRow, startColumn], this._objWorkSheet.Cells[endRow, endColumn]);
range.HorizontalAlignment = hAlign;
range.VerticalAlignment = vAlign;
}

public void CellsUnite(int startRow, int startColumn, int endRow, int endColumn)
{
this._objWorkSheet.get_Range(this._objWorkSheet.Cells[startRow, startColumn], this._objWorkSheet.Cells[endRow, endColumn]).MergeCells = true;
}

/// <summary>
/// 关闭excel
/// </summary>
public void Close()
{
try
{
this._objBook.Close(_objOpt, _objOpt, _objOpt);
this._objExcel.Workbooks.Close();
this._objExcel.Quit();
Marshal.ReleaseComObject(_objBook);
Marshal.ReleaseComObject(_objExcel);
this._objBook = null;
this._objExcel = null;
GC.Collect();
}
catch(Exception ex)
{
ErrMessage = ex.Message;
}
}

/// <summary>
/// 获取excel中单元格数据
/// </summary>
/// <param name="range"></param>
/// <returns></returns>
public object GetRange(object range)
{
try
{
this._objRange = this._objWorkSheet.get_Range(range, this._objOpt);
return this._objRange.Value2;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return null;
}
}

/// <summary>
/// 向excel中插入图片
/// </summary>
/// <param name="pictureName">图片名称</param>
/// <param name="left">左边宽度</param>
/// <param name="top">顶部宽度</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
public void InsertPictures(string pictureName, int left, int top, int width, int height)
{
this._objWorkSheet.Shapes.AddPicture(pictureName, MsoTriState.msoFalse, MsoTriState.msoTrue, (float)left, (float)top, (float)width, (float)height);
}

public bool InsertRow(object rowID, object colunmID)
{
try
{
Range range = (Range)this._objWorkSheet.Cells[rowID, colunmID];
range.Select();
range.EntireRow.Insert(this._objOpt, this._objOpt);
return true;
}
catch (Exception)
{
return false;
}
}

/// <summary>
/// 打开excel
/// </summary>
/// <returns></returns>
public bool Open()
{
try
{
this._objBook = this._objExcel.Workbooks.Open(this._templatePath, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt);
this._objSheets = this._objBook.Worksheets;
return true;
}
catch(Exception e)
{
this.ErrMessage = e.Message;
return false;
}
}

/// <summary>
/// 打开模板
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool Open(string filePath)
{
this._templatePath = filePath;
try
{
this._objBook = this._objExcel.Workbooks.Open(this._templatePath, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt);
this._objSheets = this._objBook.Worksheets;
return true;
}
catch (Exception e)
{
this.ErrMessage = e.Message;
return false;
}
}

/// <summary>
/// 行自动适合宽度
/// </summary>
/// <param name="rowNum"></param>
public void RowAutoFit(int rowNum)
{
Range range = (Range)this._objWorkSheet.Rows[rowNum.ToString() + ":" + rowNum.ToString(), Type.Missing];
range.EntireColumn.AutoFit();
}

/// <summary>
/// 保存
/// </summary>
/// <returns></returns>
public bool Save()
{
try
{
this._objBook.Save();
return true;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return false;
}
}

/// <summary>
/// 另存为
/// </summary>
/// <returns></returns>
public bool SaveAs()
{
try
{
this._objBook.SaveAs(this._savePath, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, XlSaveAsAccessMode.xlNoChange, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt);
return true;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return false;
}
}
public bool SaveAs(string newPath)
{
this._savePath = newPath;
try
{
this._objBook.SaveAs(this._savePath, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt, XlSaveAsAccessMode.xlNoChange, this._objOpt, this._objOpt, this._objOpt, this._objOpt, this._objOpt);
return true;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return false;
}
}

/// <summary>
/// 工作表激活状态
/// </summary>
/// <returns></returns>
public bool SetActiveSheer()
{
try
{
this._objWorkSheet = (_Worksheet)this._objSheets.get_Item(this._sheetIndex);
return true;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return false;
}
}

/// <summary>
///
/// </summary>
/// <param name="activeIndex"></param>
/// <returns></returns>
public bool SetActiveSheer(int activeIndex)
{
this._sheetIndex = activeIndex;
try
{
this._objWorkSheet = (_Worksheet)this._objSheets.get_Item(this._sheetIndex);
return true;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
return false;
}
}

/// <summary>
/// 设置显示警告
/// </summary>
/// <param name="b"></param>
public void SetDisplayAlerts(bool b)
{
this._objExcel.DisplayAlerts = b;
}

/// <summary>
/// 向excel中插入值
/// </summary>
/// <param name="range">插入位置</param>
/// <param name="newValue">插入值</param>
public void SetRange(object range, object newValue)
{
try
{
this._objRange = this._objWorkSheet.get_Range(range, this._objOpt);
this._objRange.Value2 = newValue;
}
catch (Exception ex)
{
this.ErrMessage = ex.Message;
}
}

/// <summary>
/// 获取excel中时间格式
/// </summary>
/// <param name="strDate"></param>
/// <returns></returns>
public DateTime GetConvertDate(string strDate)
{
return DateTime.FromOADate(Convert.ToDouble(strDate));
}
}


本人能力有限,如有错误,望大家指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: