asp.net页面如何导出为Word文档?

我是菜鸟!!! 2009-03-23 04:18:42
如题,最好有例子,谢谢大家啦。
...全文
4928 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
·九丶哥· 2011-11-02
  • 打赏
  • 举报
回复
跟着学习的
qingxiangy 2011-10-09
  • 打赏
  • 举报
回复
跟着学习的
ylx1989 2010-08-20
  • 打赏
  • 举报
回复
好东西,学习了!
sinian415 2010-03-17
  • 打赏
  • 举报
回复
好帖!
honlrlin 2009-03-27
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 sunfeiwto 的回复:]
引用 3 楼 honlrlin 的回复:
C# code
/**//// <summary>
/// 将Web控件导出
/// </summary>
/// <param name="source">控件实例 </param>
/// <param name="type">类型:Excel或Word </param>
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType…
[/Quote]


你看会会第一二项被WORD弄成标题之类的
walkghost 2009-03-25
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sunfeiwto 的回复:]
引用楼主 sunfeiwto 的帖子:
如题,最好有例子,谢谢大家啦。

是 页面如何导出为Word文档?
[/Quote]
页面放到一个pannel里不就OK了?
artwl_cn 2009-03-25
  • 打赏
  • 举报
回复
我是来学习的!
wen_eric 2009-03-25
  • 打赏
  • 举报
回复
ding
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 honlrlin 的回复:]
C# code
/**//// <summary>
/// 将Web控件导出
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="type">类型:Excel或Word</param>
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{

[/Quote]



asp.net 把整个页面导出Word 文档, 共有10项内容。结果只导出了8项,怎么解决啊?
XMMXBB 2009-03-25
  • 打赏
  • 举报
回复
学习
指间、魔法师 2009-03-24
  • 打赏
  • 举报
回复
顶一个。。
Chris_thanks 2009-03-24
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20081226/17/f2e96acd-191e-4d75-a898-1cfa938bad91.html
bj890 2009-03-24
  • 打赏
  • 举报
回复
http://blog.csdn.net/lovexiaoxiao/archive/2008/12/26/3613400.aspx
javacaspnet 2009-03-24
  • 打赏
  • 举报
回复
private string GetRequestString( int timeout, int enterType, Encoding EnCodeType)

{
string strResult;
try

{


string url = HttpContext.Current.Request.Url.AbsoluteUri;
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(url);
myReq.Timeout = timeout;
//myReq.Method="POST";
myReq.ContentType = "application/x-www-form-urlencoded";
//Stream myStream = myReq.GetRequestStream();
WebResponse HttpWResp = myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream() ;
StreamReader sr = new StreamReader(myStream , EnCodeType);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())

{
strBuilder.Append(sr.ReadLine());
if(enterType==1)
{
strBuilder.Append("\r\n");
}
}
strResult = strBuilder.ToString();
}
catch(Exception ex)

{
strResult = "请求错误:" + ex.Message;
}

return strResult ;
}



//调用此方法导出为word文档,传入参数为this.Page
public void getWordFile(System.Web.UI.Page Page)
{

HttpResponse resp=Page.Response;
resp.Clear();
resp.Buffer= true;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AddHeader("Content-disposition","attachment; filename="+HttpUtility.UrlEncode("1.doc",Encoding.UTF8 ));
string context=GetRequestString(8000,1,Encoding.GetEncoding("GB2312"));//调用GetRequestString方法获取指定远程网页内容
resp.ContentType="application/msword";
resp.Write(context);
resp.End();
}
  • 打赏
  • 举报
回复
[Quote=引用楼主 sunfeiwto 的帖子:]
如题,最好有例子,谢谢大家啦。
[/Quote]
是 页面如何导出为Word文档?
walkghost 2009-03-24
  • 打赏
  • 举报
回复
完整导出excel的代码,导出word的话,只需修改application/ms-word即可:



//This is the Print code as below.
protected void Button4_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("中文", System.Text.Encoding.UTF8) + ".xls");//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8

// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Panel1.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream.
//this.GridView1.RenderControl(oHtmlTextWriter);//将gridview输出,你也可以类似的写TextBox1.RenderControl...
Response.Output.Write(oStringWriter.ToString());//Output the stream.
Response.Flush();
Response.End();
}
//End of the Print data code.

//重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}


