C#操作Excel(读取)
一.使用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;
}
/// 返回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;
}
}
{
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;
}
}
其他链接:
创建、打开、读取、写入、保存的一般性代码
告别ASP.NET操作EXCEL的烦恼(总结篇)