word文档作为办公的主要文件格局,有时辰我们会需要我们的java法式直接将计较获得的成果生当作word文档,便利取用,这里我们利用第三方包iText来实现

方式/
1起首在Eclipse上新建一个Java工程,我取名为Java2Word,本经验只简单介绍一些iText的api,更多的api请自行百度

2项目需要用到这三个jar包,将他们拷贝到项目标根目次并BuildPath
itext-2.1.7.jar
itext-asian.jar
itext-rtf-2.1.7.jar
(找不到资本的小伙伴可以在简书搜刮 1023014360Java2Word)

3将三个jar包Buildpath到依靠里

4按例贴上完整代码,直接复制可运行
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfWriter2;
/**
* 建立word文档
:
* 1,成立文档
* 2,建立一个书写器
* 3,打开文档
* 4,标的目的文档中写入数据
* 5,封闭文档
*/
public class Java2word {
public Java2word() {
}
/**
* @param args
*/
public static void main(String[] args) {
creatDoc("C:/Users/Administrator/Desktop/test/经验.doc",
"百度经验\n多多浏览");
}
public static void creatDoc(String path,String text) {
//设置纸张的大小
Document document = new Document(PageSize.A4);
try {
//建立word文档
RtfWriter2.getInstance(document,new FileOutputStream(path));
//打开文档
document.open();
//建立段落
【怎么用Java生成并导出word文档,操作word文档】 Paragraph p = new Paragraph(text,new Font(Font.NORMAL, 10, Font.BOLD, new Color(0, 0, 0)) );
//设置段落为居中对齐
p.setAlignment(Paragraph.ALIGN_CENTER);
//写入段落
document.add(p);
//关流
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
猜你喜欢
- AI里的矩形工具的使用教程
- excel的求和函数几种用法
- 肺肿瘤是怎么形成的,怎样才能消除
- 使用下拉选择框在Flash舞台选择显示图形
- 向登录Windows10系统用户添加提示信息的方法
- 用Word制作表格的方法
- 红手指怎么安装淘宝
- 怎样用手机删掉微博用过的头像
- 用光碟重装系统的方法
- 狗狗怀孕吃什么有营养
