C#——操作Word并导出PDF
一、操作Word
首先引用这个DLL,Microsoft.Office.Interop.Word,官方提供的。
可以操作word文字,表格,图片等。
文字通过替换关键字的方式实现
document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");
表格可以自己获取模板中已有的表格
Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];
table1.Cell(1, 1).Range.Text = "TEST1";
也可以自己创建表格,可以设计表头,单元格等。
int tableRow = 6 ;
int tableColumn = ;
//定义一个Word中的表格对象
Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = 1;
table.Cell(1, 1).Merge(table.Cell(2, 1));//纵向合并
table.Cell(1, 1).Range.Text = "牌号/代码"; table.Cell(1, 2).Merge(table.Cell(2, 2));//纵向合并
table.Cell(1, 2).Range.Text = "标准编号";
有一篇文章写的很详细可以参考下:https://www.cnblogs.com/xh6300/p/5915717.html
public bool CreateWord(DataTable dttmp)
{
bool result = false;
Object Nothing = Missing.Value;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = null;
string path = @"C:\Users\Administrator\Desktop\BB\合同模版.doc";
object FileName = @"C:\Users\Administrator\Desktop\BB\" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".doc";
application.Visible = false;
document = application.Documents.Open(path); int rowNum = dttmp.Rows.Count;
for (int i = ; i <= document.Paragraphs.Count; i++)
{
string temptext = document.Paragraphs[i].Range.Text;
//以下为替换文档模版中的关键字
if (temptext.Contains("{$village}"))
document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");
Microsoft.Office.Interop.Word.Table table1 = document.Tables[];
table1.Cell(, ).Range.Text = "TEST1";
if (temptext.Contains("{$Table1}"))
{
//设置表格的行数和列数
int tableRow = + rowNum;
int tableColumn = ;
//定义一个Word中的表格对象 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range,
tableRow, tableColumn, ref Nothing, ref Nothing);
//默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框
table.Borders.Enable = ;//这个值可以设置得很大
table.Cell(, ).Merge(table.Cell(, ));//纵向合并
table.Cell(, ).Range.Text = "牌号/代码"; table.Cell(, ).Merge(table.Cell(, ));//纵向合并
table.Cell(, ).Range.Text = "标准编号"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "单重\n(MT)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "张数"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "重量\n(MT))"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "单价\n(元/吨)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "金额(人民币)"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "交货期"; table.Cell(, ).Merge(table.Cell(, ));//横向合并
table.Cell(, ).Range.Text = "规格(mm)";
table.Cell(, ).Range.Text = "厚度";
table.Cell(, ).Range.Text = "宽度";
table.Cell(, ).Range.Text = "宽度"; table.Cell(, ).Merge(table.Cell(, ));
table.Cell(, ).Range.Text = "表面加工";
table.Cell(, ).Range.Text = "边缘";
table.Cell(, ).Range.Text = "精度"; }
} object format = document.SaveFormat;
document.Save(); application.Quit(ref Nothing, ref Nothing, ref Nothing);
return result; }
二、Word导出PDF
public bool WordToPDF(string sourcePath)
{
bool result = false;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置
if (!File.Exists(@PDFPath))//存在PDF,不需要继续转换
{
document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
}
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
//document.Close();
}
return result;
}
服务器部署可以参考;https://www.cnblogs.com/5426z/articles/4865312.html
C#——操作Word并导出PDF的更多相关文章
- Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...
- iText导出pdf、word、图片
一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或 ...
- Itext导出PDF,word,图片案例
iText导出pdf.word.图片 一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生 ...
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...
- jacob 操作word转pdf
项目需要对上传的word及pdf进行在线预览,因基于jquery的pdf插件,很方面实现在线预览,而word实现在线预览费劲不少,于是想到在进行上传处理时,直接将word转成pdf,在预览时直接预览p ...
- Java使用IText(VM模版)导出PDF,IText导出word(二)
===============action=========================== //退款导出word public void exportWordTk() throws IOE ...
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Java操作word转pdf
如果转换后出现乱码,是doc格式的文档的话请转换为docx!!! 下载相关jar包和一个授权到2099年的凭证文件. 链接: https://pan.baidu.com/s/1xudkKqR1-TLL ...
- Spring Boot 系列教程18-itext导出pdf下载
Java操作pdf框架 iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好 ...
随机推荐
- Linux shell awk数组使用
awk中使用数组 一.数组格式 数组是一个包含一系列元素的表. 格式如下: abc[1]="xiaohong" abc[2]="xiaolan" ...
- ROS学习笔记(二) :使用roslaunch
目录 roslaunch roslaunch的使用 以turtlesim为例 roslaunch roslaunch是ros自带的一个库,使用roslaunch可以同时运行多个节点,通过编写launc ...
- Slf4j 打日志的问题 Exception 没有堆栈信息
Slf4j 打日志的问题 Exception 没有堆栈信息 发现线上环境有的Exception堆栈信息没打出来,只有异常信息没有堆栈信息,难以定位 一般情况下日志这么打 log.info(" ...
- JDOJ 2157 Increasing
洛谷 P3902 递增 洛谷传送门 JDOJ 2157: Increasing JDOJ传送门 Description 数列A1,A2,--,AN,修改最少的数字,使得数列严格单调递增. Input ...
- PHP csv导出数据
全部导出和时间导出 html代码,全程并不需要引用什么插件 <include file="public@header"/> <link href="__ ...
- update Select 从查询的结果中更新表
UPDATE tbl_a a INNER JOIN tbl_b b ON a.arg=b.argSET a.arg2=b.arg2 这个语法即可实现
- [RN] 解决小米手机安装应用报:INSTALL_FAILED_USER_RESTRICTED问题
解决小米手机安装应用报:INSTALL_FAILED_USER_RESTRICTED问题 https://blog.csdn.net/u013023845/article/details/821082 ...
- 请写出jQuery绑定事件的方法,不少于两种
bind on live one 简写事件:click.hover.mousemove.mouseup.mousedown……
- css3中的box-sizing常用的属性有哪些?分别有什么作用?
content-box:默认标准盒模型,总宽=width+padding+border+margin border-box:IE标准,怪异盒模型,总宽=width+margin inherit:从父元 ...
- TCP连接和连接释放(TCP的三次挥手和四次握手)
TCP的运输连接管理 TCP是面向连接的协议.运输连接是用来传送TCP报文的.TCP运输连接的建立和释放是每一次面向连接的通信中必不可少的过程.因此,运输连接就有三个阶段,即:连接建立.数据传送和连接 ...