將網頁內容轉換成word或excel文檔的方法(c#.net)

將網頁內容轉換成word或excel文檔的方法(c#.net)

最近看到好多網友關於使用c#.net實現網頁內容轉換成word或excel時時常出現亂碼的問題,現將如何轉換做個詳細的介紹。
1.轉換方法:
        一般用HTTP的Header,在header裏設置幾個關鍵字讓IE知道這是什麼類型,從而正確打開。
2.C#源碼:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace word
{
    
/// <summary>
    
/// htm2doc 的摘要說明。
    
/// </summary>

    public class htm2doc : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Button Button1;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{        
            
// 在此處放置用戶代碼以初始化頁面
        }

        
public void ExpertControl(System.Web.UI.Control source, DocumentType type)

        
{
            
//設置Http的頭信息,編碼格式
            if (type == DocumentType.Excel)
            
{
                
//Excel
                Response.AppendHeader("Content-Disposition","attachment;filename=result.xls");
                Response.ContentType 
= "application/ms-excel";
            }

            
else if (type == DocumentType.Word)
            
{
                
//Word
                Response.AppendHeader("Content-Disposition","attachment;filename=result.doc");
                Response.ContentType 
= "application/ms-word";
            }

            Response.Charset 
= "utf-8";   
            Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312");
            
//關閉控件的視圖狀態
            source.Page.EnableViewState =false;   
            
//初始化HtmlWriter
            System.IO.StringWriter writer = new System.IO.StringWriter() ;
            System.Web.UI.HtmlTextWriter htmlWriter 
= new System.Web.UI.HtmlTextWriter(writer);
            source.RenderControl(htmlWriter);
            
//輸出
            Response.Write(writer.ToString());
            Response.End();
        }

        
//文檔類型枚舉
        public enum DocumentType
        
{
            Word,
            Excel
        }

        
#region Web 窗體設計器生成的代碼
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/// <summary>
        
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
        
/// 此方法的內容。
        
/// </summary>

        private void InitializeComponent()
        
{    
            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion
           //在web窗體中添加一個按鈕使用該方法
        
private void Button1_Click(object sender, System.EventArgs e)
        
{
                ExpertControl(
this, DocumentType.Word);
        }

    }

}

3.注意點:
        在上面的代碼中關鍵的地方就是以下兩行代碼:

     Response.Charset = "utf-8";   
     Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312");
發佈了18 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章