| « | December 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | |
|
| 公告 |
Back Today!
Hold on~
Come on~ |
| 统计 |
blog名称:执著 日志总数:39 评论数量:43 留言数量:0 访问次数:247560 建立时间:2005年3月4日 | |
[.net]操作excel的类 |
执著 发表于 2005/8/24 11:44:19 |
| using System;using System.Data;using System.Data.OleDb;using Excel;using System.Collections.Specialized;
namespace winapp_test{ /// <summary> /// class_excel 的摘要说明。 /// </summary> public class class_excel { private string _filename; Excel.ApplicationClass app = new ApplicationClass(); Excel.Workbook excel_wb; public class_excel(string f) { // // TODO: 在此处添加构造函数逻辑 // _filename = f; excel_wb = app.Workbooks.Open(_filename,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); }
public StringCollection countexcel() //返回工作表名 { if(System.IO.File.Exists(_filename)) { StringCollection a = new StringCollection(); for(int i = 1;i<=excel_wb.Worksheets.Count;i++) { a.Add(((Excel.Worksheet)excel_wb.Worksheets[i]).Name); } return a; } else { return null; } }
public DataSet proces() //用datset返回整个excel { string strConn = "Provider=Microsoft.Jet.OleDb.4.0;"; strConn += "data source=" + _filename + ";"; strConn += "Extended Properties=Excel 8.0;"; strConn += "HDR=Yes;IMEX=1"; OleDbConnection objConn = new OleDbConnection(strConn); DataSet ds = new DataSet(); OleDbDataAdapter oldda = new OleDbDataAdapter();
foreach(string sheetname in countexcel()) { string a = "select * from "; a += sheetname; oldda.SelectCommand.CommandText = a; oldda.Fill(ds,sheetname); } return ds; }
public int rowcount(string sheetname) //行数 { return ((Excel.Worksheet)excel_wb.Worksheets.get_Item(sheetname)).UsedRange.Rows.Count; } public int colcount(string sheetname) //列数 { return ((Excel.Worksheet)excel_wb.Worksheets.get_Item(sheetname)).UsedRange.Columns.Count; }
public string range(string sheetname,int row,int col) //返回指定单元格的文本 { Excel.Worksheet ws = (Excel.Worksheet)excel_wb.Worksheets.get_Item(sheetname); Excel.Range r = (Excel.Range)(ws.Cells[row,col]); return r.Text.ToString(); }
public void close() //关闭 { app.Workbooks.Close(); app.Quit(); app = null; } }}
自己写的,水平实在有限 | |
|
|
|
|
|