Thursday, 20 August 2015

Get Microsoft Office/ Excel Name Function in Java

Retrieve Microsoft Excel Name and Office Path In JAVA


I have implemented an office name parsing function to reterieve which Office is installed in our machine. This code is written in Java and can be used to Automate Excel .. 

This function Performs Following Steps :

1) Find Office Excel Path Dynamically.
2) Open Excels
3) Parse the path to find which Excel is installed.

Note:- I have used parsing for Excel 2013, Excel 2010, Excel 2007 you can extend it for other office Names

public static String getofficeName()
    {
    Process p;
try {
p = Runtime.getRuntime().exec
     (new String [] { "cmd.exe", "/c", "assoc", ".xls"});

           BufferedReader input =
             new BufferedReader
               (new InputStreamReader(p.getInputStream()));
           String extensionType = input.readLine();
           input.close();
           // extract type
           if (extensionType == null) {
             System.out.println("no office installed ?");
             System.exit(1);
           }
           String fileType[] = extensionType.split("=");

           p = Runtime.getRuntime().exec
             (new String [] { "cmd.exe", "/c", "ftype", fileType[1]});
           input =
             new BufferedReader
               (new InputStreamReader(p.getInputStream()));
           String fileAssociation = input.readLine();
           // extract path
           String officePath = fileAssociation.split("=")[1];
           officePath = officePath.split("/")[0];
           System.out.println(officePath);
   
           if( officePath.contains("Root"))
  {
            String[] officePath1 = officePath.split("Root\\\\", 2);
            a = officePath1[1];
            a  = a.split("\\\\")[0];

            if (a.equalsIgnoreCase("Office15"))
            {
            OfficeName = "Excel 2013";
           
            else if (a.equalsIgnoreCase("Office14"))

    {

    OfficeName = "Excel 2010";
   
            
        else if (a.equalsIgnoreCase("Office12"))

    {

    OfficeName = "Excel 2007";
    }
    }
    else
    {
    String[] officePath2 = officePath.split("Office\\\\", 2);
    a = officePath2[1];
    a  = a.split("\\\\")[0];

    if (a.equalsIgnoreCase("Office14"))

    {

    OfficeName = "Excel 2010";
    }
   
    else if (a.equalsIgnoreCase("Office15"))
        {
        OfficeName = "Excel 2013";
       
    else if (a.equalsIgnoreCase("Office12"))

    {

    OfficeName = "Excel 2007";
    }
   
    }

      }catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
return OfficeName;

 }
    

No comments:

Post a Comment

Get Microsoft Office/ Excel Name Function in Java

Retrieve Microsoft Excel Name and Office Path In JAVA I have implemented an office name parsing function to reterieve which Office is in...

Get Counted