Results 1 to 16 of 16
  1. #1
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default How to open .ods file on button click event

    Hi Forum,
    I have done google for this but not getting a proper feedback. I am having an .ods (Open Office Calc) File. I need to open on click event of the button and read the contents of that file. Is there any Api for doing this??
    :o

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    This is possible using OpenOffice APIs,


    This code might help you

    Change the proper file name

    Let us know in case of any issue
    Java Code:
    import ooo.connector.BootstrapSocketConnector;
    
    import com.sun.star.beans.PropertyValue;
    import com.sun.star.container.XIndexAccess;
    import com.sun.star.container.XNameAccess;
    import com.sun.star.frame.XComponentLoader;
    import com.sun.star.lang.XComponent;
    import com.sun.star.lang.XMultiComponentFactory;
    import com.sun.star.text.XDefaultNumberingProvider;
    import com.sun.star.text.XTextDocument;
    import com.sun.star.text.XTextSection;
    import com.sun.star.text.XTextSectionsSupplier;
    import com.sun.star.uno.UnoRuntime;
    
    public class TestDoc {
    
    	public static void main(String[] args) throws Exception {
    		startProcessing();
    	}
    
    	static void startProcessing() throws Exception {
            String oooExeFolder = "C://Program Files//OpenOffice.org 3//program//";
            com.sun.star.uno.XComponentContext xContext = null;
            try {
                // get the remote office component context
            	xContext = BootstrapSocketConnector.bootstrap(oooExeFolder);
                if( xContext != null )
                    System.out.println("Connected to a running office ...");
            }
            catch( Exception e) {
                e.printStackTrace(System.err);
                System.exit(1);
            }
    		
            XMultiComponentFactory xMCF = xContext.getServiceManager();
            Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
            XComponentLoader xCompLoader = (XComponentLoader)
            UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, oDesktop);
            String sUrl = "file:///C:\\Template.odt";
            PropertyValue[] loadProps = new PropertyValue[1];
            loadProps[0] = new PropertyValue();
            loadProps[0].Name = "AsTemplate";
            loadProps[0].Value = new Boolean(true); 
            // Load the document, which will be displayed. More param info in apidoc
            XComponent xComp = xCompLoader.loadComponentFromURL(sUrl, "_blank", 0, loadProps);
            XTextDocument textDocument = (XTextDocument)UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xComp);
    	}
    }
    sanjeev,संजीव

  3. #3
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks Mr. sanjeevtarar,

    This code will open a .odt file, can itry this for Using GUI.
    :)

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Yes, you can do the same with UI
    sanjeev,संजीव

  5. #5
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hello Mr.sanjeevtarar,
    Can you please provide me the code for same............
    It will help me a lot in my project..
    have a great day.
    :) :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Can you please provide me the code
    This is a forum, not a code mill. Recommended reading: How to ask questions the smart way

    db

  7. #7
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Here is the code i have for selecting a file.How i select file with .ods
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.filechooser.FileFilter;

    //////////////////////////////////////////////////////// Initilization.....

    public class DemoOpen extends JFrame {

    //... Instance variables
    JTextField m_fileNameTF = new JTextField(15);
    JTextField m_wordCountTF = new JTextField(4);
    JFileChooser m_fileChooser = new JFileChooser();
    String OOO_EXEC_FOLDER = "C:/Program Files/OpenOffice.org 3/program/";


    //================================================== constructor
    DemoOpen() {
    m_fileNameTF.setEditable(false);
    m_wordCountTF.setEditable(false);

    //Buttons
    JButton openButton = new JButton("Open");
    JButton openButton1 = new JButton("Populate Data");
    //... Add listeners
    openButton.addActionListener(new OpenAction());
    //... Create contant pane, layout components
    JLabel textLabel = new JLabel("WELCOME TO DATA POPULATION ",SwingConstants.CENTER);
    textLabel.setPreferredSize(new Dimension(300, 100));
    JPanel content = new JPanel();
    content.add(textLabel);

    content.setLayout(new FlowLayout());
    content.add(openButton);
    content.add(openButton1);

    //content.add(m_fileNameTF);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setContentPane(content);
    this.pack();
    }

    //============================================= countWordsInFile

    ///////////////////////////////////////////////////// OpenAction
    class OpenAction implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
    //... Open a file dialog.
    int retval = m_fileChooser.showOpenDialog(DemoOpen.this);
    if (retval == JFileChooser.APPROVE_OPTION) {
    //... The user selected a file, process it.
    File file = m_fileChooser.getSelectedFile();
    JOptionPane.showMessageDialog(null,file);

    }
    }

    }

    //================= main
    public static void main(String[] args) {


    JFrame window = new DemoOpen();
    window.setVisible(true);
    window.setSize(new Dimension(350, 400));

    }


    }

  8. #8
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi Sanjeev,
    I am having two buttons and i want to read spreadsheet file on One of these buttons, i have used JFilechooser for the same.

  9. #9
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Ok, you can read excel file, then what do you want to do with that data.......do you need to write in another file
    sanjeev,संजीव

  10. #10
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi Sanjeev,
    I need to assign cell values to variables and write to a simple file. I am having data in .ods file is name, address , and birthday. I want to print custom message

  11. #11
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Hi Sanjeev,
    how can i achive this can u provide me some more details, Which things should i use for this?

    Waiting for your positive feedback.
    :)

  12. #12
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Dear Anagha,

    This is possible using OpenOffice APIs, and you can write code in txt file after reading from excel,

    I will help you with open office APIs, but first you prepare a program that reads from txt and write to txt

    And also implement all you functionality except reading from excel for excel i will let you know

    but prepare some code..........
    sanjeev,संजीव

  13. #13
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thankx For your Quick Response

  14. #14
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Hi Anagha,

    Did u got Solution ????

  15. #15
    sanjeev4343 is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Question Re: How to open .ods file on button click event

    Quote Originally Posted by sanjeevtarar View Post
    Dear Anagha,

    This is possible using OpenOffice APIs, and you can write code in txt file after reading from excel,

    I will help you with open office APIs, but first you prepare a program that reads from txt and write to txt

    And also implement all you functionality except reading from excel for excel i will let you know

    but prepare some code..........
    hi sanjeev
    i have a few dout, i made a app which read from one ods file to write it to another ods file i made the app but i didnt know how to read and write ods file can you help me.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: How to open .ods file on button click event

    Zombie, locking.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. creating a frame on button click event
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-16-2011, 05:11 AM
  2. How to Open .odf(calc) File using java on Click event of button
    By deshmukh.niraj04 in forum New To Java
    Replies: 14
    Last Post: 03-15-2011, 08:31 AM
  3. How we can open a file with a click in button
    By kostinio in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 04:46 PM
  4. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •