Friday 31 August 2012

Read xls or excel file from server and parse xls or excel file


CHANGE THIS BOLD URL WITH YOUR FILE ADDRESS WITH SERVER ADDRESS AND THEN JUST COPY PASTE THE CODE TO JAVA FILE.


NOTE : U MUST HAVE TO ADD JAR FILE TO YOUR PROJECT CLASS PATH FROM THIS http://jexcelapi.sourceforge.net/  DOWN LOAD JAR FILE FROM HERE IF JAR IS NOT AVAILABLE ON THIS URL THEN SEARCH JXL JAR AND DOWNLOAD IT.

import java.io.IOException; import jxl.Cell; import jxl.CellType; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import java.net.URLConnection; public class ReadExcel { public void read() throws IOException { java.net.URL url = new java.net.URL("http://www.XYZ.org/alphaguru.xls"); URLConnection conn = url.openConnection(); conn.setRequestProperty("Connection","Keep-Alive"); conn.setConnectTimeout(10000); System.out.println("value of sTring is:"+conn.getInputStream().toString()); Workbook w; try { w = Workbook.getWorkbook(conn.getInputStream()); // Get the first sheet Sheet sheet = w.getSheet(0); // Loop over first 10 column and lines for (int j = 0; j < sheet.getColumns(); j++) { for (int i = 0; i < sheet.getRows(); i++) { Cell cell = sheet.getCell(j, i); if (cell.getType() == CellType.LABEL) { System.out.println("I got a label "+ cell.getContents()); } if (cell.getType() == CellType.NUMBER) { System.out.println("I got a number "+ cell.getContents()); } } } } catch (BiffException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { ReadExcel test = new ReadExcel(); test.read(); } }

No comments:

Post a Comment