2

我正在编写java代码来实现以下目标。

1.阅读给定的Microsoft-office文档(.doc)文件。

2.在文件中搜索给定的字符串。

3.删除位于任意位置的给定字符串。

4.在指定位置插入或替换任何给定的字符串。

5.将更新的文件内容写入并保存到新的.doc文件中。

我编写了一个代码来读取、搜索、插入或替换、删除和保存文件,它运行良好,但我无法保留文本格式(如字体颜色、字体大小、对齐方式、左右缩进,样式等)应用于输入文件。

请任何人帮助我解决问题。

谢谢

4

3 回答 3

2

我将添加新的解决方案来设置Ms-Word 文档的样式。

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

}
于 2013-06-05T08:51:04.280 回答
2

您可以使用以下代码:

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

    }

}

如果您想将内容保存到同一个文件,您可以更改doc.write(new FileOutputStream("path to your inpufile/input_File_name"));

于 2017-12-06T11:21:42.390 回答
0

我建议您使用 Apache POI 文档。我将使用文档进行一些实验,并为 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

当我看到 DataFormat 类及其层次结构、BuiltinFormats 类和 CellStyle 类的 setDataFormat 方法时,我正在浏览 API。也做了一些实验,下面的代码似乎有效!

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

现在,一旦创建了电子表格,您就可以编辑一个单元格,并且当您退出时,格式仍然是“文本”。

我用完整的例子向你展示了另一种方式。我将进一步展示一个效果,你可以根据你的要求添加......

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

此代码将生成以下输出:

在此处输入图像描述

于 2013-05-30T10:56:24.093 回答