页面生成word

lnit728 2010-06-23 11:03:07

我有个合同信息的页面如图:
想在想要生成word文档,并下载下来,请问怎么做,最好给出具体实例,越详细越好,我在百度搜了几个,都没有成功。菜鸟,请说详细点,或者给我个可以参考的东西
...全文
153 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
dyrxm 2010-07-16
  • 打赏
  • 举报
回复
同样的问题啊
lnit728 2010-06-25
  • 打赏
  • 举报
回复
直接在word的做的书签,然后向书签里写数据,然后另存为
object missingValue = System.Reflection.Missing.Value;
object myTrue = false;
object fileName = Server.MapPath("ContDownload/model.doc");

Word._Application oWord = new Word.ApplicationClass();
Word._Document oDoc;

oDoc = oWord.Documents.Open(ref fileName, ref missingValue,
ref myTrue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);

//写入word文档
object tmp1 = "cno";
object tmp2 = "prname";
Word.Range tmpRng1 = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp1).Range;
Word.Range tmpRng2 = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp2).Range;
tmpRng1.Text = txtCno.Value;
tmpRng2.Text = txtPrname.Value;
//由于附值以后书签自动消除,为了以后便于修改,需要把书签再自动生成一个

object oRng1 = tmpRng1;
object oRng2 = tmpRng2;
oDoc.Bookmarks.Add(tmp1.ToString(), ref oRng1);
oDoc.Bookmarks.Add(tmp2.ToString(), ref oRng2);
object filename1 = Server.MapPath("ContDownload/" + txtPrname.Value + ".doc");
oDoc.SaveAs(ref filename1, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue, ref missingValue);
object savechange = true;
oDoc.Close(ref savechange, ref missingValue, ref missingValue);
长腿爸爸 2010-06-23
  • 打赏
  • 举报
回复
在Word里创建一个表格吧,向单元格插入对应数据。

C# word控件编程


// * 文档中创建表格

Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
//设置表格样式
newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
newTable.Columns[1].Width = 100f;
newTable.Columns[2].Width = 220f;
newTable.Columns[3].Width = 105f;

//填充表格内容
newTable.Cell(1, 1).Range.Text = "产品详细信息表";
newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体

//合并单元格
newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;// 垂直居中
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;// 水平居中

//填充表格内容
newTable.Cell(2, 1).Range.Text = "产品基本信息";
newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
//合并单元格
newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//填充表格内容
newTable.Cell(3, 1).Range.Text = "品牌名称:";
newTable.Cell(3, 2).Range.Text = "BrandName";
//纵向合并单元格
newTable.Cell(3, 3).Select();//选中一行
object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object moveCount = 5;
object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
WordApp.Selection.Cells.Merge();

//插入图片
string FileName = System.Windows.Forms.Application.StartupPath + "\\rjkf.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
//将图片设置为四周环绕型
Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

newTable.Cell(12, 1).Range.Text = "产品特殊属性";
newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
//在表格中增加行
WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

lnit728 2010-06-23
  • 打赏
  • 举报
回复
我试了下,如果是简单的写好的table,可以生产word,但是,我里面有<input type="text"/> 当我向里面写了东西的时候再执行,就会报下面的错误:
只能在执行 Render() 的过程中调用 RegisterForEventValidation;
灵雨飘零 2010-06-23
  • 打赏
  • 举报
回复
前台页面:

<DIV runat="server" id="tablePb" style="width: 100%;">

///
...合同内容
///

</DIV>


后台代码:



//重载方法
public override void VerifyRenderingInServerForm(Control control)
{

}

protected void btnToExcel_Click(object sender, EventArgs e)
{
TableToWord tableToWord = new TableToWord(Response);
tableToWord.DataToWord("合同信息—" + DateTime.Now.ToString("yyyy-MM-dd") + ".doc", tablePb);
}
灵雨飘零 2010-06-23
  • 打赏
  • 举报
回复
把页面内容放到一个DIV中,然后导出到Word中。




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

namespace Web.Common
{
public class TableToWord
{
HttpResponse Response = null;
public TableToWord(HttpResponse response)
{
Response = response;
}

public void DataToWord(string FileName, HtmlTable tableName)//Word输出
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename= 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-word";
//this.EnableViewState = false;
// 定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
tableName.RenderControl(oHtmlTextWriter);
//this 表示输出本页,tableName表示表格的名字,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}
}
}

别样苍茫 2010-06-23
  • 打赏
  • 举报
回复
没写过这么复杂的,关注
lnit728 2010-06-23
  • 打赏
  • 举报
回复
我用2楼的办法,虽然能导出word了,但是对于控件的文本不支持,出来的是都红X图片。如果只是文本就可以了。
5楼的办法,我没试,如果要做,对于我要写的表格,是不是太麻烦了点……

6楼word模板应该可以,不过我不会做标签……正在学习中
le616 2010-06-23
  • 打赏
  • 举报
回复
学习哈!
wuyq11 2010-06-23
  • 打赏
  • 举报
回复
wuyq11 2010-06-23
  • 打赏
  • 举报
回复
设置word模板,设置书签,赋值到书签
或自动添加 word

62,046

社区成员

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

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

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

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