Pplease anybody post a jsp program that reads data from microsoft excel and displays it in a table formatin the browser....(using apache tomcat poi) ......my project is that i should enter an excel file name and it should read that file and display in the form of table in webrowser by using jsp,apachetomcat,poi please.please.please....... post as soon as possible.......
thanks in advance......hoping for your earliest replies
Hi Friend,
Try the following code:
<%@page import="java.io.*"%> <%@page import="java.util.*"%> <%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%> <%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%> <%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%> <%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%> <table border="1"> <% short a=0; short b=1; short c=2; short d=3; int i=0; String value1="", value2="",value3=" ", value4=""; String filename ="C:/hello.xls"; if (filename != null && !filename.equals("")) { try{ FileInputStream fs =new FileInputStream(filename); HSSFWorkbook wb = new HSSFWorkbook(fs); for (int k = 0; k < wb.getNumberOfSheets(); k++){ int j=i+1; HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows(); for (int r = 0; r < rows; r++){ HSSFRow row = sheet.getRow(r); int cells = row.getPhysicalNumberOfCells(); out.write("<br>"); HSSFCell cell1 = row.getCell(a); value1 = cell1.getStringCellValue(); HSSFCell cell2 = row.getCell(b); value2 = cell2.getStringCellValue(); HSSFCell cell3 = row.getCell(c); value3 = cell3.getStringCellValue(); HSSFCell cell4 = row.getCell(d); value4 = cell4.getStringCellValue(); %> <tr><td><%=value1%></td><td><%=value2%></td><td><%=value3%></td><td><%=value4%></td></tr> <% } i++; } } catch(Exception e){ System.out.println(e); } } %> </table>
Thanks
Thanks for your valuable code...But i have two problems with the above code.
1.I have different number of sheets and in each sheet number of columns(i.e. we have fistname,lastname,id in 1 sheet and firstname,lastname,fullname,id in another sheet).the above code displays only concatenation of two sheets cells having same number of columns.....
2.next it also does not display if there is nothing in a cell....i.e., if a particular row in an excel file does not have anything in it...then even the next rows are also not displayed.....
My requirement is to display all the data from all sheets (including null and not null)in the form of tables....
please send the revised code..... Thank you very very much.....
Ads