using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
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;public partial class huayan2_EXCEL1112 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
        public DataSet ReadExcel(string strPath)//从EXCEL文件中取数据到数据集DataSet中
    {
        //查询语句
        string strExcel = "select * from [sheet1$]";
        OleDbDataAdapter objCommand = null;
        DataSet objDs = null;
        //连接
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;";
        try
        {
            //创建连接对象
            OleDbConnection objConn = new OleDbConnection(strConn);
            //打开连接
            objConn.Open();
            //创建读取对象
            objCommand = new OleDbDataAdapter(strExcel, strConn);
            //创建数据集
            objDs = new DataSet();
            //添充数据集
            objCommand.Fill(objDs, "table1");
            objConn.Close();
        }
        catch (Exception objex)
        {
            //清空数据集合
            objDs = null;
            //写异常
        }
        return objDs;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == false)
        {
            Response.Write("<script>alert('请您选择Excel文件')</script> ");
            return;//当无文件时,返回
        }
        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//文件名变为字符串变为小写,判断上传文件的类型system.IO.path.getextention()
        if (IsXls != ".xls")
        {
            Response.Write("<script>alert('只可以选择Excel文件')</script>");
            return;//当选择的不是Excel文件时,返回
        }
        string strpath = FileUpload1.PostedFile.FileName.ToString();//        DataSet data = ReadExcel(strpath);
        DataTable dt=data.Tables["table1"];
      
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string ResponseSQL = "";
            string ch = "'" + dt.Rows[i][0].ToString() + "'";
            string fzb = "'" + dt.Rows[i][1].ToString() + "'";
            string caf = "'" + dt.Rows[i][2].ToString() + "'";
            string mgf = "'" + dt.Rows[i][3].ToString() + "'";
            string rq = "'" + dt.Rows[i][4].ToString() + "'";
        //如何判断记录是否已经存在,如果存在则更新数据
                ResponseSQL = string.Format("select  *  from ex_qm_djzyt  where 槽号={0} and 化验日期={1}", ch, rq);//sql查询 判断有无重复记录,重复记录则更新!
                
                  string connString;
                    connString = "server=192.168.1.10;initial catalog=UFDATA_2008;user id=sa;password=123";
                    SqlConnection conn = new SqlConnection(connString);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(ResponseSQL, conn);                cmd.CommandType = CommandType.Text;
             // Response.Write(ResponseSQL);
                    cmd.CommandText = ResponseSQL;
                 SqlDataReader itemexist=cmd.ExecuteReader();//executenoquery()返回受影响的行数         
                               if ( itemexist.HasRows)                
               
                    {
                        itemexist.Close();
                     string   ResponseSQL2 = string.Format("update ex_qm_djzyt    set     分子比={0},CaF2={1},MgF2={2} where 槽号={3} and 化验日期={4}",  fzb, caf,mgf,ch,rq);//sql语句插入                
                        SqlCommand cmd2 = new SqlCommand(ResponseSQL2, conn);                        //cmd2.CommandType = CommandType.Text;
                        //Response.Write(ResponseSQL);
                       // cmd2.CommandText = ResponseSQL;
                     int r1=  cmd2.ExecuteNonQuery();
                     Response.Write(r1);
                       cmd.Dispose();
                       cmd2.Dispose();
                       conn.Close();
                    }
                    else
                {
                    itemexist.Close();
                        string ResponseSQL1 = string.Format("insert into ex_qm_djzyt(槽号,分子比,CaF2,MgF2,化验日期) values({0},{1},{2},{3},{4})", ch, fzb, caf, mgf,rq);//sql语句插入                
                        SqlCommand cmd1 = new SqlCommand(ResponseSQL1, conn);                      cmd1.CommandType = CommandType.Text;
                    Response.Write(ResponseSQL1);
               cmd1.CommandText = ResponseSQL1;
                       int r2=  cmd1.ExecuteNonQuery();
                       Response.Write(r2);
                         cmd.Dispose();
                         cmd1.Dispose();
                         conn.Close();
                    }
           
                     
            }
            
           // Execute(ResponseSQL);
        Response.Write("<script>alert('Excel文件导入成功!')</script> ");
        }
       
    }错误信息如下:Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error: 
Line 90: 
Line 91:         DataSet data = ReadExcel(strpath);
Line 92:         DataTable dt=data.Tables["table1"];Line 93:       
Line 94:         for (int i = 0; i < dt.Rows.Count; i++)