如何用OleDb读取Excel文件里面的指定Sheet的内容.

allanli 2006-09-26 04:53:48
好久没发帖了,已经在VB版发了,在这不再重述,能解决的一并给分,一万多可用分没用武之地,呵呵.

http://community.csdn.net/Expert/topic/5049/5049065.xml?temp=.6038782
...全文
390 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
火麒噬日 2006-10-16
  • 打赏
  • 举报
回复
mark
kbxj406 2006-09-28
  • 打赏
  • 举报
回复
lz,最近是采用了愚翁的方法吗???

说下
changlongbaobao 2006-09-27
  • 打赏
  • 举报
回复
c#讨论群:30781666 欢迎有经验的高手加入
thoughter 2006-09-27
  • 打赏
  • 举报
回复
抱歉,后面的没仔细看
thoughter 2006-09-27
  • 打赏
  • 举报
回复
DataTable tn = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tn的"TABLE_NAME"列可以得到表名(已包含"$")

用tn.Rows[1]["TABLE_NAME"]做为表名,你看行不行(这句没试验,直接打的,可能有错)
allanli 2006-09-27
  • 打赏
  • 举报
回复
唉,看来都是要用Excel对象了,因为我必须要按照实际顺序打开Excel各个表格啊!!!
kbxj406 2006-09-27
  • 打赏
  • 举报
回复
Mark

俺的方法对你来说是不行
Knight94 2006-09-27
  • 打赏
  • 举报
回复
to 我现在的问题是不知道Sheet的名称下,我要获取Excel中第二个Sheet的数据内容,那怎么知道第二个Sheet的名称是什么呢???

像你所说的,本来就没有什么太好的方法,毕竟计算机没那么智能。

方法一:修改Sheet的名字,减少这种判断;

方法二:逐个Sheet试着去读,或者判断某个单独的cell,来决定Sheet是否为你要读的。
allanli 2006-09-26
  • 打赏
  • 举报
回复
大家还是没有看明白我的意思.

给定一个Sheet的名称我当然会怎么把他的数据读取出来,这个我已经在帖子上给出代码了.

我现在的问题是不知道Sheet的名称下,我要获取Excel中第二个Sheet的数据内容,那怎么知道第二个Sheet的名称是什么呢???

To Small__Wolf
你的使用Excel.ApplicationClass对象的方法是可以实现我的要求.但我在帖子已经说明我不想使用Excel的COM对象了.

To kbxj406(羽儿)
你的方法只能在事先知道Excel文件中Sheet的名字才能读取数据.为什么呢?因为GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null)返回的表的顺序和Excel文件中实际的Sheet的顺序不是一一对应的,不信你可以试一下新建一个Excel文件,默认下返回的是"Sheet1","Sheet2","Sheet3".
但如果你把Excel文件里面的Sheet的顺序调乱一下,它返回的还是"Sheet1","Sheet2","Sheet3".我发现GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null)返回的表名是经过排序的.但我希望返回的顺序和Excel文件里面的顺序要一样.
kbxj406 2006-09-26
  • 打赏
  • 举报
回复
用GetExcelSheets()函数可以得到excel表中有数据的sheet,然后把sheet列到dropdownlist控件中供选择。

用ReadODBC(string SheetName)方法来读取特定sheet内的数据

Small__Wolf 2006-09-26
  • 打赏
  • 举报
回复
//获取Execl中Sheet名字
private static ArrayList GetExecl(ref ArrayList ary,string Url)
{
Object refmissing = System.Reflection.Missing.Value;
Excel._Application exc = new Excel.ApplicationClass();
exc.Visible = false;
Excel.Workbooks workbooks = exc.Workbooks;
workbooks._Open(Url,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing,refmissing);
for(int i=0;i<exc.Worksheets.Count;i++)
{
Excel.Worksheet sheet = (Excel.Worksheet)exc.Worksheets.get_Item(i+1);
ary.Add(sheet.Name.ToString());
}
exc.Application.Quit();
return ary;
}
kbxj406 2006-09-26
  • 打赏
  • 举报
回复
以上代码完全可用

前几天需要用,做的
kbxj406 2006-09-26
  • 打赏
  • 举报
