日期:2014-05-16  浏览次数:20506 次

asp.net(C#) 上传Execl文件 并插入数据到数据库实例
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class InserPosAll : System.Web.UI.Page
{
    public DataSet ExecleDs(string filenameurl, string table)
    {
        string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        OleDbDataAdapter odda = new OleDbDataAdapter("select * from [sheet1$]", conn);
        DataSet ds = new DataSet();
        odda.Fill(ds, table);
        return ds;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    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();
        //if (IsXls != ".xls")
        //{
        //    Response.Write("<script>alert('只可以选择Excel文件')</script>");
        //    return;//当选择的不是Excel文件时,返回       
        //}
        String serverPath = Server.MapPath("~/") + FileUpload1.FileName;
        FileUpload1.SaveAs(serverPath);
        string error = null;

        SqlConnection conn = new SqlConnection("data source=10.47.0.7;database=pos_inquire;user=sa;password=19730524");
        conn.Open();
        string Sqlstrl = " Truncate table pos_all";
        SqlCommand comd = new SqlCommand(Sqlstrl, conn);
        comd.ExecuteNonQuery();

        //string strpath = FileUpload1.PostedFile.FileName.ToString();   //获取Execle文件路径       
        string filename = FileUpload1.FileName;                       //获取Execle文件名         
        //DataSet ds = conn.ExecleDs(strpath,filename);
        DataSet ds = ExecleDs(serverPath, filename);
        DataRow[] dr = ds.Tables[0].Select();                        //定义一个DataRow数组       
        int rowsnum = ds.Tables[0].Rows.Count;
        if (rowsnum == 0)
        {
            Response.Write("<script>alert('Excel表为空表,无数据!')</script>");   //当Excel表为空时,对用户进行提示      
        }
        else
        {

            string 商户编号;
            string 装机日期;
            string 手续费率;
            string 商户名;
            string 主办行;
            string 经办人;
            string 装机方;
            string 开户行;
            string 有效性;
            string 装机台数;
            string POS类型;

            for (int i = 0; i < dr.Length; i++)
            {
                商户编号 = dr[i][0].ToString();              //string dh = dr[i]["YongHuMiMa"].ToString();        
                装机日期 = dr[i][1].ToString();
                手续费率 = dr[i][2].ToString();
                商户名 = dr[i][3].ToString();
                主办行 = dr[i][4].ToString();
                经办人 = dr[i][5].ToString();
                装机方 = dr[i][6].ToString();
                开户行 = dr[i][7].ToString();
                有效性 = dr[i][8].ToString();
                装机台数 = dr[i][9].ToString();
                POS类型 = dr[i][10].ToString();

                string insertstr = "insert into pos_all(商户编号,装机日期,手续费率,商户名,主办行,经办人,装机方,开户行,有效性,装机台数,POS类型) values('" + 商户编号 + "','" + 装机日期 + "','" + 手续费率 + "','" + 商户名 + "','" + 主办行 + "','" + 经办人 + "','" + 装机方 + "','" + 开户行 + "','" + 有效性 + "','" + 装机台数 + "','" + POS类型 + "')";
                SqlCommand cmd = new SqlCommand(insertstr, conn);