求导出Word文档【分不是问题】 那位大哥大姐做过类似功能的 最好提供一段简单的源代码 谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用gridview和<asp:table>都能做类似的导出,自己BAIDU下就有了。 Work 本身是能打开HTML的....... 把哪的导出啊,可以用ntko在线编辑word 替换word模板中的书签就行了,得到的wordapplication对象,可以直接保存为word文档。#region 导出到word object missing = System.Reflection.Missing.Value; Word.ApplicationClass oWordApp = new Word.ApplicationClass(); object oTemplate = Server.MapPath("~/WordTemplate/ruku.doc"); Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing); oWordDoc.Activate();//激活word文档操作 try { DataTable dtAnjian = mySocut.GetDataSet("select * from anjian where anjianid=(select anjianid from ruku where id=" + anjianID + ")", 0, 1, "anjian").Tables[0]; object oBookMark = "CaseID";//替换模板里CaseID的书签,案件编号 oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjianid"].ToString(); oBookMark = "CaseName";//替换模板里CaseName的书签,案件名称 oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjianname"].ToString(); oBookMark = "CaseTime";//替换模板里CaseTime的书签,案发时间 oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anfadate"].ToString(); oBookMark = "CasePlace";//替换模板里CasePlace的书签,案发地点 oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anfadz"].ToString(); oBookMark = "CaseDescription";//替换模板里CasePlace的书签,案件简介 oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); #region 循环物证表,生成表格 DataTable dt = GetTable(ids); if (dt.Rows.Count > 0) { oBookMark = "CaseTable";//替换模板里CaseTable的书签,证物列表 Word.Table WordTable = oWordDoc.Books.get_Item(ref oBookMark).Range.Tables[1]; for (int i = 0; i < dt.Rows.Count; i++) { Word.Row row = WordTable.Rows[2]; DataRow dr = dt.Rows[i]; row.Cells[1].Range.Text = dr["wuzhengname"].ToString(); row.Cells[2].Range.Text = dr["wuzhengID"].ToString(); row.Cells[3].Range.Text = dr["tiqubw"].ToString(); row.Cells[4].Range.Text = dr["tiquren"].ToString(); row.Cells[5].Range.Text = dr["cunfangwzid"].ToString(); object newRow = row; WordTable.Rows.Add(ref newRow); } // 添加标题 Word.Row rowTitle = WordTable.Rows[1]; rowTitle.Cells[1].Range.Text = "物证名称"; rowTitle.Cells[2].Range.Text = "物证编号"; rowTitle.Cells[3].Range.Text = "提取位置"; rowTitle.Cells[4].Range.Text = "提取人"; rowTitle.Cells[5].Range.Text = "存放位置"; object newRowTitle = rowTitle; WordTable.Rows.Add(ref newRowTitle); //移除模板里原有的行 WordTable.Rows[1].Delete(); WordTable.Rows[2].Delete(); } #endregion //oBookMark = "CaseFrom";//替换模板里CaseFrom的书签,移交人 //oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseFromDate";//替换模板里CaseFromDate的书签,移交日期 //oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseReceiver";//替换模板里CaseReceiver的书签,接收人 //oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseReceiveDate";//替换模板里CaseReceiveDate的书签,接收日期 //oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); object fileName = Server.MapPath("~/tmp.doc"); oWordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); #region 释放资源 if (oWordDoc != null) { oWordDoc.Close(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc); oWordDoc = null; } if (oWordApp != null) { oWordApp.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp); oWordDoc = null; } GC.Collect(); #endregion #region 流式输出tmp.doc string filePath = fileName.ToString(); FileInfo fileInfo = new FileInfo(filePath); //以字符流的形式下载文件 FileStream fs = new FileStream(filePath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); fs.Dispose(); Response.Clear(); Response.ContentType = "application/msword"; //通知浏览器下载文件而不是打开 Response.AddHeader("Content-Disposition", "attachment; filename=CaseRuku_" + dtAnjian.Rows[0]["anjianid"].ToString() + "_" + DateTime.Now.ToShortDateString() + ".doc"); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); #endregion } catch { try { #region 释放资源 if (oWordDoc != null) { oWordDoc.Close(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc); oWordDoc = null; } if (oWordApp != null) { oWordApp.Quit(ref missing, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp); oWordDoc = null; } } catch { } GC.Collect(); #endregion } #endregion [code=C#]#region 定义书签变量 object obj_bianhao = "dengjibianhao"; …… #endregion var appWord = new Word.Application(); Word.Document doc = null; object nothging = System.Reflection.Missing.Value; try { appWord.Visible = false; object objTrue = true; object objFalse = false; object objTemplate = Server.MapPath("djb.dot");//模板 object objDocType = Word.WdDocumentType.wdTypeDocument; doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); #region 给书签赋值 doc.Books.get_Item(ref obj_bianhao).Range.Text = "J" + dr["id"].ToString().PadLeft(5, '0'); //如果有图,可以调整图位置 //foreach (Word.InlineShape inlineshape in doc.Application.ActiveDocument.InlineShapes) //{ // Microsoft.Office.Interop.Word.Shape cShape = inlineshape.ConvertToShape(); // cShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom; // cShape.WrapFormat.Type = Word.WdWrapType.wdWrapFront; // cShape.Top = cShape.Top + 30f; //} #endregion //保存word object filepath = basePath + "docs\\J" + dr["id"].ToString().PadLeft(5, '0') + ".doc"; doc.SaveAs(ref filepath); object doNotSaveChanges = Word.WdSaveOptions.wdSaveChanges; doc.Close(doNotSaveChanges); appWord.Application.Quit(); doc = null; appWord = null;[/code] ASP.NET 导出 Word<%@ Page Language="C#" AutoEventWireup="true" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { Repeater1.DataSource = new String[] { "测", "孟宪会" }; Repeater1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { String ExportFileName = "孟宪会导出 Word 测试"; if (Request.Browser.Browser.IndexOf("MSIE") > -1) { ExportFileName = HttpUtility.UrlEncode(ExportFileName, System.Text.UnicodeEncoding.GetEncoding("GB2312")); } ExportFileName += ".doc"; Response.Clear(); Response.BufferOutput = true; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExportFileName); Response.ContentType = "application/ms-word"; Page.EnableViewState = false; System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter); Literal header = new Literal(); header.Text = "<h2>文章标题</h2>"; Header.Controls.Add(header); Header.RenderControl(textWriter); this.Repeater1.RenderControl(textWriter); Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body lang=ZH-CN>" + stringWriter.ToString()); Response.End(); Response.Flush(); } public override void VerifyRenderingInServerForm(System.Web.UI.Control control) { }</script><html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table cellspacing="1" cellpadding="3" width="96%" align="center" border="1"> </HeaderTemplate> <FooterTemplate> </table></FooterTemplate> <ItemTemplate> <tr><td>测试啦</td><td>This is a 测试啊</td><td> <%#Container.DataItem%></td></tr> </ItemTemplate> </asp:Repeater> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出成Word文档" /> </form></body></html> asp.net 导出word文档 1.新建一个word文档,然后把你想要显示的布局在word文档上布局好,然后另存为xx.html;2.以word打开方式打开xx.html,点击视图,选择页面视图,然后保存!3.打开xx.html,同样用打开word的方式打开,然后选择页面视图,然后在里面随便修改一点,我自己是弄个空格然后点保存4.后台代码:Response.ContentEncoding = System.Text.Encoding.UTF8;Response.ClearContent();Response.ClearHeaders();Response.AddHeader("content-disposition","attachment;filename="+name+".doc");Response.AddHeader("Content-type","application");Response.ContentType = "application/ms-html";Response.ContentEncoding = System.Text.Encoding.Default;Response.Write(sb.ToString());Response.Flush();Response.Close(); asp.net 导出Excel2007 能实现这个功能吗 此页的状态信息无效,可能已损坏! asp.net实现图片批量下载问题 用http方式下载网站文件夹内的文件时,用global文件方式计数会计数吗? 在 Server 端得知前端显示区大小 如何在支持asp的服务器上运行asp.net程序 .net 谁帮我简化一下代码 = = 我是新手,推荐几本asp.net的书吧 又是头疼的兼容性问题 GridView的行数显示问题
#region 导出到word
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
object oTemplate = Server.MapPath("~/WordTemplate/ruku.doc");
Word.Document oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
oWordDoc.Activate();//激活word文档操作
try
{
DataTable dtAnjian = mySocut.GetDataSet("select * from anjian where anjianid=(select anjianid from ruku where id=" + anjianID + ")", 0, 1, "anjian").Tables[0];
object oBookMark = "CaseID";//替换模板里CaseID的书签,案件编号
oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjianid"].ToString(); oBookMark = "CaseName";//替换模板里CaseName的书签,案件名称
oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjianname"].ToString(); oBookMark = "CaseTime";//替换模板里CaseTime的书签,案发时间
oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anfadate"].ToString(); oBookMark = "CasePlace";//替换模板里CasePlace的书签,案发地点
oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anfadz"].ToString(); oBookMark = "CaseDescription";//替换模板里CasePlace的书签,案件简介
oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); #region 循环物证表,生成表格
DataTable dt = GetTable(ids);
if (dt.Rows.Count > 0)
{
oBookMark = "CaseTable";//替换模板里CaseTable的书签,证物列表
Word.Table WordTable = oWordDoc.Books.get_Item(ref oBookMark).Range.Tables[1];
for (int i = 0; i < dt.Rows.Count; i++)
{
Word.Row row = WordTable.Rows[2];
DataRow dr = dt.Rows[i];
row.Cells[1].Range.Text = dr["wuzhengname"].ToString();
row.Cells[2].Range.Text = dr["wuzhengID"].ToString();
row.Cells[3].Range.Text = dr["tiqubw"].ToString();
row.Cells[4].Range.Text = dr["tiquren"].ToString();
row.Cells[5].Range.Text = dr["cunfangwzid"].ToString();
object newRow = row;
WordTable.Rows.Add(ref newRow);
}
// 添加标题
Word.Row rowTitle = WordTable.Rows[1];
rowTitle.Cells[1].Range.Text = "物证名称";
rowTitle.Cells[2].Range.Text = "物证编号";
rowTitle.Cells[3].Range.Text = "提取位置";
rowTitle.Cells[4].Range.Text = "提取人";
rowTitle.Cells[5].Range.Text = "存放位置";
object newRowTitle = rowTitle;
WordTable.Rows.Add(ref newRowTitle); //移除模板里原有的行
WordTable.Rows[1].Delete();
WordTable.Rows[2].Delete();
}
#endregion //oBookMark = "CaseFrom";//替换模板里CaseFrom的书签,移交人
//oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseFromDate";//替换模板里CaseFromDate的书签,移交日期
//oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseReceiver";//替换模板里CaseReceiver的书签,接收人
//oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); //oBookMark = "CaseReceiveDate";//替换模板里CaseReceiveDate的书签,接收日期
//oWordDoc.Books.get_Item(ref oBookMark).Range.Text = dtAnjian.Rows[0]["anjiantext"].ToString(); object fileName = Server.MapPath("~/tmp.doc");
oWordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); #region 释放资源
if (oWordDoc != null)
{
oWordDoc.Close(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc);
oWordDoc = null;
}
if (oWordApp != null)
{
oWordApp.Quit(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
oWordDoc = null;
}
GC.Collect();
#endregion #region 流式输出tmp.doc
string filePath = fileName.ToString();
FileInfo fileInfo = new FileInfo(filePath);
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
fs.Dispose();
Response.Clear();
Response.ContentType = "application/msword";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=CaseRuku_" + dtAnjian.Rows[0]["anjianid"].ToString() + "_" + DateTime.Now.ToShortDateString() + ".doc");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
#endregion }
catch
{
try
{
#region 释放资源
if (oWordDoc != null)
{
oWordDoc.Close(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordDoc);
oWordDoc = null;
}
if (oWordApp != null)
{
oWordApp.Quit(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
oWordDoc = null;
}
}
catch { }
GC.Collect();
#endregion
}
#endregion
#region 定义书签变量
object obj_bianhao = "dengjibianhao";
……
#endregion
var appWord = new Word.Application();
Word.Document doc = null;
object nothging = System.Reflection.Missing.Value;
try
{
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = Server.MapPath("djb.dot");//模板
object objDocType = Word.WdDocumentType.wdTypeDocument;
doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue); #region 给书签赋值
doc.Books.get_Item(ref obj_bianhao).Range.Text = "J" + dr["id"].ToString().PadLeft(5, '0'); //如果有图,可以调整图位置
//foreach (Word.InlineShape inlineshape in doc.Application.ActiveDocument.InlineShapes)
//{
// Microsoft.Office.Interop.Word.Shape cShape = inlineshape.ConvertToShape();
// cShape.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapTopBottom;
// cShape.WrapFormat.Type = Word.WdWrapType.wdWrapFront;
// cShape.Top = cShape.Top + 30f;
//}
#endregion //保存word
object filepath = basePath + "docs\\J" + dr["id"].ToString().PadLeft(5, '0') + ".doc";
doc.SaveAs(ref filepath);
object doNotSaveChanges = Word.WdSaveOptions.wdSaveChanges;
doc.Close(doNotSaveChanges);
appWord.Application.Quit();
doc = null;
appWord = null;
[/code]
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new String[] { "测", "孟宪会" };
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
String ExportFileName = "孟宪会导出 Word 测试";
if (Request.Browser.Browser.IndexOf("MSIE") > -1)
{
ExportFileName = HttpUtility.UrlEncode(ExportFileName, System.Text.UnicodeEncoding.GetEncoding("GB2312"));
}
ExportFileName += ".doc";
Response.Clear();
Response.BufferOutput = true;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + ExportFileName);
Response.ContentType = "application/ms-word";
Page.EnableViewState = false;
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
Literal header = new Literal();
header.Text = "<h2>文章标题</h2>";
Header.Controls.Add(header);
Header.RenderControl(textWriter);
this.Repeater1.RenderControl(textWriter);
Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body lang=ZH-CN>" + stringWriter.ToString());
Response.End();
Response.Flush();
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="1" cellpadding="3" width="96%" align="center" border="1">
</HeaderTemplate>
<FooterTemplate>
</table></FooterTemplate>
<ItemTemplate>
<tr><td>测试啦</td><td>This is a 测试啊</td><td>
<%#Container.DataItem%></td></tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出成Word文档" />
</form>
</body>
</html>
1.新建一个word文档,然后把你想要显示的布局在word文档上布局好,然后另存为xx.html;2.以word打开方式打开xx.html,点击视图,选择页面视图,然后保存!3.打开xx.html,同样用打开word的方式打开,然后选择页面视图,然后在里面随便修改一点,我自己是弄个空格然后点保存4.后台代码:Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition","attachment;filename="+name+".doc");
Response.AddHeader("Content-type","application");
Response.ContentType = "application/ms-html";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Write(sb.ToString());
Response.Flush();
Response.Close();