c#开发的Excel类

本文介绍了一个用于操作Excel文件的C#类,包括打开、读取和关闭工作簿等功能。该类通过COM对象实现与Excel的交互,支持指定工作表名称或索引来获取工作表。
using System; using System.Diagnostics; namespace Prolink.EHU.WebGui.classes { /// /// ExcelOperation 的摘要说明。 /// public class ExcelOperation { private object missing = System.Reflection.Missing.Value; // 在引用excel时,有些参数为空,就用它替换 private Excel.Application excel = new Excel.ApplicationClass(); // 引用excel组件 private Excel.Workbook workbook = null; // 工作薄 private Excel.Worksheet worksheet = null; // 工作表 /// /// 初始化,并打开工作薄和工作表 /// /// 文件名包含路径 /// 字符型--工作表 public ExcelOperation(string fileName) { // // TODO: 在此处添加构造函数逻辑 excel.Visible = false; excel.DisplayAlerts = false; workbook = excel.Workbooks.Open(fileName, missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); } /// /// 得到工作表 /// /// /// public void GetWorkSheet(string worksheetName) { worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(worksheetName); } /// /// 得到工作表 /// /// /// public void GetWorkSheet(int worksheetIndex) { worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(worksheetIndex); } public void Close() { workbook.Close(false, null, null); //退出Excel,并且释放调用的COM资源 excel.Quit(); // // System.Runtime.InteropServices.Marshal.ReleaseComObject(Range); // Range = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); worksheet=null; workbook=null; excel=null; GC.Collect(); } /// /// 取得工作表名的集合 /// /// public string[] GetSheetNames() { string[] names = new string[workbook.Worksheets.Count]; for (int i = 0; i < names.Length; i++) { Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(i+1); names[i] = worksheet.Name; } return names; } /// /// 根据行号和列号取值 /// /// 如:B6 /// public object GetCellValue(string position) { object obj = null; try { obj = worksheet.get_Range(position, missing).Value2; } catch(Exception exp) { throw exp; } return obj; } /// /// 取出范围内的单元格值 /// /// /// /// public object[] GetCellRangeValue(string fromPosition, string toPosition) { string[] ranges = GetRange(fromPosition, toPosition); object[] obj = new object[ranges.Length]; for (int i = 0; i < obj.Length; i++) { obj[i] = GetCellValue(ranges[i]); } return obj; } /// /// 根据excel中的两点,取出这两点范围的位置。如"A1,A2",以逗号分隔 /// /// /// /// private string[] GetRange(string fromPosition, string toPosition) { int[] _formPosition = GetPosition(fromPosition); int[] _toPosition = GetPosition(toPosition); if (_formPosition[0] > _toPosition[0]) { int temp = _toPosition[0]; _toPosition[0] = _formPosition[0]; _formPosition[0] = temp; } if (_formPosition[1] > _toPosition[1]) { int temp = _toPosition[1]; _toPosition[1] = _formPosition[1]; _formPosition[1] = temp; } string result = ""; for (int i = _formPosition[0]; i <= _toPosition[0]; i++) { for (int j = _formPosition[1]; j <= _toPosition[1]; j++) { result += ReplaceNumber(i) + j.ToString() + ","; } } if (result.EndsWith(",")) result = result.Substring(0, result.Length - 1); return result.Split(','); } /// /// 根据行数和列数(列数为字母,并且行号和列号放在一起)取出对应的数字数组 /// /// /// private int[] GetPosition(string str) { char[] chars = str.ToUpper().ToCharArray(); int[] position = new int[2]; string column = ""; string row = ""; for (int i = 0; i < chars.Length; i++) { if ('A' <= chars[i] && chars[i] <= 'Z') column += chars[i].ToString(); if ('0' <= chars[i] && chars[i] <= '9') row += chars[i].ToString(); } position[0] = ReplaceString(column); position[1] = int.Parse(row); return position; } /// /// 把在excel中的字母列转换成相应的数字 /// /// /// private string ReplaceNumber(int index) { int i = index; int j = 0; if (index > 26) { i = index % 26; j = index / 26; } if (j == 0) return ((char)((int)'A' + i - 1)).ToString(); else return ((char)((int)'A' + j - 1)).ToString() + ((char)((int)'A' + i - 1)).ToString(); } /// /// 与ReplaceNumber相反 /// /// /// private int ReplaceString(string str) { str = str.ToUpper(); char[] chars = str.ToCharArray(); int index = 0; if (chars.Length == 1) index = (int)chars[0] - (int)'A' + 1; else if (chars.Length == 2) { index = (int)chars[1] - (int)'A' + 1; index += ((int)chars[0] - (int)'A' + 1) * 26; } return index; } } } 转至:http://www.bhcode.net/article/20080504/924.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值