|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Integration WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"
The Apache Jakarta POI project provides components for the access and generation of Excel documents
By: Deepak Vohra; Ajay Vohra
Feb. 19, 2006 02:30 PM
for (int i=1;resultSet.next(); i++) Create a FileOutputStream to output the Excel spreadsheet to an XLS file. An XLS file represents an Excel spreadsheet: FileOutputStream output=new FileOutputStream(new File("c:/excel/catalog.xls")); Output the Excel spreadsheet to an XLS file: wb.write(output); The ExcelWebLogic.jsp JSP used to generate an Excel spreadsheet is available in the References section. To run the ExcelWebLogic.jsp JSP in the WebLogic Server, copy the JSP to the <weblogic81>\samples\server\examples\build\mainWebApp directory. Run the JSP with the URL http://localhost:7001/ExcelWebLogic.jsp. An Excel spreadsheet gets generated which may be opened in Excel (http://office.microsoft.com/en-us/FX010858001033.aspx) or the Excel Viewer tool (http://office.microsoft.com/en-us/assistance/HA011620741033.aspx).
Storing an Excel Document in a Database Table MySQL>DROP table Catalog; Develop a JSP application to store the example Excel document in the MySQL database. In the JSP application import the Apache POI packages org.apache.poi.poifs.filesystem and org.apache.poi.hssf.usermodel. The org.apache.poi.poifs.filesystem package has classes to create an Excel workbook and the org.apache.poi.hssf.usermodel package has classes that represent an Excel workbook, spreadsheet, spreadsheet row, and row cell.
<%@ page import="org.apache.poi.poifs.filesystem.*, As in the previous section, obtain a JDBC connection from the MySQL datasource:
InitialContext initialContext = new InitialContext(); Create java.sql.Statement from the JDBC connection: Statement stmt=conn.createStatement(); Create a MySQL table in which the Excel spreadsheet will be stored:
String createTable="CREATE TABLE Catalog(CatalogId VARCHAR(25) PRIMARY KEY,Journal Create a POIFSFileSystem to read the Excel document:
File catalogExcel=new File("C:/ExcelWebLogic/catalog.xls"); Obtain a HSSF workbook from the POIFSFileSystem: HSSFWorkbook wb=new HSSFWorkbook(fileSystem); Obtain an Excel spreadsheet from the Excel workbook: HSSFSheet sheet1=wb.getSheet("sheet1"); Iterate over the rows in the spreadsheet with a row iterator:
java.util.Iterator rowIterator=sheet1.rowIterator(); Retrieve the row cell values for each of the rows. For example, the CatalogId row cell value is retrieved with: String catalogId=row.getCell((short)0).getStringCellValue(); Add a table row for each of the rows in the Excel spreadsheet:
String exceltable="INSERT INTO Catalog VALUES("+"\'"+catalogId+"\ Copy the POIWebLogic.jsp to the <weblogic81>\samples\server\examples\build\mainWebApp directory. Run the JSP with the URL http://localhost:7001/POIWebLogic.jsp. A MySQL database table gets generated from the Excel spreadsheet. The POIWebLogic.jsp used to generate a database table from an Excel spreadsheet is available in the References section.
Conclusion YOUR FEEDBACK
BEA WEBLOGIC LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||