如何操作Excel

該例子實現了:將上傳的Excel文件內容在頁面上顯示出來,或者加入到數據庫.

Default.aspx

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3.     <title>Untitled Page</title>
  4. </head>
  5. <body>
  6.     <form id="form1" runat="server">
  7.         <div>
  8.             <asp:GridView ID="GridView1" runat="server" Height="177px" Width="911px">
  9.             </asp:GridView>
  10.             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="读数据" />
  11.             <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="导入数据库" />
  12.             <asp:FileUpload ID="FileUpload1" runat="server" /></div>
  13.     </form>
  14. </body>
  15. </html>

 

Default.aspx.cs

  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.Text;
  11. using System.IO;
  12. using System.Data.OleDb;
  13. public partial class _Default : System.Web.UI.Page 
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.     }
  18.     protected void Button1_Click(object sender, EventArgs e)
  19.     {
  20.         GridView1.DataSource = CreateDataSource();
  21.         GridView1.DataBind();
  22.     }
  23.     protected void Button2_Click(object sender, EventArgs e)
  24.     {
  25.         DataSet ds = CreateDataSource();
  26.         int intCount = ds.Tables[0].Rows.Count;
  27.         string[] ArrayList = new string[intCount];
  28.         // string intcountcel=ds.Tables[0].Rows[0].ToString();
  29.         // Response.Write(intcountcel);
  30.         string strVal = "insert into a(aa,bb,cc,dd,ee,ff) values (";
  31.         for (int i = 0; i < intCount; i++)
  32.         {
  33.             string str_a = ds.Tables[0].Rows[i][0].ToString();
  34.             string str_b = ds.Tables[0].Rows[i][1].ToString();
  35.             string str_c = ds.Tables[0].Rows[i][2].ToString();
  36.             string str_d = ds.Tables[0].Rows[i][3].ToString();
  37.             string str_e = ds.Tables[0].Rows[i][4].ToString();
  38.             ArrayList[i] = strVal + str_a + "," + str_b + "," + str_c + "," + str_d + "," + str_e + ")";
  39.         }
  40.         for (int i = 0; i < ArrayList.Length; i++)
  41.         {
  42.             Response.Write(ArrayList[i].ToString());
  43.             Response.Write("<br>");
  44.         }
  45.     }
  46.     private DataSet CreateDataSource()
  47.     {
  48.         string strCon;
  49.         string excel = string.Empty;
  50.         string Adress = FileUpload1.PostedFile.FileName.ToString();
  51.         int intPos = Adress.LastIndexOf("//");
  52.         string strFileName = Adress.Substring(intPos + 1);
  53.         string strSavePath = HttpContext.Current.Server.MapPath("~/Sounce") + "/" + strFileName;
  54.         if (!File.Exists(strSavePath))
  55.         {
  56.             FileUpload1.PostedFile.SaveAs(strSavePath);
  57.         }
  58.         strCon = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=Excel 8.0;", strSavePath);
  59.         OleDbConnection olecon = new OleDbConnection(strCon);
  60.         OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
  61.         DataSet myds = new DataSet();
  62.         myda.Fill(myds);
  63.         return myds;
  64.     }
  65. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值