zoukankan      html  css  js  c++  java
  • 导入导出Excel点滴


            
    #region 输出Excel

            
    protected void ExcelOutput(System.Web.UI.WebControls.DataGrid dg,System.Data.DataTable dt)
            {
                    Response.Clear();
                Response.Buffer
    = true;
                Response.ContentType 
    = "application/vnd.ms-excel";
                Response.Charset 
    = "gb2312";
                Response.AppendHeader(
    "Content-Disposition","attachment;filename=FXYDAKK.xls");
                Response.ContentEncoding
    =System.Text.Encoding.GetEncoding("gb2312");

                
    //this.EnableViewState = false;

                System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
                
    //
                dg.DataSource = dt;
                dg.DataBind();

                dg.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                Response.End();    
            }


            
    protected void ExcelOutput(System.Web.UI.WebControls.DataGrid dg,
                                        System.Data.DataTable dt,
                                        
    string Encoding,
                                        
    string excelfilename)
            {
                Response.Clear();
                Response.Buffer
    = true;
                Response.ContentType 
    = "application/vnd.ms-excel";
                Response.Charset 
    = Encoding;
                Response.AppendHeader(
    "Content-Disposition","attachment;filename="+excelfilename);
                Response.ContentEncoding
    =System.Text.Encoding.GetEncoding(Encoding);

                
    //this.EnableViewState = false;

                System.IO.StringWriter oStringWriter 
    = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
                
    //
                dg.DataSource = dt;
                dg.DataBind();

                dg.RenderControl(oHtmlTextWriter);
                Response.Write(oStringWriter.ToString());
                Response.End();    
            }



            
    #endregion

            
    #region 导入Excel
            
    public DataTable ExcelInput(string sourcePath,
                                        
    string SheetName)
            {

                System.Data.DataTable dt 
    = new System.Data.DataTable();

                OleDbConnection conn 
    = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+sourcePath+";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"");
                OleDbCommand cmd 
    = new OleDbCommand("SELECT * FROM ["+SheetName+"$]", conn);
                OleDbDataAdapter adapter 
    = new OleDbDataAdapter(cmd);
                DataTable table 
    = new DataTable();
                adapter.Fill(dt);
                
    return dt;
            }
            
    #endregion
  • 相关阅读:
    python学习之关于变量与内存的问题
    [题解]GDUT 2020年11月赛DE题
    【转】关于Oracle默认用户名system密码不正确登录不上解决方案
    [转载] Monitor Tools
    java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getAsyncContext()Ljavax/servlet/AsyncContext;
    【SSM】Result Maps collection already contains value for crud.dao.EmployeeMapper.BaseResultMap
    EL表达式失效,页面取不到数据
    处理回归BUG最佳实践
    固定QPS压测初试
    Java字符串到数组的转换--最后放大招
  • 原文地址:https://www.cnblogs.com/Bruce_H21/p/831794.html
Copyright © 2011-2022 走看看