使用Apache POI在Java编辑微软的Office .doc文件 [英] Edit Microsoft-office .doc file in java using Apache POI

查看:381
本文介绍了使用Apache POI在Java编辑微软的Office .doc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写Java code实现如下。

I'm writing java code to achieve the followings.

1.Read考虑到微软办公文档(.doc)文件。

1.Read given Microsoft-office document(.doc) file.

2.Search在文件中给定的字符串

2.Search for given string in the file.

3.删除指定的字符串位于任何地方。

3.Delete the given String located in any place.

4.Insert,或在指定位置更换任何给定的字符串。

4.Insert or replace any given string at specified position.

5.Write并保存更新的文件内容到新的.doc文件。

5.Write and save the updated file content into new .doc file.

我已经写了code阅读,搜索,插入或更换,删除和保存文件,它的工作好,但我不能够preserve文本格式(如字体颜色,字体大小,对齐,左,右缩进,样式等)在输入文件应用

I have written a code to read, search, insert or replace, delete and save the file and it's working good, but i couldn't able to preserve the text format(such as font color, font size, justification, left and right indent, styles etc) applied in the input file.

请人帮我解决这个问题。

please anyone helps me to solve the issue.

感谢您

推荐答案

我添加了新的解决方案,对于造型为Ms-Word文档..

I'll added new solution for styling Ms-Word Document..

public class CreateDocumentFromScratch {

    public static void main(String[] args) {
        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        paragraphOne.setBorderBottom(Borders.SINGLE);
        paragraphOne.setBorderTop(Borders.SINGLE);
        paragraphOne.setBorderRight(Borders.SINGLE);
        paragraphOne.setBorderLeft(Borders.SINGLE);
        paragraphOne.setBorderBetween(Borders.SINGLE);

        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setItalic(true);
        paragraphOneRunOne.setText("Hello world! This is paragraph one!");
        paragraphOneRunOne.addBreak();

        XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
        paragraphOneRunTwo.setText("Run two!");
        paragraphOneRunTwo.setTextPosition(100);

        XWPFRun paragraphOneRunThree = paragraphOne.createRun();
        paragraphOneRunThree.setStrike(true);
        paragraphOneRunThree.setFontSize(20);
        paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
        paragraphOneRunThree.setText(" More text in paragraph one...");

        XWPFParagraph paragraphTwo = document.createParagraph();
        paragraphTwo.setAlignment(ParagraphAlignment.DISTRIBUTE);
        paragraphTwo.setIndentationRight(200);
        XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
        paragraphTwoRunOne.setText("And this is paragraph two.");

        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(args[0]);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            document.write(outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

这篇关于使用Apache POI在Java编辑微软的Office .doc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