GridView读取数据XLS ACCESS MDB 并写入到数据库

WBOY
發布: 2016-06-07 15:37:39
原創
1225 人瀏覽過

%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel_CS.aspx.cs" Inherits="ReadExcel_CS" % !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http



    无标题页


   

   

       
       
            

         
           
            

   

   



-------------------------------------

   using System;
 using System.Data;
 using System.Configuration;
 using System.Collections;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using System.Data.OleDb;
 
 public partial class ReadExcel_CS : System.Web.UI.Page
{
     public DataTable DT;
     protected void Page_Load(object sender, EventArgs e)
     {
         //read excel file
         string strConn;
         strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
         "Data Source=C://test.xls;" +
         "Extended Properties=Excel 8.0;";
 
         OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
 
         DataSet myDataSet = new DataSet();
         myCommand.Fill(myDataSet, "ExcelInfo");
         this.GridView1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
         this.GridView1.DataBind();

          DT = (DataTable)myDataSet.Tables["ExcelInfo"];
         /*
         //read access file
         string strConn2;
         strConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;" +
         "Data Source=c://test.mdb;" +
         "Persist Security Info=True;";
 
         OleDbDataAdapter myCommand2 = new OleDbDataAdapter("select * from test", strConn2);
 
         DataSet myDataSet2 = new DataSet();
         myCommand2.Fill(myDataSet2, "AccessInfo");
         this.GridView2.DataSource = myDataSet2.Tables["AccessInfo"].DefaultView;
         this.GridView2.DataBind();
          */
 
     }
     protected void Button1_Click(object sender, EventArgs e)
     {
         /*
         //写入数据库david
         for (int i = 1; i          {
             for (int j = 1; j              {
                 string a = GridView1.Rows[i].Cells[j].ToString() ;
                 Response.Write(a.ToString() ); 
             }
            
         }
          */
         for (int i = 0; i          {
             //for (int j = 0; j              //{
                 string A=DT.Rows[i][0].ToString();
                 string B= DT.Rows[i][1].ToString();
                 string C = DT.Rows[i][2].ToString();
                 string D = DT.Rows[i][3].ToString();

                 string sqlinsert = "insert TEST(A,B,C,D)values('"+A+"','"+B+"','"+C+"','"+D+"')";
                 DataAccess.DataAccessSHDB.GetNonQuery(sqlinsert.ToString());  
                 //Response.Write(a.ToString()+","); 
             //}
         }
     }
 }

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!