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)); } }
本人能力有限,如有错误,望大家指正。
相关文章推荐
- C# 操作Excel ——Excel获取数据、时间、图片
- C# 操作Excel获取数据、时间、图片
- .net杂记 (调用EXE 获取路径 target VB DIV C# Split 页面之间传递值 更改session时间 EXCEL操作 控制电子邮件Mail input记录值的窍门)
- C# 操作Excel进程与获取Excel表格数据
- C#操作Excel数据导入和dataGridView重绘
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- c# OleDb操作Access时间类型:操作符丢失,或者提示错误“标准表达式中数据类型不匹配
- C#操作excel(读取excel里的图片并show在页面上)
- C# 获取Excel列数据类型不一致的数据
- C# 几十万级数据导出Excel,及Excel各种操作
- C# 获取Excel中数据
- 如何通过 Visual C# 使 Excel 自动执行操作以使用数组来填充或获取某区域中的数据
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel
- C#操作Excel数据增删改查示例
- C#操作Excel数据增删改查示例
- C#中对Excel的数据操作.
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- c#操作Excel和把Dataset数据写入Excel
- C# 操作数据导出到Excel遇到的一个小问题
- C#获取键盘和鼠标操作的时间的类