c#.net 将网页内容转换成word或excel文档

本文介绍了一种使用C#从网页内容生成Word或Excel文件的方法,重点解决了编码问题导致的乱码现象。通过设置HTTP头部信息及编码格式,确保了导出文件的正确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近研究了一下网页生成Word的方法,结果看到好多网友出现了关于使用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
        }
        Web 窗体设计器生成的代码#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");        第一行代码设定浏览器所使用的编码为utf-8,第二行代码设定的是word和excel的编码,因为word和excel的编码一般使用的是gb2312编码,不一定和浏览器(一般默认utf-8)的一致,这也是许多网友问题所在!

转载于:https://www.cnblogs.com/291099657/archive/2009/08/18/1549039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值