有关获取Excel的sheet名和获取excel数据的问题

 
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.OleDb;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.     }
  16.     protected void Button1_Click(object sender, EventArgs e)
  17.     {
  18.         
  19.         string fileName = "ToOwn_On.xls";
  20.         string filePath = Server.MapPath("~/Excel/") + fileName;
  21.         Excel.Application myExcel = new Excel.ApplicationClass();
  22.         object oMissing = System.Reflection.Missing.Value;
  23.         myExcel.Application.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
  24.         Excel.Workbook myBook = myExcel.Workbooks[1];
  25.         Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1];
  26.         Response.Write(mySheet.Name);
  27.         string strConnection = string.Format("Provider = Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source ={0}", filePath);
  28.         OleDbConnection conn = new OleDbConnection(strConnection);
  29.         OleDbCommand com = new OleDbCommand("SELECT * FROM [ExcuteResult$]", conn);
  30.         OleDbDataAdapter da = new OleDbDataAdapter(com);
  31.         DataSet ds = new DataSet();
  32.         da.Fill(ds);
  33.         GridView1.DataSource = ds.Tables[0].DefaultView;
  34.         GridView1.DataBind(); 
  35.     }
  36. }

你可能感兴趣的:(Excel)