First i made ontology using protege.then to extract its construct e.g.... 
classes,properties i use jena and sparql...now i want to save my result in 
excel file...how can i do this.i try the following code but it am not getting 
proper result.Kindly please help me.


import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;



public class ExcelShet {
 // protected FileOutputStream workbook;
 protected HSSFWorkbook workbook ;
   protected HSSFWorkbook workbook12 ;
   protected HSSFSheet sheet;
  protected HSSFSheet sheet12;
  protected Row header;
  protected Model model;
  protected String queryString;
  protected  Query query;
  protected QueryExecution qe;
  protected ResultSet results ;
public ExcelShet(){
  
   workbook = new HSSFWorkbook();
   sheet = workbook.createSheet("Sample sheet");
   
  /*Map<String, Object[]> data = new HashMap<String, Object[]>();
  data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
  data.put("2", new Object[] {1d, "John", 1500000d});
  data.put("3", new Object[] {2d, "Sam", 800000d});
  data.put("4", new Object[] {3d, "Dean", 700000d});
   
  Set<String> keyset = data.keySet();
  int rownum = 0;
  for (String key : keyset) {
      Row row = sheet.createRow(rownum++);
      Object [] objArr = data.get(key);
      int cellnum = 0;
      for (Object obj : objArr) {
          Cell cell = row.createCell(cellnum++);
          if(obj instanceof Date) 
              cell.setCellValue((Date)obj);
          else if(obj instanceof Boolean)
              cell.setCellValue((Boolean)obj);
          else if(obj instanceof String)
              cell.setCellValue((String)obj);
          else if(obj instanceof Double)
              cell.setCellValue((Double)obj);
          
      }
  }*/
          try {
            FileOutputStream out = 
                    new FileOutputStream(new File("D:/new.xls"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");
             
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
     
  
  
  
  }// end constructor
void jena_SPAPQL_Query_Ontology1(){
 workbook12 = new HSSFWorkbook();
    sheet12 = workbook12.createSheet("Calculate Simple Interest");
   
try{

System.out.println("Using Jena API with SPARQL Queries");
//Open the  graph from the file system
FileInputStream in = new FileInputStream(new File("D:/Pizza.owl"));

//Create an empty in-memory model and populate it from the graph
model = ModelFactory.createMemModelMaker().createModel(null);
model.read(in,null); // null base URI, since model URIs are absolute
//To extract equivalent classes
//String queryString= " PREFIX rdf: 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: 
<http://www.w3.org/2002/07/owl#> PREFIX xsd: 
<http://www.w3.org/2001/XMLSchema#> PREFIX rdfs: 
<http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE{  ?s owl:equivalentClass 
?o} ";
 queryString  ="PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX xsd: 
<http://www.w3.org/2001/XMLSchema#> PREFIX rdfs: 
<http://www.w3.org/2000/01/rdf-schema#> SELECT ?subject ?object WHERE { 
?subject rdfs:subClassOf ?object}";
 query = QueryFactory.create(queryString);

//Execute the query and obtain results
 qe = QueryExecutionFactory.create(query, model);
 results = qe.execSelect();
 header = sheet12.createRow(5);
   header.createCell(0).setCellValue(queryString);
   
//Output query results
//ResultSetFormatter.out(System.out, results, query);
//ResultSetFormatter.outputAsXML(System.out, results, query); //to produce 
result as XML to console
//File f= new File("D:/Jena_output.txt");
  //  FileOutputStream fo= new FileOutputStream(f);
    //FileOutputStream fo = new FileOutputStream("f");

 FileOutputStream out = 
                new FileOutputStream(new File("D:/formula.xls"));
        workbook12.write(out);
        
        System.out.println("Excel written successfully..");
ResultSetFormatter.out(out, results,query);// to produce result in a file
  out.close();
 

   
//Important - free up resources used running the query
qe.close();

System.out.println("The End of Using Jena API.");
}

catch(Exception e){

e.printStackTrace();
System.out.println("In Exception.");
System.out.println("Problem is " + e.toString());
}
}
 
}





Main.java




public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Ont a= new Ont();
a.Jena_SPAPQL_Query_Ontology();
     ExcelShet a1 = new ExcelShet();
     ExcelShet a2 = new ExcelShet();
     a2.jena_SPAPQL_Query_Ontology1();
}

}

Reply via email to