用户登录
用户注册

分享至

.net模板导出word

  • 作者: 老司饥
  • 来源: 51数据库
  • 2020-06-05

1. .net 页面导出为word

npoi可以导出为docx格式的word文档,具体怎么用表格,没有试过,给你一段导出的代码,你可以参考

using (DocX document = DocX.Create(Server.MapPath("~/Temp/ScoreResultDoc") + "/ScoreResultDoc.docx"))

{

Paragraph p = document.InsertParagraph();

// Append some text and add formatting.

p.Append("河北省信用档案\n")

.Font(new FontFamily("Times New Roman"))

.FontSize(24)

.Color(Color.Black)

.Bold();

p.Alignment = Alignment.center;

//p.Direction = Direction;

p.Append("记分告知单\n")

.Font(new FontFamily("Times New Roman"))

.FontSize(24)

.Color(Color.Black)

.Bold();

p.Alignment = Alignment.center;

Paragraph p1 = document.InsertParagraph();

//p1.Append(" ")

// .Font(new FontFamily("Times New Roman"))

// .FontSize(14)

// .Color(Color.Black)

// .Bold();

//float ff = 14.0f * 1.0f / 2.83f / 10.0f;

p1.IndentationFirstLine = f14;

p1.Append(businessName).UnderlineStyle(UnderlineStyle.singleLine)

.Font(new FontFamily("宋体"))

.FontSize(14)

.Color(Color.Black)

.Bold();

p1.Alignment = Alignment.left;

p1.Append(": 编号:[ ]号")

.Font(new FontFamily("宋体"))

.FontSize(14);

p1.Alignment = Alignment.left;

p1.SetLineSpacing(LineSpacingType.Line, 1.5f);

document.Save();

}

2. asp.net导出word

public void Download()

{

Random rd = new Random();

string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() + ".doc";

//存储路径

string path = Server.MapPath(fileName);

//创建字符输出流

StreamWriter sw = new StreamWriter(path, true, System.Text.UnicodeEncoding.UTF8);

//需要导出的内容

string str = "<html><head><title>;无标题文档</title></head><body>;这里放从数据库导出的word文档内容</body></html>";

//写入

sw.Write(str);

sw.Close();

Response.Clear();

Response.Buffer = true;

this.EnableViewState = false;

Response.Charset = "utf-8";

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

Response.ContentType = "application/octet-stream";

Response.WriteFile(path);

Response.Flush();

Response.Close();

Response.End();

}

标准word文档的格式微软暂未公布,由此我们可将需要导出的内容转为标准HTML文件储存,后缀名为.doc

也可以将要导出内容转为标准XML格式存储,改后缀为.doc

具体格式随意新建个word文档,输入内容,另存为.XML可见

另外一种导出方式为word导出标准格式,服务器需要安装Microsoft Office word,需要预先设置好一个word文档并在要插入内容的地方设置书签做为模版,导出word文档时需要先遍历模版文件中的所有书签,然后给书签赋值就能实现导出数据了

还有不懂的可以直接百度HI我

是否可以解决您的问题?

3. .net页面保存为word

这个最好是调用com组件,先建个word模版,这个模版这是你要导入的内容,其中的布局可以根据table里的布局来设置,具体的代码及参数设置你可以参考下一下代码:using Microsoft.Office.Interop.Word;///

/// PrintWord 的摘要说明/// public class PrintWord{ public PrintWord(System.Collections.SortedList list, object oTemplate) { object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = false; ApplicationClass oWordApp = new ApplicationClass(); oWordApp.Visible = true; Document oWordDoc = oWordApp.Documents.Open(ref oTemplate, ref missing, ref readOnly, 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); foreach (System.Collections.DictionaryEntry item in list) { for (int j = 1; j <= oworddoc.bookmarks.count;="" j++)="" {="" object="" obji="j;" if="" (oworddoc.bookmarks.get_item(ref="" obji).name.tolower()="=" item.key.tostring().tolower())="" oworddoc.bookmarks.get_item(ref="" obji).range.text="item.Value.ToString();" }="" }="" object="" savechanges="Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;" object="" originalformat="Microsoft.Office.Interop.Word.WdOriginalFormat.wdPromptUser;" object="" routedocument="true;object" savepath="HttpContext.Current.Server.MapPath("~")" +="" @"\tender\tenderinformation\sbdoc\"="" +="" datetime.now.tostring("yyyymmddhhssmm")="" +="" ".doc";="" oworddoc.saveas(ref="" savepath,="" 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);="" oworddoc.close(ref="" savechanges,="" ref="" originalformat,="" ref="" routedocument);="" owordapp.application.quit(ref="" missing,="" ref="" missing,="" ref="" missing);="" owordapp="null;" system.io.fileinfo="" file="new" system.io.fileinfo(savepath.tostring());="" httpcontext.current.response.clear();="" httpcontext.current.response.addheader("content-disposition",="" "attachment;="" filename=" + file.Name); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(" utf-8");="" httpcontext.current.response.addheader("content-length",="" file.length.tostring());="" httpcontext.current.response.contenttype="application/ms-word" ;="" httpcontext.current.response.writefile(file.fullname);="" httpcontext.current.response.end();="" file.delete();="">

4. 如何C#操作word模板生成word文档并打开

