数据按书签方式插入word例子
来源:互联网 发布:常州软件培训机构 编辑:程序博客网 时间:2024/05/21 15:46
#region WordDoc
public void WordDoc()
{
Microsoft.Office.Interop.Word.Application appWord = null;
Microsoft.Office.Interop.Word.Document doc = null;
try
{
appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
appWord.Visible = false;
object objTrue = true;
object objFalse = false;
object objTemplate = @"G:"123.doc";
//System.Web.HttpContext.Current.Server.MapPath(@"G:"123.doc");//模板路径
object objDocType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
//第一步生成word文档
//定义书签变量
object obDD_Name = "DD_Name";//姓 名
object obDD_Sex = "DD_Sex";//性 别
object obDD_Age = "DD_Age";//年龄
object obDD_Birthday = "DD_Birthday"; //出生年月
object obDD_Nation = "DD_Nation"; //民 族
object obDD_Native = "DD_Native"; //籍 贯
//...
//第二步 读取数据,填充数据集
//SqlDataReader dr = null;//读取出来的数据集
//第三步 给书签赋值
//给书签赋值
object missing = System.Reflection.Missing.Value;
object BookMarkName = "obDD_Name";
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
doc.ActiveWindow.Selection.TypeText("朱一鸣");
doc.ActiveWindow.Selection.TypeParagraph();
//doc.Bookmarks.get_Item(ref i).Range.Text = "朱一鸣"; //姓 名
//doc.Bookmarks.get_Item(ref j).Range.Text = "男";//性 别
//doc.Bookmarks.get_Item(ref k).Range.Text = "23";//年龄
// ....
//第四步 生成word
object filename = @"G:"zym" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".doc";
object miss = System.Reflection.Missing.Value;
doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object missingValue = Type.Missing;
object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/msword";
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.IO.FileInfo file = new System.IO.FileInfo(filename.ToString());
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8) + "");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.WriteFile(file.FullName);
Response.Flush();
Response.Close();
File.Delete(filename.ToString());
}
catch (System.Exception e)
{
//捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
string aa = e.ToString();
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
appWord.Application.Quit(ref miss, ref miss, ref miss);
doc = null;
appWord = null;
}
}
#endregion
- 数据按书签方式插入word例子
- 数据按书签方式插入word例子
- 数据按书签方式插入word例子
- Word批量给每个单元格插入书签
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- c#操作word 在指定书签插入文字或者图片
- C# asp.net Word中使用书签插入图片导出
- word根据模板给指定书签插入值
- 在word里指定书签的位置插入图片
- c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
- Aspose.Words使用word模板中的书签插入图片(导出含有图片的word)
- word2003 插入书签 书签定位 查看书签 删除书签
- 数据插入的方式
- word vba 内置书签
- Jacob操作Word书签
- 操作word文档书签
- c#操作word 在指定书签插入文字或者图片(转)
- C#操作Word文档(加密、解密、对应书签插入分页符)
- H3C S5100-SI[EI]系列以太网交换机 命令手册-Release 220X系列
- Learning Perl: 3.6. The foreach Control Structure
- 利用数据库进行表达式运算
- 路由的循环问题
- Learning Perl: 3.7. Scalar and List Context
- 数据按书签方式插入word例子
- Learning Perl: 3.8. in List Context
- 个人总结这个东西,还是要写的
- 求一个数组中第二大数...
- Learning Perl: 4.1. Defining a Subroutine
- 如何顺利实现LINUX系统开机自动启动指定的脚本程序
- Learning Perl: 4.2. Invoking a Subroutine
- OpenCore之文件解析
- 控制网页不打印和分页