c#如何读取.xls文件内容? 怎么读取呢?最好能指定单元格读取 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Xml.Xsl.XslTransform.Load(String) private void importbutton_Click(object sender, System.EventArgs e) { ConStr=ConfigurationSettings.AppSettings["ConStr"]; Conn=new SqlConnection(ConStr); string fileName=Server.MapPath("")+"\\upload"+"\\test.xls"; myFile.PostedFile.SaveAs(fileName); string extName=System.IO.Path.GetExtension(myFile.PostedFile.FileName); if(extName!=".xls"||extName.Length<1) { Response.Write( "<script language=javascript>alert('数据导入失败.可能是文件格式或路径不正确!!!');</script>"); return; } string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '"+fileName+"'"+";Extended Properties=Excel 8.0"; OleDbConnection cnnxls = new OleDbConnection (mystring); OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls); DataSet myDs =new DataSet(); myDa.Fill(myDs,"DWJBQK"); if(myDs.Tables[0].Rows.Count<=0) { Response.Write( "<script language=javascript>alert('文件中可能没有数据,请检查后重新导入!');</script>"); return; } dwid=new int[myDs.Tables[0].Rows.Count]; string strSql = ""; string CnnString="Provider=SQLOLEDB;"+ConStr; OleDbConnection conn =new OleDbConnection(CnnString); OleDbCommand myCmd =null; try { conn.Open (); for(int i=0; i<myDs.Tables[0].Rows.Count; i++) { strSql="insert into DWJBQK(DWMC,ZGBM,GDZCZE,FRDB,DWWZ,EMAIL,LXR,LXDH,DWGK) values ('"; strSql += myDs.Tables[0].Rows[i]["DWMC"].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i]["ZGBM"].ToString() + "', "; strSql += myDs.Tables[0].Rows[i]["GDZCZE"].ToString() +", '"; strSql += myDs.Tables[0].Rows[i]["FRDB"].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i]["DWWZ"].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i]["EMAIL"].ToString() +"', '"; strSql += myDs.Tables[0].Rows[i]["LXR"].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i]["LXDH"].ToString() + "', '"; strSql += myDs.Tables[0].Rows[i]["DWGK"].ToString() + "')"; strSql +="select @@IDENTITY"; try { myCmd=new OleDbCommand(strSql,conn); dwid[i]=int.Parse(myCmd.ExecuteScalar().ToString().Trim()); } catch { Response.Write( "<script language=javascript>alert('数据导入失败!');</script>"); return; } } } catch { Response.Write( "<script language=javascript>alert('数据导入失败!');</script>"); return; } finally { conn.Close(); } Response.Write("<script language=javascript>alert('操作成功.共导入"+myDs.Tables[0].Rows.Count+"条记录!');window.location.href='cddw_excel.aspx?total="+myDs.Tables[0].Rows.Count+"';</script>"); } 是xls不是xsl我在winform里面做得- - 就是读excel文件,搜一下先,应该比较多的帖子讨论这个问题的。 http://blog.csdn.net/fangxinggood/archive/2006/04/08/655313.aspx 这是一个简单读取EXCEL文件到DATAGRID中的小例子: string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ='e:\\20051215销售发票.zzp' ; Extended Properties=Excel 8.0"; OleDbConnection conn = new OleDbConnection (mystring); conn.Open(); OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",conn); DataSet myDs =new DataSet(); myDa.Fill(myDs,"xls"); this.dataGrid1.DataSource=myDs.DefaultViewManager; conn.Close(); Excel.ApplicationClass excel = new ApplicationClass(); excel.Application.Workbooks.Add(true); excel.Visible = false; string ExcelFile ="D:\\111.xls"; object missing = System.Reflection.Missing.Value; excel.Workbooks.Open(ExcelFile, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); this.textBox1.Text = ((System.Object)(((Excel.Range)(((Excel.Range)(excel.ActiveCell)).Cells)).FormulaR1C1Local)).ToString(); //FormulaR1C1Local //row1col1得值 GemBox ExcelLite Free 2.3 free edtion download from www.dnc.com.cn 用storyboard做控件的移动,必须结合Canvas吗? C# 创建文件夹。 C#读取SQL脚本中指定关键字的内容 请问各位大虾关于GetChanges方法的问题 C#中如何在dataGridView中显示不同表的记录以及如何实时更新 vs2008 发布web应用程序失败 我自己做的翻页 有个问题,很想得到答案,分不够再加 如何使一个程序在两个显示器显示不同结果? 谁有NNuit 2.0 发我一个,立即给分! c#的属性应该放在代码中的哪个位子? 会C#的肯定都能得这10分
{
ConStr=ConfigurationSettings.AppSettings["ConStr"];
Conn=new SqlConnection(ConStr); string fileName=Server.MapPath("")+"\\upload"+"\\test.xls";
myFile.PostedFile.SaveAs(fileName);
string extName=System.IO.Path.GetExtension(myFile.PostedFile.FileName);
if(extName!=".xls"||extName.Length<1)
{
Response.Write( "<script language=javascript>alert('数据导入失败.可能是文件格式或路径不正确!!!');</script>");
return;
} string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '"+fileName+"'"+";Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs,"DWJBQK");
if(myDs.Tables[0].Rows.Count<=0)
{
Response.Write( "<script language=javascript>alert('文件中可能没有数据,请检查后重新导入!');</script>");
return;
} dwid=new int[myDs.Tables[0].Rows.Count];
string strSql = "";
string CnnString="Provider=SQLOLEDB;"+ConStr;
OleDbConnection conn =new OleDbConnection(CnnString);
OleDbCommand myCmd =null; try
{
conn.Open ();
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
strSql="insert into DWJBQK(DWMC,ZGBM,GDZCZE,FRDB,DWWZ,EMAIL,LXR,LXDH,DWGK) values ('";
strSql += myDs.Tables[0].Rows[i]["DWMC"].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i]["ZGBM"].ToString() + "', "; strSql += myDs.Tables[0].Rows[i]["GDZCZE"].ToString() +", '";
strSql += myDs.Tables[0].Rows[i]["FRDB"].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i]["DWWZ"].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i]["EMAIL"].ToString() +"', '"; strSql += myDs.Tables[0].Rows[i]["LXR"].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i]["LXDH"].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i]["DWGK"].ToString() + "')"; strSql +="select @@IDENTITY"; try
{
myCmd=new OleDbCommand(strSql,conn);
dwid[i]=int.Parse(myCmd.ExecuteScalar().ToString().Trim());
}
catch
{
Response.Write( "<script language=javascript>alert('数据导入失败!');</script>");
return;
}
}
}
catch
{
Response.Write( "<script language=javascript>alert('数据导入失败!');</script>");
return;
}
finally
{
conn.Close();
}
Response.Write("<script language=javascript>alert('操作成功.共导入"+myDs.Tables[0].Rows.Count+"条记录!');window.location.href='cddw_excel.aspx?total="+myDs.Tables[0].Rows.Count+"';</script>");
}
我在winform里面做得- -
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ='e:\\20051215销售发票.zzp' ; Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection (mystring);
conn.Open();
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",conn);
DataSet myDs =new DataSet();
myDa.Fill(myDs,"xls");
this.dataGrid1.DataSource=myDs.DefaultViewManager;
conn.Close();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
string ExcelFile ="D:\\111.xls";
object missing = System.Reflection.Missing.Value;
excel.Workbooks.Open(ExcelFile, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing);
this.textBox1.Text = ((System.Object)(((Excel.Range)(((Excel.Range)(excel.ActiveCell)).Cells)).FormulaR1C1Local)).ToString();
//FormulaR1C1Local
//row1col1得值
download from www.dnc.com.cn