java 读取txt文件生成word文档

在pom.xml中配置如下:

复制代码

com.e-iceblue

https://repo.e-iceblue.cn/repository/maven-public/

e-iceblue

spire.doc.free

3.9.0

复制代码

2. 手动导入。

需先下载jar包到本地,解压,找到lib路径下的jar文件。然后在Java程序中打开“Project Structure”窗口,然后执行如下步骤导入:

找到本地路径下的jar文件,添加到列表,然后导入:

读取txt生成Word

代码大致步骤如下:

  1. 实例化Document类的对象。然后通过**Document.addSection()方法和Section.addParagraph()**方法添加节和段落。

  2. 读取txt文件:创建InputStreamReader类的对象,构造方法中传递输入流和指定的编码表名称。通过BufferedReader类,创建字符流缓冲区。将读取的txt内容通过**Paragraph.appendText()**方法添加到段落。

  3. 调用**Document.saveToFile(string fileName, FileFormat fileFormat)**方法保存为Word文档。

复制代码

import com.spire.doc.*;

import com.spire.doc.documents.Paragraph;

import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

import java.io.*;

public class ReadTextAndCreateWord {

public static void main(String[] args) throws IOException {

//实例化Document类的对象,并添加section和paragraph

Document doc = new Document();

Section section = doc.addSection();

Paragraph paragraph = section.addParagraph();

//读取txt文件

String encoding = “GBK”;

File file = new File(“test.txt”);

if (file.isFile() && file.exists()) {

InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encoding);

BufferedReader bufferedReader = new BufferedReader(isr);

String lineTXT;

while ((lineTXT = bufferedReader.readLine()) != null) {

paragraph.appendText(lineTXT);//在段落中写入txt内容

}

isr.close();

}

//设置段落样式,并应用到段落

ParagraphStyle style = new ParagraphStyle(doc);

style.setName(“newstyle”);

style.getCharacterFormat().setBold(true);

style.getCharacterFormat().setTextColor(Color.BLUE);

style.getCharacterFormat().setFontName(“幼圆”);

style.getCharacterFormat().setFontSize(12);

doc.getStyles().add(style);

paragraph.applyStyle(“newstyle”);

paragraph.getFormat().setMirrorIndents(true);

//保存为docx格式的Word

doc.saveToFile(“addTxttoWord.docx”, FileFormat.Docx_2013);

doc.dispose();

}

}

复制代码

Word创建结果:

注意事项

代码中的txt文件和word保存路径为IDEA程序项目文件夹路,如:F:\IDEAProject\CreateWord_Doc\addTxttoWord.docx ,文件路径可定义为其他路径。
—End—

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用POI库来实现在Java中根据Word模板生成Word文档。以下是基本的步骤: 1. 创建一个Word模板文件,将需要动态生成的变量用占位符(如${variable})替换。 2. 在Java中使用POI库打开模板文件读取并替换占位符。 3. 将替换后的内容写入到新的Word文档中。 4. 保存新的Word文档。 示例代码如下: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class WordTemplateGenerator { public static void main(String[] args) throws Exception { // 读取Word模板文件 FileInputStream fis = new FileInputStream(new File("template.docx")); XWPFDocument document = new XWPFDocument(fis); // 替换模板中的变量 Map<String, String> variables = new HashMap<>(); variables.put("name", "张三"); variables.put("age", "25"); variables.put("address", "北京市海淀区"); replaceVariables(document, variables); // 保存新的Word文档 FileOutputStream fos = new FileOutputStream(new File("output.docx")); document.write(fos); fos.close(); document.close(); } private static void replaceVariables(XWPFDocument document, Map<String, String> variables) { for (XWPFParagraph paragraph : document.getParagraphs()) { replaceInParagraph(paragraph, variables); } for (XWPFTable table : document.getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph paragraph : cell.getParagraphs()) { replaceInParagraph(paragraph, variables); } } } } } private static void replaceInParagraph(XWPFParagraph paragraph, Map<String, String> variables) { for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null) { for (String variable : variables.keySet()) { if (text.contains(variable)) { text = text.replace(variable, variables.get(variable)); run.setText(text, 0); } } } } } } ``` 在上述示例代码中,我们使用了XWPFDocument类来读取Word模板文件生成新的Word文档。replaceVariables()方法用于替换模板中的变量,replaceInParagraph()方法用于替换段落中的变量。在示例代码中,我们假设模板中的变量为name、age和address。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值