C# 读取EXCEL

lihuinihao6315 2012-02-22 10:03:22
EXCEL中数据 第一行为title,第二到第5行是空行,第6行是日期 年月日,第7行是表的列名,下面是表数据。
我想读取EXCEL把第7行作为列名 读取出来。请问 连接中该怎么写?
...全文
191 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
crackdung 2012-02-23
  • 打赏
  • 举报
回复
http://www.e-iceblue.com/Download/download-office-for-net-now.html
lihuinihao6315 2012-02-23
  • 打赏
  • 举报
回复
现在是这样做的,
public List<T> GetModels<T>(ExcelModel model)
{
List<T> lstT = new List<T>();
DataSet ds = new DataSet();
string connStr = string.Empty;
try
{
connStr = GetConnString(model, connStr);

using (OleDbConnection conn = new OleDbConnection(connStr))
{
//string sql = string.Format("SELECT {0} FROM [{1}$]",FormatColumns( model.Columns), model.ExcelSheetName);
string sql = string.Format("SELECT * FROM [{0}$]", model.ExcelSheetName);
OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
da.Fill(ds);

ds = FormatDataSet(ds, model.Columns);
lstT = IntoEnity<T>(ds,model.Columns);
}
}
catch (Exception ex)
{
throw ex;
}
return lstT;
}

/// <summary>
/// 将表中无用的空行删除掉,把表列名替换成需要的列名。
/// </summary>
/// <param name="ds"></param>
/// <param name="Columns"></param>
/// <returns></returns>
private DataSet FormatDataSet(DataSet ds,Dictionary<string,string> Columns)
{
if (null == ds || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;

DataSet ds1 = ds.Copy();
List<int> listIndex = new List<int>();
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{

if (ds1.Tables[0].Rows[i] == null || ds1.Tables[0].Rows[i].ItemArray.Count() < 2)
{
continue;
}

if (ds1.Tables[0].Rows[i][0] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][0].ToString())
&& ds1.Tables[0].Rows[i][1] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][1].ToString())
&& ds1.Tables[0].Rows[i][2] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][2].ToString())
&& ds1.Tables[0].Rows[i][3] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][3].ToString())
&& ds1.Tables[0].Rows[i][4] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][4].ToString())
&& ds1.Tables[0].Rows[i][5] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][5].ToString()))
{

for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
if (ds1.Tables[0].Rows[i][j] != null && !string.IsNullOrWhiteSpace(ds1.Tables[0].Rows[i][j].ToString())) //替换表列名
ds.Tables[0].Columns[j].ColumnName = ds1.Tables[0].Rows[i][j].ToString();
else
ds.Tables[0].Columns.RemoveAt(j);
}


//删除前面的空行
for (int index = listIndex.Count; index >= 0; index--)
{
ds.Tables[0].Rows.RemoveAt(index);
}
break;
}
else
{
//收集前面的空行
listIndex.Add(i);
}
}
ds1.Clear();
//删除最后一行是合计的
for (int i = ds.Tables[0].Rows.Count-1; i >=0 ; i--)
{
if (ds.Tables[0].Rows[i] != null && ds.Tables[0].Rows[i][0] != null && !string.IsNullOrWhiteSpace(ds.Tables[0].Rows[i][0].ToString()))
{
if (ds.Tables[0].Rows[i][0].ToString().Equals("合计"))
{
ds.Tables[0].Rows.RemoveAt(i);
break;
}
}
}


return ds;
}
lihuinihao6315 2012-02-23
  • 打赏
  • 举报
回复
回楼上,我现在是这么做的。。但是貌似效率不高。有没有更好的。。因为做的是公用类。。所以要考虑到多处调用。。
CSharp_Go 2012-02-22
  • 打赏
  • 举报
回复
郁闷,我也有这类要求,查百度查到这,却还木有答案。楼主找到答案也通知我一怔,好不?
437453992@qq.com
至尊贱客 2012-02-22
  • 打赏
  • 举报
回复
先给个地址你看看http://blog.csdn.net/cemer815/article/details/6320939

然后把下面的代码自己改改就可以了



public static DataSet LoadDataFromExcel(string filePath, ref string message)
{
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等

OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
message = err.Message;
//MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
// MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
梁山草寇 2012-02-22
  • 打赏
  • 举报
回复
很简单的。。。。
先声明一个int 变量。
把excel读取出来放入到一个DataSet里面。
然后循环遍历dataset,同时给int变量赋值。
当int变量等于你想要的那一行数据的时候在进行进一步的操作就可以了。

int num=0;
string url = (@Server.MapPath("~/UploadFiles/") + filename).ToString();
this.FileUpload1.PostedFile.SaveAs(@Server.MapPath("~/UploadFiles/") + filename); //得到excel路径
string mystring = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + url + " ;Extended Properties='Excel 8.0;HDR=yes;IMEX=1'");
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$] ", cnnxls);
DataSet ds = new DataSet();
myDa.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
num ++ ;
if(num ==7)//如果是第七行数据就执行相应的代码,要是还想读取第八行,第九行...就把 等于 换成大于
{
userName = ds.Tables[0].Rows[i][0].ToString();//读取第一列数据
sid=ds.Tables[0].Rows[i][1].ToString();//读取第二列数据
....
}
else
{
continue;
}
}


有不懂请联系我。

110,538

社区成员

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

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

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