2013-05-30 68 views
0

我正在编写java代码以实现以下功能。使用Apache POI编辑Microsoft-office .doc文件使用Apache POI

1.阅读Microsoft Office文档(.doc)文件。

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

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

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

5.将更新后的文件内容写入并保存到新的.doc文件中。我写了一个代码来读取,搜索,插入或替换,删除和保存文件,它运行良好,但我无法保留文本格式(如字体颜色,字体大小,对齐方式,左和右缩进,样式等)在输入文件中应用。

请任何人都帮我解决这个问题。

谢谢

回答

0

我建议您使用Apache POI文档。 我会使用文档和获取文本为MS-Excel工作表轻易格式化做一些实验..

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

我在浏览通过API当我看到DATAFORMAT类及其层次, BuiltinFormats类, 和CellStyle类的setDataFormat方法。 所以做了一些实验,下面的代码似乎工作!现在

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

该代码会产生波纹管输出:

enter image description here

+0

这也将成为为Excel虽然不是字! – Gagravarr

+0

谢谢你的回复。虽然你给出了在Excel表格中定义样式的答案,但是需要获取和应用旧ms-office .doc文件中使用的相同样式。 我正在寻找一种方法来获取应用于文档文件中每个文本的样式。一旦我得到了样式,在创建doc文件的新副本(带有修改的内容)的同时应用样式将会很容易。 – nagesh

+0

我很抱歉误导你。 我已经添加了格式化Ms-Word文档的新答案,请检查... –

1

我会增添了新的解决方案造型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(); 
     } 
    } 

} 
+0

嗨,您能否帮助我在页面中添加寄宿生而不仅仅是段落。提前致谢。 –

0

您可以使用下面的代码:

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"));