黑马程序员技术交流社区
标题:
怎样读取excel单元格数据
[打印本页]
作者:
李伟斌
时间:
2012-12-4 17:25
标题:
怎样读取excel单元格数据
本帖最后由 李伟斌 于 2012-12-5 12:55 编辑
string table0 = textBox1.Text.Trim();
//string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+table0+";Extended Properties=Excel 8.0";
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + table0 + ";Extended Properties=\"Excel 8.0; HDR = No;IMEX=1\"" ;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [sheet1$] ";
myConn.Open();
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
//创建一个 DataSet对象
//myDataSet = new DataSet();
DataSet myDataSet = new DataSet();
//得到自己的DataSet对象
myCommand.Fill(myDataSet, "[sheet1$]");
//数据绑定
string adf1=myDataSet .Tables ["sheet1$"].Rows[2][5].ToString();
这样读取不了数据,为什么?
作者:
生活墨墨
时间:
2012-12-5 09:09
不知道你引用了excel的组件没有
作者:
生活墨墨
时间:
2012-12-5 09:15
引用了组件还要引用命名空间,这是我的一段代码,你看看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Runtime.InteropServices;
namespace ITSERVICE
{
public partial class 向excel传值 : System.Web.UI.Page
{
// Excel object references.
private Excel.Application m_objExcel = null;
private Excel.Workbooks m_objBooks = null;
private Excel._Workbook m_objBook = null;
private Excel.Sheets m_objSheets = null;
private Excel._Worksheet m_objSheet = null;
private Excel.Range m_objRange = null;
private Excel.Font m_objFont = null;
private Excel.QueryTables m_objQryTables = null;
private Excel._QueryTable m_objQryTable = null;
private object m_objOpt = Missing.Value;
private string m_strSampleFolder;
protected void Page_Load(object sender, EventArgs e)
{
string filename = @"C:\Users\dell\Desktop\EuOrderReport.xls";
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objExcel.Workbooks.Open(filename);
// Add data to cells in the first worksheet in the new workbook.
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
m_objRange = m_objSheet.get_Range("A1", m_objOpt);
m_objRange.Value = "Last Name";
m_objRange = m_objSheet.get_Range("B1", m_objOpt);
m_objRange.Value = "First Name";
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange.Value = "Doe";
m_objRange = m_objSheet.get_Range("B2", m_objOpt);
m_objRange.Value = "John";
// Apply bold to cells A1:B1.
m_objRange = m_objSheet.get_Range("A1", "B1");
m_objFont = m_objRange.Font;
m_objFont.Bold = true;
// Save the Workbook and quit Excel.
m_objBook.SaveAs(@"C:\Users\dell\Desktop\Books1.xls", m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
}
}
}
复制代码
作者:
刘-洋
时间:
2012-12-5 10:16
//连接字符串
string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DatabasePath + ";Extended Properties=Excel 8.0";
//查询Sheet1表中的所有数据
string SelectStr = "SELECT * FROM [Sheet1$]";
//建立连接
OleDbConnection myconn = new OleDbConnection(ConnString);
OleDbDataAdapter da = new OleDbDataAdapter(SelectStr, myconn);
//填充数据集
DataSet ds = new DataSet();
// 将 Excel 的[Sheet1]表内容填充到 DataSet 对象
da.Fill(ds);
// 数据绑定
string ss = ds.Tables[0].Rows[b].ItemArray[0].ToString();
return ss;
复制代码
作者:
李伟斌
时间:
2012-12-5 12:54
{:soso__12581814603535396430_3:} 明白了,谢谢大家了
作者:
yuchen208
时间:
2012-12-6 15:08
这个不是太理解,还是使用jxl自己写好
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2