c# 连接,读取 excel单元格数据 需要用到什么命名空间,如何读取单元格数据,能不能具体的说说看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你需要在 References 点击右键 选择 Add References 在选择COM 选项卡,查找 MicroSoft Office 11.0 Object Library 添加这个组件,就可以开发了。 有一个类似的例子,经过测试可以使用,已经用在了一个小项目上!http://download.csdn.net/source/2565456 public DataSet readExcelData(string sFile) { DataSet result = null; string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sFile + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); try { conn.Open(); string strExcel = "select * from [sheet1$]"; OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn); result = new DataSet(); myCommand.Fill(ds, "table1"); } finally { conn.Close(); } return result; } string fileName; fileName = Server.MapPath( ". ")+ "\\excel\\ajcl.xls "; object o = System.Reflection.Missing.Value; Excel.Application oXL = new Excel.ApplicationClass(); //Response.Write( " <script> alert( "+fileName+ ") </script> "); Excel.Workbook oWB = oXL.Workbooks.Open(fileName,o,o,o,o,o,o,o,o,o,o,o,o); Excel.Worksheet oSheet = (Excel.Worksheet)oWB.Worksheets[1]; oXL.Visible = true; string strSql = Session[ "sql "].ToString().Trim(); SqlDataAdapter MyCommand = new SqlDataAdapter(strSql,MyConn); DataSet ds = new DataSet(); MyCommand.Fill(ds); DataTable MyTable = new DataTable(); MyTable = ds.Tables[0]; if(MyTable.Rows.Count != 0) { for (int i = 0;i <MyTable.Rows.Count;i++) { DataRow MyRow = MyTable.Rows[i]; string str_xh = MyRow[ "xh "].ToString().Trim(); oSheet.Cells[i+4,1] = MyRow[ "ajbh "].ToString().Trim(); oSheet.Cells[i+4,2] = MyRow[ "csmc "].ToString().Trim(); oSheet.Cells[i+4,3] = MyRow[ "lb "].ToString().Trim(); string str_ajly = MyRow[ "jclx "].ToString().Trim(); 由于我的excel中,还有其他的图形啊什么的,不是规则的表,所以可能用dataset是不是不太适合? Object refmissing = System.Reflection.Missing.Value; Excel._Application exc = new Excel.ApplicationClass(); exc.Visible = true; Excel.Workbooks workbooks = exc.Workbooks; workbooks._Open("E:\\edwardliu\\Timereport status Query Robot.xlsm", refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing); Excel.Worksheet sheet = (Excel.Worksheet)exc.Worksheets.get_Item(1); return sheet.Cells[3,12].ToString();以上是我写的读取单元格的代码,但是其中读出来是System.__ComObject,请问是什么原因呢? 静态方法修改主窗口控件属性 设计一个故障检测仪应采用哪样的方案 c# 怎样用代码改变listview中item的顺序 有关 GridView 用字符串绑定数据 求助? 如何实现交互式的图像处理(学习ing)? 你能将如下代码转成VB.NET吗? C#中SmtpMail.SmtpServer = 这个属性到底该填写什么呢?求救! 请教DefaultCredentials的username,password,domain问题 为什么我的EXE文件,会增大!? 如何自己写一个可以多选的下拉列表框 用AS序列化后的文件怎么用C#反序列化回来 Windows Mobile 6.0关于授权提示
{
DataSet result = null;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + sFile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = "select * from [sheet1$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
result = new DataSet();
myCommand.Fill(ds, "table1");
}
finally
{
conn.Close();
}
return result;
}
Excel.Application oXL = new Excel.ApplicationClass();
//Response.Write( " <script> alert( "+fileName+ ") </script> ");
Excel.Workbook oWB = oXL.Workbooks.Open(fileName,o,o,o,o,o,o,o,o,o,o,o,o);
Excel.Worksheet oSheet = (Excel.Worksheet)oWB.Worksheets[1];
oXL.Visible = true; string strSql = Session[ "sql "].ToString().Trim();
SqlDataAdapter MyCommand = new SqlDataAdapter(strSql,MyConn);
DataSet ds = new DataSet();
MyCommand.Fill(ds);
DataTable MyTable = new DataTable();
MyTable = ds.Tables[0];
if(MyTable.Rows.Count != 0)
{
for (int i = 0;i <MyTable.Rows.Count;i++)
{
DataRow MyRow = MyTable.Rows[i];
string str_xh = MyRow[ "xh "].ToString().Trim();
oSheet.Cells[i+4,1] = MyRow[ "ajbh "].ToString().Trim();
oSheet.Cells[i+4,2] = MyRow[ "csmc "].ToString().Trim();
oSheet.Cells[i+4,3] = MyRow[ "lb "].ToString().Trim();
string str_ajly = MyRow[ "jclx "].ToString().Trim();
Object refmissing = System.Reflection.Missing.Value;
Excel._Application exc = new Excel.ApplicationClass();
exc.Visible = true;
Excel.Workbooks workbooks = exc.Workbooks;
workbooks._Open("E:\\edwardliu\\Timereport status Query Robot.xlsm", refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing, refmissing); Excel.Worksheet sheet = (Excel.Worksheet)exc.Worksheets.get_Item(1);
return sheet.Cells[3,12].ToString();以上是我写的读取单元格的代码,但是其中读出来是System.__ComObject,请问是什么原因呢?