converting doc

Upload: vinhxuann

Post on 04-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Converting Doc

    1/4

    JTextArea to Word Document

    Jakarta POI has provided several classes that enable us to perform read, write operations with ms word

    file. Here we are going to write the data into word document file through a swing component.

    You can see in the given code, we have allowed the user to enter data in textarea. As the user clicked the

    'Save' button after entering the data, the data will get saved in the word file using POI classes. And you

    can view your file by clicking the 'View' button. This is possible due to Runtime class.

    Here is the code:

    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;

    import java.util.*;

    import java.io.*;

    import org.apache.poi.hwpf.HWPFDocument;

    import org.apache.poi.hwpf.usermodel.*;

    import org.apache.poi.hwpf.usermodel.Range;

    import org.apache.poi.poifs.filesystem.*;

    import org.apache.poi.hwpf.extractor.WordExtractor;

    publicclassTextAreaInDocumentextendsJFrame{

    JTextArea area;

    JScrollPane pane;

    JButton b1, b2;

    File file =newFile("C:/New.doc");

    privatestaticvoid writeDoc(StringFileName,String content){

    try{

    POIFSFileSystem fs =newPOIFSFileSystem();

    DirectoryEntry directory = fs.getRoot();

    directory.createDocument("WordDocument",new

    ByteArrayInputStream(

    content.getBytes()));

    FileOutputStream out =new

  • 7/30/2019 Converting Doc

    2/4

    FileOutputStream(FileName);

    fs.writeFilesystem(out);

    out.close();

    }catch(Exception ex){

    System.out.println(ex.getMessage());

    }

    }

    TextAreaInDocument(){

    area =newJTextArea(5,30);

    area.setFont(newFont("courier New",Font.BOLD,12));

    pane =newJScrollPane(area);

    b1 =newJButton("Save");

    b2 =newJButton("View");

    b1.addActionListener(newActionListener(){

    publicvoid actionPerformed(ActionEvent e){

    String text = area.getText();

    String f = file.getPath();

    writeDoc(f, text);

    area.setText("");

    }

    });

    b2.addActionListener(newActionListener(){

    publicvoid actionPerformed(ActionEvent e){

    try{

    Runtime rt =Runtime.getRuntime();

    rt.exec("cmd.exe /C start

    C:/New.doc");

    }catch(Exception ex){

    }

    }

    });

    JPanel p =newJPanel();

    p.add(pane);

    p.add(b1);

  • 7/30/2019 Converting Doc

    3/4

    p.add(b2);

    add(p);

    setVisible(true);

    pack();

    }

    publicstaticvoid main(String[] args){

    TextAreaInDocument doc =newTextAreaInDocument();

    }

    }

    Output:

    On clicking the button 'Save', the text will get saved in the word file. You can view your word file

    by clicking the 'View' button.

    Related Tags for JTextArea to Word Document:

  • 7/30/2019 Converting Doc

    4/4

    Download Link :

    http://mirror-fpt-telecom.fpt.net/apache//poi/dev/bin/poi-bin-3.8-beta5-20111217.zip 21.9MB

    http://mirror-fpt-telecom.fpt.net/apache//poi/dev/src/poi-src-3.8-beta5-20111217.zip 51.2MB

    http://mirror-fpt-telecom.fpt.net/apache/poi/dev/bin/poi-bin-3.8-beta5-20111217.ziphttp://mirror-fpt-telecom.fpt.net/apache/poi/dev/bin/poi-bin-3.8-beta5-20111217.ziphttp://mirror-fpt-telecom.fpt.net/apache/poi/dev/src/poi-src-3.8-beta5-20111217.ziphttp://mirror-fpt-telecom.fpt.net/apache/poi/dev/src/poi-src-3.8-beta5-20111217.ziphttp://mirror-fpt-telecom.fpt.net/apache/poi/dev/src/poi-src-3.8-beta5-20111217.ziphttp://mirror-fpt-telecom.fpt.net/apache/poi/dev/bin/poi-bin-3.8-beta5-20111217.zip