• 导入导出xls数据


    
    
    /// <summary>
        /// 导出为Word
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="objControl"></param>
        protected void OutExportWord(string filename, Control objControl)
        {
            #region
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/msword";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename) + ".doc");
            objControl.EnableViewState = false;
            CultureInfo cult = new CultureInfo("zh-CN", true);
            StringWriter sw = new StringWriter(cult);
            HtmlTextWriter writer = new HtmlTextWriter(sw);
            objControl.RenderControl(writer);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
    
            #endregion
        }
    
    
    
        /// <summary>
        /// 导入xls数据
        /// </summary>
        /// <param name="filename">导入的Xls文件名</param>
        /// <returns></returns>
        public DataTable ExportXls(string filename)
        {
            #region
            OleDbConnection dbf_conn = null;
            DataTable dt = null;
            string sqlMaster = "";
            try
            {
                string oleDBConnString = String.Empty;
                oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
                oleDBConnString += "Data Source=";
                oleDBConnString += Server.HtmlDecode(filename);
                oleDBConnString += ";Extended Properties='Excel 8.0;IMEX=1;'";
    
                dbf_conn = new OleDbConnection(oleDBConnString);
                dbf_conn.Open();
                dt = new DataTable();
                // dt = null;
                dt = dbf_conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            }
            catch
            {
    
                Alert("无法连接到数据源!");
                return null;
            }
            try
            {
                sqlMaster = " SELECT * FROM [Sheet1$]";//指定某列开始读取:Sheet1$B1:B5
                OleDbDataAdapter odbcAdMaster = new OleDbDataAdapter(sqlMaster, dbf_conn);
                odbcAdMaster.Fill(dt);
                return dt;
            }
            catch
            {
                Alert("Excel工作表名称必须为Sheet1,或者删除Excel多余的列!");
                return null;
            }
    
            #endregion
        }
        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="filename"></param>
        /// <Muser>AllenCheng</Muser>
        protected void OutExportXls(DataTable dt, string filename)
        {
            #region
            if (dt != null)
            {
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                HttpContext.Current.Response.ContentType = "application/ms-excel/msword";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename) + ".xls");
                DataGrid dg = new DataGrid();
                dg.DataSource = dt;
                dg.AllowPaging = false;
                dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(dg_ItemDataBound1);
                dg.DataBind();
                CultureInfo cult = new CultureInfo("zh-CN", true);
                StringWriter sw = new StringWriter(cult);
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                writer.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\">");
                writer.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=GB2312\">");
                dg.RenderControl(writer);
                dg.Dispose();
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
    
            }
            #endregion
        }
    
    
    
     
  • 相关阅读:
    为什么switch里的case没有break不行
    CLS的探索:Python如何让日志免费云化
    做一次“黑客“,入侵一次自己的服务器
    斥资288买了三年服务器之后应该如何配置
    Scrapy入门到放弃01:开启爬虫2.0时代
    c#自制抽奖小程序
    c#中的几种Dialog
    解决数据库排序空值排在前问题
    Oracle 数据库添加定时事件
    FileReader 对象实现图片预览
  • 原文地址:https://www.cnblogs.com/gaochun413/p/2706754.html
Copyright © 2020-2023  润新知