首页 > 代码库 > 读取 excel 2003,2007 资料

读取 excel 2003,2007 资料

    protected void Page_Load(object sender, EventArgs e)    {        //直接在bin add referece search Microsoft.Office.Interop.Excel.dll (.net 4.5 version 好像是是 1.4.0.0.0)        string filePath = Request.MapPath(@"~\excel\someData.xlsx"); //excel文件路径(server path)        string sheetName = "Sheet1"; //文件中,哪一个sheet        string connectionString07 = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=‘Excel 12.0;HDR=YES‘", filePath);        //string connectionString03 = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0", filePath);                       string query = String.Format("select * from [{0}$]", sheetName);         OleDbConnection connection = new OleDbConnection(connectionString07);        OleDbDataAdapter adapter = new OleDbDataAdapter(query, connectionString07);        DataSet dataSet = new DataSet();        adapter.Fill(dataSet);        DataTable table = dataSet.Tables[0];            }

注意 : 如果读取后要insert to SQL, 要注意格式和字的长度,把最长的放最上面,微软默认会依据前面8 rows来决定类型和长度等等。(如果遇到问题可以参考)

 

以后才研究导出excel

读取 excel 2003,2007 资料