回复
给你一个完整的类:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Test
{
/// <summary>
/// ExcelOperator 的摘要说明。
/// </summary>
public class ExcelOperator
{

#region 私有成员

/// <summary>
/// 要操作的Excel文件名称
/// </summary>
private string m_ExcelFileName;



#region 数据库相关

/// <summary>
/// 链接对象
/// </summary>
private System.Data.OleDb.OleDbConnection m_Conn = null;
/// <summary>
/// 提取数据命令对象
/// </summary>
private System.Data.OleDb.OleDbCommand m_CommSelect = null;
/// <summary>
/// 更新数据命令对象
/// </summary>
private System.Data.OleDb.OleDbCommand m_CommUpdate = null;
/// <summary>
/// 数据适配器对象
/// </summary>
private System.Data.OleDb.OleDbDataAdapter m_da = null;



#endregion

#endregion

#region 属性
public string ExcelFileName
{
get{return this.m_ExcelFileName;}

set
{
this.m_ExcelFileName = value;
this.m_Conn.ConnectionString = GetConnectionString(value);

}
}
#endregion

#region 构造函数

/// <summary>
/// 带参数的构造
/// </summary>
/// <param name="FileName">Excel文件名</param>
public ExcelOperator(string FileName)
{
this.m_ExcelFileName = FileName;

///构造数据库对象
///
this.m_Conn = new OleDbConnection(GetConnectionString(FileName));

this.m_CommSelect = new OleDbCommand();
this.m_CommUpdate = new OleDbCommand();

this.m_CommSelect.Connection = this.m_Conn;
this.m_CommUpdate.Connection = this.m_Conn;

this.m_da = new OleDbDataAdapter();

this.m_da.SelectCommand = this.m_CommSelect;
this.m_da.UpdateCommand = this.m_CommUpdate;
}

/// <summary>
/// 不带参数的构造
/// </summary>
public ExcelOperator()
{
///构造数据库对象
///
this.m_Conn = new OleDbConnection();

this.m_CommSelect = new OleDbCommand();
this.m_CommUpdate = new OleDbCommand();

this.m_CommSelect.Connection = this.m_Conn;
this.m_CommUpdate.Connection = this.m_Conn;

this.m_da = new OleDbDataAdapter();

this.m_da.SelectCommand = this.m_CommSelect;
this.m_da.UpdateCommand = this.m_CommUpdate;

}

#endregion


#region 方法

#region 私有方法

/// <summary>
/// 根据EXCEL文件名,返回连接字符串
/// </summary>
/// <param name="FileName">Excel文件名</param>
/// <returns>连接字符串</returns>
private static string GetConnectionString(string FileName)
{
return
@"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=" + FileName + ";" +
@"Extended Properties=Excel 8.0"/* + Convert.ToChar(34).ToString() +
@"Excel 8.0;"+ "Imex=2;HDR=No;" + Convert.ToChar(34).ToString()*/;


}
#endregion

#region 公有方法

/// <summary>
/// 返回Excel表中的Sheets
/// </summary>
/// <returns>Sheets的名称</returns>
public string[] GetExcelSheets()
{

System.Data.DataTable dt = null;

string[] res = null;
try
{
//打开数据库链接
this.m_Conn.Open();

//将Sheets获取到表中
dt = this.m_Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);


if(null != dt)
{
res = new string[dt.Rows.Count];

for(int i = 0;i<dt.Rows.Count;i++)
{
res[i] = dt.Rows[i]["TABLE_NAME"].ToString();
res[i] = res[i].Substring(0,res[i].Length-1);
}

this.m_Conn.Close();


}
else
{
res = null;
}
}
catch(System.Data.OleDb.OleDbException e)
{


this.m_Conn.Close();

MessageBox.Show("获取Excel文件工作表失败",e.Message);
return null;

}

return res;
}
#endregion

public DataSet ReadODBC(string SheetName)
{
this.m_CommSelect.CommandText = @"SELECT * FROM ["+SheetName+"$]";
DataSet myDataSet = new DataSet();
try
{
this.m_da.Fill(myDataSet);
return myDataSet;
}
catch(System.Data.ConstraintException exp)
{
MessageBox.Show(exp.Message);
return null;
}
catch(System.Data.OleDb.OleDbException exp)
{
MessageBox.Show(exp.Message);
return null;
}
catch(System.Runtime.InteropServices.COMException exp)
{
MessageBox.Show(exp.Message);
return null;
}
catch(Exception exp)
{
MessageBox.Show(exp.Message);
return null;
}
}

#endregion
}
}
Small__Wolf 2006-09-26
  • 打赏
  • 举报
回复
//参考
http://dev.csdn.net/develop/article/15/15544.shtm
Small__Wolf 2006-09-26
  • 打赏
  • 举报
回复
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Excel所在的路径;Extended Properties=Excel 8.0;";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM ["+指定的Sheet名字+"$]", strConn);
DataSet myDataSet= new DataSet();
myCommand.Fill(myDataSet,"table1");

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