Viewing an excel file

Hi experts.....

I am looking for an API that can open a file with certain application.
i.e. it opens test.xls with excel.exe application

Thank you

DarknessShadowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
something like:

        String[] runArgs = {"c:\\test\\test.xls"};
        try
        {
        Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office10\\excel.exe", runArgs);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
0
esorfCommented:
Or

 String[] runArgs = {"/C", "start", "c:\\test\\test.xls"};
and
 Runtime.getRuntime().exec("C:\\winnt\system32\cmd.exe", runArgs);

The advantage here is that you can start any document and the shell will launch the appropriate application.

Try it out from the command line with
  cmd /C start c:\test\test.xls

- Frank
0
petmagdyCommented:
esorf,
did u tried ur solution?
I tried:

             String[] runArgs = {"/C", "start", "c:\\test\\test.xls"};
        try
        {
             Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\cmd.exe", runArgs);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

didn't work
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vikraman_bCommented:
Hi try this,
also have a look on this url
http://j-integra.intrinsyc.com/com/doc/java_excel/programmerguide.html

import excel.*;
import java.util.*;
 
public class JExcelGuide {
     
  public JExcelGuide(){
  }
 
  public static void main(String[] args) {
   
    try {
      JExcelGuide JExcelGuide1 = new JExcelGuide();
 
      JExcelGuide1.createWorkbook(
                                  "MyApplication",
                                  "C:\\JExB\\examples");
 
      JExcelGuide1.openWorkbook(
                                "MyApplication",
                                "C:\\JExB\\examples",
                                "saveCopyAs.xls");
    } catch(Exception e) {
      e.printStackTrace();
    } finally {
      // Release all remote objects that haven't already been garbage collected.
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
 
  /*
  * This function show how to create, save and close a new Workbook object
  * and save it.
  */
  public void createWorkbook(
                            String strNewApplicationCaption,
                            String strNewApplicationFilePath) {
    try {
      System.out.println( "createWorkbook starts." );
 
      Application _xlApp = new Application();
      System.out.println( "Application object created." );
 
      // MS Office will be invoked visually
      _xlApp.setVisible( true );
       
      // If this is set to true, MS Office requires user's interaction
      _xlApp.setDisplayAlerts( false );
       
      // operating Workbook object
      Workbooks workbooks=_xlApp.getWorkbooks();
      System.out.println( "get workbooks." );
       
      int iWorkbookCount = workbooks.getCount();
      System.out.println( "    [Workbook::Count]" + iWorkbookCount );
       
      Workbook wb1=workbooks.add( null );
      System.out.println( "add 1st workbook" );
       
      Workbook wb2=workbooks.add( new Integer(XlWBATemplate.xlWBATWorksheet) );
      System.out.println( "add 2nd workbook" );
       
      iWorkbookCount = workbooks.getCount();
      System.out.println( "    [Workbook::Count]" + iWorkbookCount );
       
      // save as default name
      wb1.save();
      System.out.println(
                        "workbook 1 saved as " +
                        wb1.getFullName());
 
      // save as default name
      wb2.save();
      System.out.println(
                        "workbook 2 saved as " +
                        wb2.getFullName());
 
      // save as custom name
      wb1.saveCopyAs(strNewApplicationFilePath + "\\saveCopyAs.xls");
      System.out.println("workbook 1 saved as " +
                        strNewApplicationFilePath +
                        "\\saveCopyAs.xls");
 
      // save as custom name using shared mode
      // You may use XlSaveAsAccessMode.xlShared.
      // To share this workbook, it must contain at
      // least one visible worksheet.
      wb1.saveAs(
                "saveAs.xls",
                null,
                null,
                null,
                null,
                null,
                XlSaveAsAccessMode.xlExclusive,
                null,
                null,
                null,
                null);
 
      System.out.println("workbook 1 saved as " +
                        strNewApplicationFilePath +
                        "saveAs.xls");
 
      // clear up Workbooks and Workbook objects
      wb1.close( new Boolean (true), null, null );
      wb2.close( new Boolean (true), null, null );
      workbooks.close();
 
      // quit application
      _xlApp.quit();
 
      System.out.println( "createWorkbook ends." );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  /*
  * This function show how to open and close an existing Workbook object.
  */
  public void openWorkbook(
                          String strNewApplicationCaption,
                          String strNewApplicationFilePath,
                          String strNewWorkbookName) {
    try {
      System.out.println( "openWorkbook starts." );
 
      Application _xlApp = new Application();
      System.out.println( "Application object created." );
       
      // MS Office will be invoked visually
      _xlApp.setVisible( true );
       
      // If this is set to true, MS Office requires user's interaction
      _xlApp.setDisplayAlerts( false );
       
      // operating Workbook object
      Workbooks workbooks = _xlApp.getWorkbooks();
      System.out.println( "get workbooks." );
       
      Workbook wb = workbooks.open(
                          strNewApplicationFilePath + "\\" + strNewWorkbookName,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null,
                          null);
      System.out.println("open workbook in " + wb.getFullName());
             
      wb.activate();
      System.out.println( "Workbook is activated. " );
 
      int iWorkbookCount = workbooks.getCount();
      System.out.println( "    [Workbook::Count]" + iWorkbookCount );
 
      String strTemp = wb.getName();
      System.out.println( "    [Workbook::Name]" + strTemp );
 
      strTemp = wb.getPath();
      System.out.println( "    [Workbook::Path]" + strTemp );
 
      strTemp = wb.getAuthor();
      System.out.println( "    [Workbook::Author]" + strTemp );
 
      strTemp = wb.getSubject();
      System.out.println( "    [Workbook::Subject]" + strTemp );
 
      strTemp = wb.getTitle();
      System.out.println( "    [Workbook::Title]" + strTemp );
 
      // clear up Workbooks and Workbook objects
      wb.close( new Boolean (true), null, null );
      workbooks.close();
 
      // quit application
      _xlApp.quit();
 
      System.out.println( "openWorkbook ends." );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
0
vikraman_bCommented:
all these examples belong to that url...
using Excel JAPI


import excel.*;
import java.util.*;
 
public class JExcelGuide {
     
  public JExcelGuide() {
  }
 
  public static void main(String[] args) {
         
    try {
      JExcelGuide JExcelGuide1 = new JExcelGuide();
       
      JExcelGuide1.operateRange(
                                "MyApplication",
                                "C:\\JExB\\examples",
                                "JExcelGuide.xls",
                                "JExcelGuideSheet");
    } catch(Exception e) {
      e.printStackTrace();
    } finally {
      // Release all remote objects that haven't already been garbage collected.
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
 
  /*
  * This function show how to manipulate Range object from an existing
  * Worksheet, i.e. fill out data, do calculation, invoke VBA script
  * if applicable.
  */
  public void operateRange(
                          String strNewApplicationCaption,
                          String strNewApplicationFilePath,
                          String strWorkbookName,
                          String strWorksheetName) {
    try {
      System.out.println( "operateRange starts." );
 
      Application _xlApp = new Application();
 
      // MS Office will be invoked visually
      _xlApp.setVisible( true );
       
      // If this is set to true, MS Office requires user's interaction
      _xlApp.setDisplayAlerts( false );
       
      // set caption and default path
      _xlApp.setCaption( strNewApplicationCaption );
      _xlApp.setDefaultFilePath( strNewApplicationFilePath );
       
      // operating Workbook object
      Workbooks workbooks=_xlApp.getWorkbooks();
       
      int iWorkbookCount = workbooks.getCount();
       
      Workbook wb1 = workbooks.add( null );
       
      Sheets worksheets = null;
      Worksheet ws1 = null;
       
      // get Worksheets collection
      worksheets = wb1.getWorksheets();
 
      Integer iIndex = new Integer( 1 );
      // get the 1st Worksheet(new one)
      ws1 = new Worksheet( worksheets.getItem( iIndex ) );
       
      // change the name of 1st Worksheet to "newsheet"
      ws1.setName( strWorksheetName );
       
      ws1.activate();
      System.out.println( "Activate " + strWorksheetName + " obejct." );
       
      // make Range and selected
      Range range = null;
       
      range = ws1.getRange( "B3:D6", null );
       
      range.select();
      Thread.sleep( 1000 );
       
      // set values
      Object[][] newValue = {
        { "ninja", new Boolean(false), new Double(98765.0/12345.0) },
        { "07/11/1998", new Integer(5454), new Float(22.0/7.0) },
        { new Boolean( true ), "samurai", "07/11/1999" },
        { new Integer(123), "SOAP", "Space" }
      };
      range.setValue(newValue);
      Thread.sleep( 1000 );
       
      // clear values
      range = ws1.getRange( "B3:D3", null );
       
      range.select();
      range.clearContents();
      Thread.sleep( 1000 );
       
      // set values and formula, calculate the result
      range = ws1.getRange( "A8:C8", null );
      range.select();
      int[] newValue2 = { 10, 20, 30 };
      range.setValue( newValue2 );
      Thread.sleep( 1000 );
 
      range = ws1.getRange( "D8", null );
      range.select();
      range.setValue( "=SUM(A8:C8)" );
       
      Thread.sleep( 1000 );
       
      // set range width and height
      range = ws1.getRange( "A10", null );
      range.select();
      Double dWidth = (Double)range.getColumnWidth();
      System.out.println("    [Range::DefaultColumnWidth]" + dWidth );
      double dTemp = dWidth.doubleValue();
      dTemp *= 1.5;
      dWidth = new Double(dTemp);
      range.setColumnWidth( dWidth );
 
      Double dHeight = (Double)range.getRowHeight();
      System.out.println("    [Range::DefaultRowHeight]" + dHeight );
      dTemp = dHeight.doubleValue();
      dTemp *= 1.5;
      dHeight = new Double(dTemp);
      range.setRowHeight( dHeight );
       
      Thread.sleep( 1000 );
       
      // set border weight by using Borders object
      range = ws1.getRange( "C10:D12", null );
      range.select();
       
      Borders bds = range.getBorders();
      Integer iWeight = (Integer)bds.getWeight();
      System.out.println("    [Borders::DefaultWeight]" + iWeight );
      int iTemp = iWeight.intValue();
      iTemp += 2;//maximum weight=4
      iWeight = new Integer( iTemp );
      bds.setWeight( iWeight );
       
      // set border line style by using Borders obejct
      bds.setLineStyle( new Integer( XlLineStyle.xlContinuous ) );
       
      Thread.sleep( 1000 );
 
      // set edge left border use Border object
      Integer iLineStyle=new Integer( XlLineStyle.xlDot );
       
      Border bd = bds.getItem( XlBordersIndex.xlEdgeLeft );
      bd.setLineStyle( iLineStyle );
       
      Thread.sleep( 1000 );
       
      // set Font style
      range = ws1.getRange( "B4:D5", null );
      range.select();
       
      Font ft = range.getFont();
      ft.setBold( new Boolean( true ) );
       
      ft.setItalic( new Boolean( true ) );
       
      ft.setShadow( new Boolean( true ) );
       
      ft.setUnderline( new Boolean( true ) );
       
      Thread.sleep( 1000 );
       
      // set Font color
      Double dbColor = (Double)ft.getColor();
      System.out.println("    [Font::DefaultColor]" + dbColor );
      dTemp = dbColor.doubleValue() + 32700;
      dbColor = new Double(dTemp);
      ft.setColor( dbColor );
       
      // set Font size
      Double dbSize = (Double)ft.getSize();
      System.out.println("    [Font::DefaultSize]" + dbSize );
      dTemp = dbSize.doubleValue() * 1.5;
      dbSize = new Double( dTemp );
      ft.setSize( dbSize );
       
      Thread.sleep( 1000 );
       
      // save
      wb1.setSaved( true );
      wb1.saveCopyAs( strWorkbookName );
       
      // clear up Workbooks and Workbook objects
      wb1.close( new Boolean (true), null, null );
      workbooks.close();
       
      // quit application
      _xlApp.quit();
       
      System.out.println( "operateRange ends." );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

0
DarknessShadowAuthor Commented:
Hi  petmagdy....thank you for your quick response

I tried your suggestion but it started up the Excel and closed it immediately. Therefore, I don’t know if it opened the excel file or not.

Best regards
0
petmagdyCommented:
are u sure that the excel file u r tring to open a valid excel file?
0
DarknessShadowAuthor Commented:
Hi  petmagdy

the excel.exe has a problem in my PC. therfore, i tried this comand in the command prompt:

notepad d:\Java2Excel\code\test.xls

and it works fine.

However, the following java code doesn't open the file

String[] runArgs = {"D:\\Java2Excel\\code\\test.xls"};
try{Runtime.getRuntime().exec("notepad.exe", runArgs);}
catch(Exception ex)
{
            ex.printStackTrace();
}
0
petmagdyCommented:
>> try{Runtime.getRuntime().exec("notepad.exe", runArgs);}
to be:
try{Runtime.getRuntime().exec("c:\\FullPathTo\\notepad.exe", runArgs);}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DarknessShadowAuthor Commented:
Hi.... petmagdy.

I finally have tried the following code and it worked.
try
        {
        Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\OFFICE11\\Excel.EXE D:\\Java2Excel\\code\\test.xls");
}
catch(Exception ex)
        {
            ex.printStackTrace();
        }


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.