C#操作Excel(读取)

一.使用OleDb,这个法子好像不大好使.容易读错.
引用System.Data.OleDb;

ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif        
/// 返回Excel数据源
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="filename">文件路径</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif      static   public  DataSet ExcelToDataSet( string  filename)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        DataSet ds;
InBlock.gif        
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
InBlock.gif                        
"Extended Properties=Excel 8.0;" +
InBlock.gif                        
"data source=" + filename;
InBlock.gif        OleDbConnection myConn 
= new OleDbConnection(strCon);
InBlock.gif        
string strCom = " SELECT * FROM [Sheet1$]";
InBlock.gif        myConn.Open();
InBlock.gif        OleDbDataAdapter myCommand 
= new OleDbDataAdapter(strCom, myConn);
InBlock.gif        ds 
= new DataSet();
InBlock.gif        myCommand.Fill(ds);
InBlock.gif        myConn.Close();
InBlock.gif        
return ds;
ExpandedBlockEnd.gif    }

二.使用com.
导入Microsoft.Excel
使用命名空间
using Excel= Microsoft.Office.Interop.Excel;
using System.Diagnostics;
None.gif public    class  ExcelHelper
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
private Excel._Application excelApp;
InBlock.gif      
private string fileName=string.Empty;
InBlock.gif      
private Excel.WorkbookClass wbclass;
InBlock.gif      
public ExcelHelper(string _filename)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          excelApp 
= new Excel.Application();
InBlock.gif          
object   objOpt   =   System.Reflection.Missing.Value;
InBlock.gif          wbclass 
= (Excel.WorkbookClass)excelApp.Workbooks.Open(_filename, objOpt, false, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// 所有sheet的名称列表
InBlock.gif      
/// </summary>
ExpandedSubBlockEnd.gif      
/// <returns></returns>

InBlock.gif      public List<string> GetSheetNames()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          List
<string> list = new List<string>();
InBlock.gif          Excel.Sheets sheets 
= wbclass.Worksheets;
InBlock.gif          
string sheetNams = string.Empty;
InBlock.gif          
foreach (Excel.Worksheet sheet in sheets)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            list.Add(sheet.Name);
ExpandedSubBlockEnd.gif          }

InBlock.gif          
return list;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
public Excel.Worksheet GetWorksheetByName(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          Excel.Worksheet sheet
=null;
InBlock.gif          Excel.Sheets sheets
= wbclass.Worksheets;
InBlock.gif          
foreach (Excel.Worksheet s in sheets)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif              
if (s.Name == name)
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif                  sheet 
= s;
InBlock.gif                  
break;
ExpandedSubBlockEnd.gif              }

ExpandedSubBlockEnd.gif          }

InBlock.gif          
return sheet;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// 
InBlock.gif      
/// </summary>
InBlock.gif      
/// <param name="sheetName">sheet名称</param>
ExpandedSubBlockEnd.gif      
/// <returns></returns>

InBlock.gif      public Array GetContent(string sheetName)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          Excel.Worksheet sheet 
= GetWorksheetByName(sheetName);
InBlock.gif          
//获取A1 到AM24范围的单元格
InBlock.gif
          Excel.Range rang = sheet.get_Range("A1""AM24");
InBlock.gif          
//读一个单元格内容
InBlock.gif          
//sheet.get_Range("A1", Type.Missing);
InBlock.gif        
//不为空的区域,列,行数目
InBlock.gif       
//   int l = sheet.UsedRange.Columns.Count;
InBlock.gif         
// int w = sheet.UsedRange.Rows.Count;
InBlock.gif        
//  object[,] dell = sheet.UsedRange.get_Value(Missing.Value) as object[,];
InBlock.gif
          System.Array values = (Array)rang.Cells.Value2;
InBlock.gif          
return values;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public void Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif          excelApp.Quit();
InBlock.gif          excelApp 
= null;
ExpandedSubBlockEnd.gif      }

InBlock.gif   
ExpandedBlockEnd.gif    }

其他链接:
创建、打开、读取、写入、保存的一般性代码
告别ASP.NET操作EXCEL的烦恼(总结篇)

转载于:https://www.cnblogs.com/solo/archive/2008/04/14/1152802.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值