将字符串写入粗体和下划线样式的文件

时间:2013-12-02 10:37:45

标签: java ms-word

我的课程WriteFile的方法WriteToFileString写入word文档。

  WriteFile data=new WriteFile("C:/test3.doc",true);  
  data.writeToFile(user.saxeli+" "+user.gvari+" "+user.asaki+" "+user.swori);

如何用粗体和下划线样式将String写入“test3.doc”?

这是WriteFile类:

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class WriteFile {

 private String path;
 private boolean append=true;
 public WriteFile(String path,boolean append){
  this.path=path; this.append=append;
}
 public void writeToFile(String text) throws IOException{
  FileWriter write=new FileWriter(path,append);
  PrintWriter print_line= new PrintWriter(write);
  print_line.printf("%s"+"%n", text);
  print_line.close();
}
}

2 个答案:

答案 0 :(得分:6)

看看Apache POI。该库可以写入Microsoft Word文件并使用粗体和下划线(字体)样式。

答案 1 :(得分:1)

你可以参考这个::

 XWPFParagraph paragraphOne = document.createParagraph();
        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setItalic(true);
        paragraphOneRunOne.setText(text);

点击此链接:: http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/