出处:
http://hi.baidu.com/dangzhang/blog/item/2e8ca86e6156badf81cb4a95.html
walkghost 2009-03-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 honlrlin 的回复:]
C# code
/**//// <summary>
/// 将Web控件导出
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="type">类型:Excel或Word</param>
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{

[/Quote]

你试试这个方法能不能解决那个必须放在runat=server的错误:


//重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}


  • 打赏
  • 举报
回复
要是office 2007 怎么办呢?
zzxap 2009-03-24
  • 打赏
  • 举报
回复
http://hi.baidu.com/timy68/blog/item/0e1c81caf06cc485c91768ce.html
在网络上看到很多关于ASP.NET导出DOC文档的例子,有的干脆就直接将html页面不做任何处理直接导出为DOC文件,但是那样会有很多错误,例如将

某些控件显示为图片。我还曾经见过微软为中国某个大公司制作的一个XX系统,导出的DOC文件实际上是某种特殊格式的XML,但是对于这个技术我还

不是很了解。于是我在网络上收集资料,找到很多种实现方法,一一实验,最后总结出以下经验。

一、首先配置系统环境:

1、在命令行(work 的sdk命令行提示)中输入:dcomcnfg,会显示出“组件服务”管理器
2、打开“组件服务-》计算机-》我的电脑-》DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性”
3、在“属性”对话框中单击“安全”选项卡,在“启动和激活权限”处选择“自定义”,再单击右边的”编辑“,在弹出的对话框中添加”ASPNET
“(在IIS6中是NETWORD SERVICE)用户,给予”本地启动“和”本地激活“的权限,单击”确定“,关闭”组件服务“管理器。
这样就能在Asp.net页面中访问Word对象了。

二、修改WEB.CONFIG,在<system.web>区段内加入:<identity impersonate="true"/>

三、添加引用:《网站》——>《添加引用》——>《COM》——Microsoft Word 11.0 Object Library

四、添加命名空间引用,
using Microsoft.Office.Interop.Word;

五:代码
Object Nothing = System.Reflection.Missing.Value;
//取得Word文件保存路径
object filename = "c:\aa.doc";
//创建一个名为WordApp的组件对象
Microsoft.Office.Interop.Word.Application WordApp = new ApplicationClass();
//网络上有写代码中直接使用Word.Application WordApp = new ApplicationClass(); 但我在实际编写中总是出现错误。
//创建一个名为WordDoc的文档对象
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//增加一表格
Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
//在表格第一单元格中添加自定义的文字内容
table.Cell(1, 1).Range.Text = "lllll";
//在文档空白地方添加文字内容
WordDoc.Paragraphs.Last.Range.Text = "Wellcome To Aspxcn.Com";
//将WordDoc文档对象的内容保存为DOC文档
WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭WordDoc文档对象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭WordApp组件对象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

zzxap 2009-03-24
  • 打赏
  • 举报
回复
[code=HTML]

ASP.NET导出WORD
<%@ Page language="c#" Src="datagrid2.aspx.cs" AutoEventWireup="false" Inherits="DataGrid_import_WordExcel.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link rel="stylesheet" href="Style.css" type="text/css">
<body>
<form width="120%" id="Form1" method="post" runat="server">
<table width="120%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle HorizontalAlign="center" Height="20"></ItemStyle>
<Columns>

</Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button id="BtnImportWord" runat="server" Text="轉到WORD"></asp:button></P>
</form>
</body>
</HTML>
-------------------
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;
using System.IO;
using System.Data.SqlClient ;
using System.Text;
using System.Configuration;

namespace DataGrid_import_WordExcel
{

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public DataRow dr;
private DataSet myDS =new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
Data_Load();
}
#region Web 敦极扢數?汜傖腔測鎢
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ExportDataGrid(string FileType, string FileName) //植DataGrid絳堤
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Data_Load()
{

SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlCommand cmd=new SqlCommand("page2",myConnection);
cmd.CommandType=CommandType.StoredProcedure;
.
.
.
myConnection.Open();

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
DataView CreateDataSource()
{
string nowDSN=ConfigurationSettings.AppSettings["data"];
SqlConnection myConnection=new SqlConnection(nowDSN);

SqlCommand cmd=new SqlCommand("page2",myConnection);
cmd.CommandType=CommandType.StoredProcedure;
.
.
.
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
return ds.Tables["pur"].DefaultView;
myConnection.Close();
Page.DataBind();
}

void DataBind()
{
DataView source=CreateDataSource();
if(!IsPostBack)
{
}
DataGrid1.DataSource = source;
DataGrid1.DataBind();
}
private void BtnImportWord_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-word", "Word.doc");
}
}
}
[/CODE]
加载更多回复(8)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