Maison  >  Article  >  base de données  >  GridView读取数据XLS ACCESS MDB 并写入到数据库

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

WBOY
WBOYoriginal
2016-06-07 15:37:391225parcourir

%@ 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()+","); 
             //}
         }
     }
 }

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn