欢迎来到天天文库
浏览记录
ID:40533181
大小:23.00 KB
页数:4页
时间:2019-08-04
《C#Excel操作类》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingExcel=Microsoft.Office.Interop.Excel;usingSystem.Diagnostics;usingSystem.Windows.Forms;namespaceOfficeManager{publicclassExcelClass:IDisposable{privateExcel._Applicationm_ExcelApplicati
2、on=null;privateExcel._Workbookm_Workbook=null;publicExcel._Worksheetm_Worksheet=null;privateobjectmissing=System.Reflection.Missing.Value;publicExcelClass(){if(m_ExcelApplication==null){m_ExcelApplication=newExcel.ApplicationClass();}}~ExcelClass(){tr
3、y{if(m_ExcelApplication!=null)m_ExcelApplication.Quit();}catch(Exceptionex){Debug.Write(ex.ToString());}}//////获取或设置当前工作表///publicintCurrentWorksheetIndex{set{if(value<=0
4、
5、value>m_Workbook.Worksheets.Count)thrownewException("索引超出范围"
6、);else{objectindex=value;m_Worksheet=m_Workbook.Worksheets[index]asExcel._Worksheet;}}}//////打开一个Excel工作薄//////publicvoidOpenWorkbook(stringfileName){m_Workbook=m_ExcelApplication.Workbooks.Open(fileNam
7、e,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing);if(m_Workbook.Worksheets.Count>0){objectindex=1;m_Worksheet=m_Workbook.Worksheets[index]asExcel._Worksheet;}}//////保存数据///8、mary>publicvoidSave(){if(m_Workbook!=null){m_Workbook.Save();}}//////关闭文档//////publicvoidClose(boolisSave){this.ClearClipboard();objectobj_Save=isSave;if(m_Workbook!=null)m_Workbook.Close(obj_Save,missing9、,missing);}//////设置当前工作表中某单元格的值/////////publicvoidSetCellValue(stringcellIndex,objectvalue){if(m_Worksheet!=null){objectcell1=cellIndex;Excel.Rangerange=m_Worksheet.get_Range10、(cell1,missing);if(range!=null){range.Value2=value;}}}//////合并单元格/////////publicvoidMerge(stringcellIndex1,stringcellIndex2){if(m_Worksheet!=null){objectcell1=c
8、mary>publicvoidSave(){if(m_Workbook!=null){m_Workbook.Save();}}//////关闭文档//////publicvoidClose(boolisSave){this.ClearClipboard();objectobj_Save=isSave;if(m_Workbook!=null)m_Workbook.Close(obj_Save,missing
9、,missing);}//////设置当前工作表中某单元格的值/////////publicvoidSetCellValue(stringcellIndex,objectvalue){if(m_Worksheet!=null){objectcell1=cellIndex;Excel.Rangerange=m_Worksheet.get_Range
10、(cell1,missing);if(range!=null){range.Value2=value;}}}//////合并单元格/////////publicvoidMerge(stringcellIndex1,stringcellIndex2){if(m_Worksheet!=null){objectcell1=c
此文档下载收益归作者所有