codeproject上有很多操作word的C#代码:顺便给你几个链接:Writer = new System.Web.UI.HtmlTextWriter(writer); source.RenderControl(htmlWriter); //输出 HttpContext.Current.Response.Write(writer.ToString()); HttpContext.Current.Response.End(); } /**//// /// 将Web控件或页面信息导出(带文件名参数) /// /// 控件实例 /// 导出类型:Excel或Word /// 保存文件名 public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename) { //设置Http的头信息,编码格式 if (DocumentType == "Excel") { //Excel HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".xls",System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-excel"; } else if (DocumentType == "Word") { //Word HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".doc",System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-word"; } HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态 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); //输出 HttpContext.Current.Response.Write(writer.ToString()); HttpContext.Current.Response.End(); } #endregion 调用说明#region 调用说明 //方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中 //第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定, //第二个参数DocumentType表示导出的文件类型word或excel //第三个参数filename表示需要导出的文件所取的文件名 //调用方法: //ExportData export=new ExportData(); //export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面 //这是将整个页面导出为Word,并命名为testfilename #endregion }}。

7. 我要用C#,NET 导出一个word文档

楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格 string title = "个人信息"; object titleLengh = title.Length; string first = "\n 公司最近需要利用C#对项目进行编程,其" + "中存在一个功能就是可自动生成WORD文档,但一直以来都" + "找不到什么好办法,无奈之下,只有自已学着写一个了."; object firstLengh = first.Length; string second = "\n 如果能真正生成WORD文档的好处有:"; object secondLengh = second.Length; string third = "\n1、根据数据库信息自动生成文档;"; object thirdLengh = third.Length; string forth = "\n2、免去书写文档之苦;"; object forthLengh = forth.Length; string fifth = "\n3、可以通过邮件方式传出文档。

"; object fifthLengh = fifth.Length; object missing; object zero = 0; object c = 10; object one = 1; object two = 2; object tree = 3; object four = 4; object five = 5; object six = 6; object seven = 7; object eight = 8; object nine = 9; object ten = 10; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); missing = System.Reflection.Missing.Value; wa.Visible = true; wa.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero); object r = myRange; Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r); p.Range.InsertBefore(title); //p.Range.Font.Size = 1; Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh); //titleRange.Font.Size = 1; titleRange.Font.Name = "幼圆"; titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue; //MessageBox.Show("NO.1"); //titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range; Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing); r = firstR; p = wa.ActiveDocument.Paragraphs.Add(ref r); firstR.Font.Size = 40; firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在表格第一单元格中添加自定义的文字内容 table.Cell(1, 1).Range.Text = "lllll"; firstR.InsertAfter(first);//firstR.InsertParagraphAfter(); //firstR=wa.ActiveDocument.Paragraphs.Item(3).Range; firstR.InsertAfter(second); firstR.InsertAfter(third); firstR.InsertAfter(forth); firstR.InsertAfter(fifth); Context.Response.Write("成功");。

8. .net页面保存为word

这个最好是调用com组件,先建个word模版,这个模版这是你要导入的内容,其中的布局可以根据table里的布局来设置,具体的代码及参数设置你可以参考下一下代码:using Microsoft.Office.Interop.Word;///

/// PrintWord 的摘要说明/// public class PrintWord{ public PrintWord(System.Collections.SortedList list, object oTemplate) { object missing = System.Reflection.Missing.Value; object readOnly = false; object isVisible = false; ApplicationClass oWordApp = new ApplicationClass(); oWordApp.Visible = true; Document oWordDoc = oWordApp.Documents.Open(ref oTemplate, ref missing, ref readOnly, 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); foreach (System.Collections.DictionaryEntry item in list) { for (int j = 1; j <= oworddoc.bookmarks.count;="" j++)="" {="" object="" obji="j;" if="" (oworddoc.bookmarks.get_item(ref="" obji).name.tolower()="=" item.key.tostring().tolower())="" oworddoc.bookmarks.get_item(ref="" obji).range.text="item.Value.ToString();" }="" }="" object="" savechanges="Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;" object="" originalformat="Microsoft.Office.Interop.Word.WdOriginalFormat.wdPromptUser;" object="" routedocument="true;object" savepath="HttpContext.Current.Server.MapPath("~")" +="" @"\tender\tenderinformation\sbdoc\"="" +="" datetime.now.tostring("yyyymmddhhssmm")="" +="" ".doc";="" oworddoc.saveas(ref="" savepath,="" 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);="" oworddoc.close(ref="" savechanges,="" ref="" originalformat,="" ref="" routedocument);="" owordapp.application.quit(ref="" missing,="" ref="" missing,="" ref="" missing);="" owordapp="null;" system.io.fileinfo="" file="new" system.io.fileinfo(savepath.tostring());="" httpcontext.current.response.clear();="" httpcontext.current.response.addheader("content-disposition",="" "attachment;="" filename=" + file.Name); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(" utf-8");="" httpcontext.current.response.addheader("content-length",="" file.length.tostring());="" httpcontext.current.response.contenttype="application/ms-word" ;="" httpcontext.current.response.writefile(file.fullname);="" httpcontext.current.response.end();="" file.delete();="">

转载请注明出处51数据库 » .net模板导出word

软件
前端设计
程序设计
Java相关