zoukankan      html  css  js  c++  java
  • 不依赖OFFICE组件实现带图片的EXCEL导出

        现在很多的虚拟主机提供商都不提供OWC或者OFFICE,这使得我们不能通过操作Excel或者OWC的方式来生成带有饼或柱图的EXCEL文件。下面说两种可以不依赖OFFICE组件或DLL的导出方法,权当抛砖引玉:

        1.使用Response.Write直接导出EXCEL


         这种方式网上介绍的非常多,但大部分都是导出纯表格的,介绍导出带图片的不多。其实方法很简单,就是将图片放在Gridview里,然后导出整个Gridview就可以了,导出按钮代码如下:

      protected void Button1_Click(object sender, EventArgs e)
        
    {
            System.IO.StringWriter SW 
    = new System.IO.StringWriter();
            
    //添加图片
            GridViewRow dviRow = new GridViewRow(55, DataControlRowType.Footer, DataControlRowState.Normal);//这里设置导出位置
            TableCell tcCell = new TableCell();
            Image img 
    = new Image();//新建一个图片控件
            
    // img.ImageUrl = "http://localhost:1141/ExcelImage/3.GIF";//这里添上要导出的图片。
            string strPath = "http://" + Request.ServerVariables["server_name"+ Request.ServerVariables["script_name"].ToLower().Trim();
            strPath 
    = strPath.Replace("testintvsoffer.aspx""Graphs/"+ "Demo.gif";
            tcCell.Controls.Add(img);
            dviRow.Cells.Add(tcCell);
            
    this.GridView1.Controls[0].Controls.Add(dviRow);
            
    //以下为导出Excel(代码源于网上)
            System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
            
    this.GridView1.RenderControl(HTW);
            
    //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
            Response.Buffer = true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType 
    = "application/ms-excel";
            
    //Response.ContentType是输出流的 HTTP MIME 类型
            
    //Response.ContentType     --- word文件
            
    //application/vnd.ms-excel --- excel文件
            
    //
            Response.Charset = "utf-8";
            Response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("utf-8");
            Response.AddHeader(
    "Content-Disposition""attachment;filename=XXX.xls");
            
    //attachment --- 作为附件下载
            
    //inline --- 在线打开
            
    //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
            
    //进行进行编码,以解决文件名乱码的问题
            Response.Write(SW.ToString());
            Response.Flush();
            Response.Close();
        }


    上面代码会报一个异常,只需要重载VerifyRenderingInServerForm将方法清空就可以啦~

    2.使用ReportViewer导出EXCEL

    利用Rdlc报表和ReportViewer可以非常好的实现EXCEL和PDF的导出功能而不需要OFFICE组件的支持,而且由于RDLC设计器本身的优势,设计起来也十分方便。



    注:第一种方法主要的问题就是报表图片的生成~~不知众高手一般用什么类库来做,请赐教~~

  • 相关阅读:
    e621. Activating a Keystroke When Any Child Component Has Focus
    e587. Filling Basic Shapes
    e591. Drawing Simple Text
    e595. Drawing an Image
    e586. Drawing Simple Shapes
    e636. Listening to All Key Events Before Delivery to Focused Component
    在 PL/SQL 块的哪部分可以对初始变量赋予新值? (选择1项)
    Oracle数据库中,在SQL语句中连接字符串的方法是哪个?(选择1项)
    你判断下面语句,有什么作用?(单选)
    Oracle数据库表空间与数据文件的关系描述正确的是( )
  • 原文地址:https://www.cnblogs.com/Magicsky/p/810274.html
Copyright © 2011-2022 走看看