黄缘龟喜欢吃什么:C# 导出Excel 禁止单元格换行

来源:百度文库 编辑:九乡新闻网 时间:2024/05/17 00:46:48

C# 中导出Excel,要求Excel不换行,不合并单元格,导出的Excel后实现如下图所示的效果。如下图:

 步骤如下:1、将设置好格式的Excel另存为html格式。2、将stylesheet.css中的样式,放入aspx页面的头部。例如:  3、将sheet001.htm中的内容拷贝到aspx页面的中。根据需要修改aspx页面,比如:将数据绑定到Repeater控件、dataList控件或者绑定到gridView控件中。 例如:   

       
           
           
           
           
           
           
           
           
           
           
               
               
               
               
               
               
               
           
           
               
               
               
               
               
               
               
               
               
               
               
           
           
               
               
               
               
               
               
               
               
               
               
               
           
           
               
                   

                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                       
                   
               
           
           
           
               
               
               
               
               
               
               
               
               
               
               
           
           
       

                    公司名称

                   

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

                    访问日期

                    机器型号

                    客户管理号

                    使用机种

                    运转时间

                    作业区分

                    作业内容

                    其他内容

                    备注

                    负责人

                    服务费用

                           

                           

                           
                       

                           

                           
                       

                           

                           

                           
                       

                           

                           
                       

                           
                       

               

               

               

               

               

               

               

               

               

               

               

   
 4、在aspx.cs文件中的导出方法中给控件赋值后,导出样式和body的内容即可。导出Excel的后台代码如下:protected void BtnExcel_Click(object sender, EventArgs e)
        {
             //赋值
            DataSet ds = this.getDateToExcel();
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.rptService.DataSource = ds.Tables[0];
                        this.rptService.DataBind();
                    }
                }
                if (ds.Tables[1] != null)
                {
                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        lblHeadCompanyName1.Text = ds.Tables[1].Rows[0]["HEADCOMPANYNAME"].ToString();
                    }
                }
            }             StringWriter swhead = new StringWriter();
            HtmlTextWriter hwhead = new HtmlTextWriter(swhead);
            this.toExcelStyle.RenderControl(hwhead);            StringWriter swbody = new StringWriter();
            HtmlTextWriter hwbody = new HtmlTextWriter(swbody);
            this.toExcel.Visible = true;
            this.toExcel.RenderControl(hwbody);            string style = @" ";            Response.Clear();
            Response.Buffer = true;            string title = "客户服务一览_";
            CultureInfo en = new CultureInfo("en-US");
            string Excel_ShortTime = title + DateTime.Now.ToString("yyyyMMddHHmmss", en);            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Response.AppendHeader("Content-Disposition", "attachment;filename=\""+ System.Web.HttpUtility.UrlEncode(Excel_ShortTime, System.Text.Encoding.UTF8) + ".xls");           //写Excel的样式和Excel中的内容
            Response.Write(swhead.ToString()+swbody.ToString());
            Response.End();
        }