C#导入Excel
来源:互联网 发布:算法分析与设计视频 编辑:程序博客网 时间:2024/06/18 12:52
一.使用OleDb,这个法子好像不大好使.容易读错.
引用System.Data.OleDb;
/**//// <summary> /// 返回Excel数据源 /// </summary> /// <param name="filename">文件路径</param> /// <returns></returns> static public DataSet ExcelToDataSet(string filename) { DataSet ds; string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + filename; OleDbConnection myConn = new OleDbConnection(strCon); string strCom = " SELECT * FROM [Sheet1$]"; myConn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); ds = new DataSet(); myCommand.Fill(ds); myConn.Close(); return ds; }
二.使用com.
导入Microsoft.Excel
使用命名空间
using Excel= Microsoft.Office.Interop.Excel;
using System.Diagnostics;
public class ExcelHelper{ private Excel._Application excelApp; private string fileName=string.Empty; private Excel.WorkbookClass wbclass; public ExcelHelper(string _filename) { excelApp = new Excel.Application(); object objOpt = System.Reflection.Missing.Value; wbclass = (Excel.WorkbookClass)excelApp.Workbooks.Open(_filename, objOpt, false, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt); } /**//// <summary> /// 所有sheet的名称列表 /// </summary> /// <returns></returns> public List<string> GetSheetNames() { List<string> list = new List<string>(); Excel.Sheets sheets = wbclass.Worksheets; string sheetNams = string.Empty; foreach (Excel.Worksheet sheet in sheets) { list.Add(sheet.Name); } return list; } public Excel.Worksheet GetWorksheetByName(string name) { Excel.Worksheet sheet=null; Excel.Sheets sheets= wbclass.Worksheets; foreach (Excel.Worksheet s in sheets) { if (s.Name == name) { sheet = s; break; } } return sheet; } /**//// <summary> /// /// </summary> /// <param name="sheetName">sheet名称</param> /// <returns></returns> public Array GetContent(string sheetName) { Excel.Worksheet sheet = GetWorksheetByName(sheetName); //获取A1 到AM24范围的单元格 Excel.Range rang = sheet.get_Range("A1", "AM24"); //读一个单元格内容 //sheet.get_Range("A1", Type.Missing); //不为空的区域,列,行数目 // int l = sheet.UsedRange.Columns.Count; // int w = sheet.UsedRange.Rows.Count; // object[,] dell = sheet.UsedRange.get_Value(Missing.Value) as object[,]; System.Array values = (Array)rang.Cells.Value2; return values; } public void Close() { excelApp.Quit(); excelApp = null; } }
- C# Excel 导入Excel 留档
- C#中EXCEL导入
- C#导入excel重写
- Excel 导入sqlserver c#
- c#导入导出Excel
- C# 导入Excel
- C# excel数据导入
- C#导入Excel重写
- C#导入Excel
- c# 导入导出excel
- c# 导入导出excel
- C#导入Excel数据
- C# datagridview 导入excel
- C#导入、导出Excel
- c# 导入导出excel
- C# 导入Excel
- C#导入导出EXCEL
- C#导入Excel
- el 表达式简单的集合输出方式
- EL表达式总结
- MFC注册热键的方法(RegisterHotKey)
- 生成互不相同随机数的两种算法
- netsh ipsec 使用方法
- C#导入Excel
- 从Nand Flash启动U-BOOT的基本原理
- page request session application的使用区别
- ECC 加密说说(二)
- System.getProperty()参数一览表
- hdu 1171 Big Event in HDU
- FlashBuilder中 Molehill项目运行时 VerifyError: Error #1014: 无法找到类 flash.display3D::Context3D。错误解决方法
- (三)U-Boot在AT91RM9200上的移植及启动分析
- 最近比较烦? 细数Android成长的烦恼