导入导出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
    }
 
  

 

 

转载于:https://www.cnblogs.com/gaochun413/archive/2012/09/28/2706754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值