Edit Microsoft-office .doc file in java using Apache POI

风格不统一 提交于 2019-12-08 03:43:26

问题


I'm writing java code to achieve the followings.

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

2.Search for given string in the file.

3.Delete the given String located in any place.

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

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

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.

Thank you


回答1:


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();
        }
    }

}



回答2:


You can use the following code:

public class EditingWord{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String filename = "path to input file/file_input_name";
        List<String> paraList = new ArrayList<String>();
        try {

            XWPFDocument doc = new XWPFDocument(OPCPackage.open(new FileInputStream(filename)));
            List<XWPFParagraph> paragraphList = doc.getParagraphs();
            for (XWPFParagraph para : paragraphList) {
                if ((para.getStyle() != null) && (para.getNumFmt() != null)) {
                    for (XWPFRun run : para.getRuns()) {
                        String text = run.text();
                        text = text.replaceAll(text, "replacement" + text);
                        run.setText(text, 0);
                    }
                }
            }
            doc.write(new FileOutputStream("path to your file/output_File_name"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

In case if you want to save your content to same file, you can change doc.write(new FileOutputStream("path to your inpufile/input_File_name"));




回答3:


I'll suggest you to use of Apache POI Documentation. I'll do some experiment using documentation and getting text formatting easily for Ms-Excel Sheet..

http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html

I was browsing through the API when I saw the DataFormat class and its hierarchy, the BuiltinFormats class, and the setDataFormat method of the CellStyle class. So did some experimentation, and the code below seems to work!

XSSFCellStyle textFormatStyle = book.createCellStyle(); 
textFormatStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text")); 
XSSFCell cell = row.createCell(columnIndex++); 
cell.setCellStyle(textFormatStyle); 

Now, once spreadsheet is created, you can edit a cell, and when you tab out, the format remains "text".

I having showing you another way with full example.. In which I'll show one effect further you can add as per your requirement...

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Style example");

HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("This is bold");
cell.setCellStyle(style);


font = workbook.createFont();
font.setItalic(true);
style = workbook.createCellStyle();
style.setFont(font);

row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("This is italic");
cell.setCellStyle(style);

try {
    FileOutputStream out = new FileOutputStream(new File("C:\\style.xls"));
    workbook.write(out);
    out.close();
    System.out.println("Excel written successfully..");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

This code will generate bellow output :



来源:https://stackoverflow.com/questions/16833767/edit-microsoft-office-doc-file-in-java-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!