Tuesday, March 28, 2017

Extracting Excel Content as Text

The following code extracts text content in an xlsx file

package com.finra;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestExcelExtractor {

	public static void main(String [] args) throws Exception {
		
        FileInputStream file = new FileInputStream(new File("c:/users/dgandikota/Test.xlsx"));

        
        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);
    
        XSSFExcelExtractor excelExtractor = new XSSFExcelExtractor(workbook);
        String allTxt = excelExtractor.getText();
        System.out.println(allTxt);
        allTxt = allTxt.replaceAll("\t", "|");
        allTxt = allTxt.replace("null", "");
        if (new File("c:/users/dgandikota/excelextract.txt").exists()) {
        	new File("c:/users/dgandikota/excelextract.txt").delete();
        }
        PrintWriter pw = new PrintWriter(new FileOutputStream(new File("c:/users/dgandikota/excelextract.txt")));
        pw.print(allTxt);
        pw.close();
//        System.out.println(allTxt);
        
	}
}

The sheet names are output by default

The end of sheet is found by the absence field separator (eg. line.indexOf("|") == -1)

REST Example with Headers using TomEE Plus

apache-tomee-plus-1.7.4 comes with out of the box REST


Tools to test REST services

  • Firefox-Poster Add-On
  • Chrome - postman?

    Create the following class and deploy inside a war on TomEE-Plus

    package com.apache.rest;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
     @Path("/greeting")
     public class GreetingService {
         @GET
         @Produces( { MediaType.TEXT_XML })
          @Path("{id1}/{id2}")
         public String message(@PathParam("id1") String id1, @PathParam("id2") String id2,  @Context HttpHeaders httpHeaders) {
    //called as http://localhost/greeting/{id1}/{id2}
    //http://localhost//greeting/3/2
           /** how to get specific header info? **/
    //        String cacheControl = httpHeaders.getRequestHeader("Cache-Control").get(0);
    //         System.out.println("Cache-Control: "+cacheControl);
     /** get list of all header parameters from request **/
     Set headerKeys = httpHeaders.getRequestHeaders().keySet();
     for(String header:headerKeys){
           if (httpHeaders.getRequestHeader(header)!= null && httpHeaders.getRequestHeader(header).size() > 0 )
              System.out.println(header+":"+httpHeaders.getRequestHeader(header).get(0));
     }
             return "Hi REST!" + id1 + " " + id2;
         }
    
         @POST
    //called as http://localhost//greeting?message=test
         public String lowerCase(final String message) {
             return "Hi REST!".toLowerCase();
         }
     }
    
    

    More at http://www.vogella.com/tutorials/REST/article.html