1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
package word;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JTextPane;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class DocFile
{
public void newWordDoc(String filename, String fileContent) throws Exception
{
XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText(fileContent);
tmpRun.setFontSize(18);
FileOutputStream fos = new FileOutputStream(new File(
"M:\\" + filename + ".doc"));
document.write(fos);
fos.close();
}
public static void main(String[] args) throws Exception
{
String fileName = "testfile";
JTextPane fileContent = new JTextPane();
fileContent.setContentType("text/html");
fileContent.setText("<html><body>"
+ "<h1>My First Heading</h1>"
+ "<p>My first paragraph.</p>"
+ "</body></html>");
DocFile app = new DocFile();
app.newWordDoc(fileName,fileContent.getText());
}
} |
Partager