2013-05-30 186 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"));