我读了一个word文档,想用
Java写入另一个word文件.我希望将读取文档中的内容的样式(字体,粗体,斜体,标题等)写入,因为它是创建的新文档.
我能够复制内容而不是格式样式.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.util.List;
public class ReadFile
{
public static void main(String[] args)throws Exception
{
XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));
List<XWPFParagraph> paragraphList = docx.getParagraphs();
XWPFDocument document= new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
XWPFParagraph n = document.createParagraph();
XWPFRun run=n.createRun();
for (XWPFParagraph paragraph: paragraphList)
{
run.setText(paragraph.getText());
run.addCarriageReturn();
}
document.write(out);
document.close();
out.close();
System.out.println("Test2.docx written successfully");
}
}
我得到了复制相同格式文本的答案,但我无法复制数字.
我执行了这段代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import java.util.List;
public class ReadFile
{
public static void main(String[] args)throws Exception
{
XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx"));
List<XWPFParagraph> paragraphList = docx.getParagraphs();
XWPFDocument document= new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx"));
XWPFParagraph n = document.createParagraph();
for (XWPFParagraph paragraph : paragraphList)
{
for(XWPFRun run1 : paragraph.getRuns())
{
XWPFRun run=n.createRun();
run.setText(run1.getText(0));
run.setFontFamily( run1.getFontFamily() );
run.setBold( run1.isBold() );
run.setItalic( run1.isItalic() );
run.setStrike( run1.isStrike() );
run.setColor( run1.getColor() );
}
XWPFRun run=n.createRun();
run.addCarriageReturn();
}
document.write(out);
document.close();
out.close();
System.out.println("Test2.docx written successfully");
}
}
最佳答案 这可能有所帮助:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class FontStyle
{
public static void main(String[] args)throws Exception
{
//Blank Document
XWPFDocument document= new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(
new File("fontstyle.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set Bold an Italic
XWPFRun paragraphOneRunOne = paragraph.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Font Style");
paragraphOneRunOne.addBreak();
//Set text Position
XWPFRun paragraphOneRunTwo = paragraph.createRun();
paragraphOneRunTwo.setText("Font Style two");
paragraphOneRunTwo.setTextPosition(100);
//Set Strike through and Font Size and Subscript
XWPFRun paragraphOneRunThree = paragraph.createRun();
paragraphOneRunThree.setStrike(true);
paragraphOneRunThree.setFontSize(20);
paragraphOneRunThree.setSubscript(
VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" Different Font Styles");
document.write(out);
out.close();
System.out.println("fontstyle.docx written successully");
}
}
道具:http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_font_style.htm