C#:excel匯入匯出

來源:互聯網
上載者:User

標籤:c#   excelservice   報表匯入匯出   

 

資源:excelService 服務 http://download.csdn.net/detail/istend/8060501

 

 

 

  • 排列問題

匯出時,數字和字元的排列格式預設不一樣,數字靠右,字元靠左,想讓排列一致,有兩個辦法:

1 在綁定時,就做固定格式

2 匯出時,將數字那一列轉換為文本格式,如下

 

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)  {              if (e.Row.RowType == DataControlRowType.DataRow)              {  //讓數字以文本形式表示                   e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");  }  }  
  • 轉換函式
        /// <summary>        /// 匯出按鈕        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnExport_Click(object sender, System.Web.UI.ImageClickEventArgs e)        {            //匯出檔案名稱            string strFileName = "報表.xls";            //取未經處理資料,並綁定            this.dagInfo.AllowPaging = false;            this.dagInfo.AllowSorting = false;            DataTable dt = BingData();            this.dagInfo.DataSource = dt;            this.dagInfo.DataBind();            //使用本頁面直接輸出            WHPT_WebApp.Statistics.GridStyleReport.ToExcel(this.dagInfo                , strFileName,                delegate(System.Web.UI.HtmlTextWriter writer)                {                    writer.Write(string.Format("<table width=100%><tr><td align=center colspan=5>{0}</td></tr></table>", "報表"));                }                , null);            //恢複分頁資料            this.dagInfo.AllowPaging = true;            this.dagInfo.AllowSorting = true;            BindInfo(1, "");            return;        }        #endregion


        /// <summary>        /// 轉換成EXCEL        /// </summary>        /// <param name="ctl"></param>        /// <param name="file_name"></param>        public static void ToExcel(System.Web.UI.Control ctl, string file_name,RenderCallBack beforeRende,RenderCallBack afterRender)        {            //將控制項資料匯出成Excel檔案            if (string.IsNullOrEmpty(file_name))                return;            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file_name, System.Text.Encoding.UTF8));            HttpContext.Current.Response.Charset = "GB2312"; //"UTF-8";            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;            HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword            ctl.Page.EnableViewState = true;            System.IO.StringWriter tw = new System.IO.StringWriter();            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);            if (beforeRende != null)                beforeRende(hw);            ctl.RenderControl(hw);            if (afterRender != null)                afterRender(hw);            HttpContext.Current.Response.Write(GetHtmlHeadText(tw.ToString()));            HttpContext.Current.Response.End();        }


 

PS:此文僅做記錄交流所用,不做他用


C#:excel匯入匯出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.