zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    jquery 中的 map each has
    jquery的 dom操作
    jquery的 包装集
    JQuery 的了解之 选择器
    JS 中闭包的变量 闭包与this
    IPhone下json的解析 NSJSONSerialization
    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法
    win7系统的用户去掉用户账户控制 提升管理员
    移动开发在路上-- IOS移动开发系列 多线程三
    MVC 入门 自动生成 增删改查所有功能
  • 原文地址:https://www.cnblogs.com/dclcc/p/3484135.html
Copyright © 2011-2022 走看看