• Excel报表


    Excel报表

    1.Excel报表导入到GridView

    复制代码
     protected void Page_Load(object sender, EventArgs e)
            {
                string path = Server.MapPath("~/Book1.xls");
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0";Data Source=" + path);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();
    
    
                string path = Server.MapPath("~/Book1.xls");
                this.GridView1.DataSource = GetExcel(path);
                this.GridView1.DataBind();
            }
            public DataSet GetExcel(string filepath)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES;IMEX=1";Data Source=" + filepath);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
    
                string sql = string.Empty;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sql += string.Format("insert into class values({0},'{1}','{2}','{3}')",ds.Tables[0].Rows[i][0].ToString(),ds.Tables[0].Rows[i][1].ToString(),ds.Tables[0].Rows[i][2].ToString(),ds.Tables[0].Rows[i][3].ToString());
                    
                }
               
            }
    复制代码

    2.Excel数据导入到数据库

    复制代码
     protected void Page_Load(object sender, EventArgs e)
            {
                string path = Server.MapPath("~/Book1.xls");
                DataSet ds = GetExcel(path);
                string sql = string.Empty;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sql += string.Format("insert into class values({0},'{1}','{2}','{3}')", ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][2].ToString(), ds.Tables[0].Rows[i][3].ToString());
    
                }
                SqlHelper.ExecuteNonQuery(sql);
            }
    
           
            public DataSet GetExcel(string filepath)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties="Excel 8.0;HDR=YES;IMEX=1";Data Source=" + filepath);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
  • 相关阅读:
    【转】PHP中文乱码的常见解决方法总结(GOOD)
    【转】Apache的安装与配置
    【转】PHP Notice: Undefined index: ... 问题的解决方法
    【转】方程的解数(哈希表的应用)
    【转】PHP Notice: undefined index 完美解决方法
    【转】如何修改IIS的默认端口号
    Apache配置(转载)
    BZOJ 1014 Splay+hash
    BZOJ 1045 环形均分纸牌
    BZOJ 1008 快速幂
  • 原文地址:https://www.cnblogs.com/yangshuaigg/p/3484549.html
Copyright © 2020-2023  润新知