Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

WebLogic Tutorial: "Integrating Apache Poi in WebLogic Server"

The Apache Jakarta POI project provides components for the access and generation of Excel documents

for (int i=1;resultSet.next(); i++)
         {
     row=sheet1.createRow(i);
row.createCell((short)0).setCellValue(resultSet.getString(1));
row.createCell((short)1).setCellValue(resultSet.getString(2));
row.createCell((short)2).setCellValue(resultSet.getString(3));
row.createCell((short)3).setCellValue(resultSet.getString(4));
row.createCell((short)4).setCellValue(resultSet.getString(5));
}

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
In this section we will store an Excel spreadsheet in a MySQL database table with the Apache POI API. The example Excel document stored is the spreadsheet, catalog.xls, which was generated in the previous section. The Excel spreadsheet is stored in MySQL table Catalog. Drop the Catalog table from which the Excel document was generated in the previous section with the MySQL command:

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.*,
org.apache.poi.hssf.usermodel.*, java.sql.*,
java.io.*,javax.naming.InitialContext"%>

As in the previous section, obtain a JDBC connection from the MySQL datasource:

InitialContext initialContext = new InitialContext();
       javax.sql.DataSource ds = (javax.sql.DataSource)
       initialContext.lookup("MySqlDS");
    java.sql.Connection conn = ds.getConnection();

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
VARCHAR(25),Section VARCHAR(25),Edition VARCHAR(25),Title Varchar(125),Author Varchar(25))";

stmt.execute(createTable);

Create a POIFSFileSystem to read the Excel document:

File catalogExcel=new File("C:/ExcelWebLogic/catalog.xls");
FileInputStream inputStream=new FileInputStream(catalogExcel);
POIFSFileSystem fileSystem=new POIFSFileSystem(inputStream);

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();
HSSFRow row=(HSSFRow)rowIterator.next();

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+"\
'"+","+"\'"+journal
+"\'"+","+"\'"+section+"\'"+","+"\'"+edition+"\'"+","+"\'"+title+"\
'"+","+"\'"+author+"\'"+")";
stmt.execute(exceltable);

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
In this tutorial, an Excel spreadsheet was generated from a MySQL database table and subsequently the spreadsheet was stored in a database table. The WebLogic Server facilitates the conversion from database table to Excel spreadsheet and from spreadsheet to database table by providing a datasource and a J2EE application server to run a JSP application.

More Stories By Deepak Vohra

Deepak Vohra is a Sun Certified Java 1.4 Programmer and a Web developer.

More Stories By Ajay Vohra

Ajay Vohra is a senior solutions architect with DataSynapse Inc.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
bob kennelly 08/19/06 05:49:54 PM EDT

Hello this is just what i've been looking for, it's a very helpfull article!

In the article there is a statement saying "see the Reference area" to find the JSP code, can anyone tell me where i can find the reference section please?

Thanks very much!

SYS-CON Italy News Desk 02/19/06 03:33:36 PM EST

The Apache Jakarta POI project provides components for the access and generation of Excel documents. The POI HSSF API is used to generate Excel Workbooks and to add Excel spreadsheets to a workbook. An Excel spreadsheet consists of rows and cells. The layout and fonts of a spreadsheet are also set with the POI HSSF API.

@ThingsExpo Stories
"MobiDev is a Ukraine-based software development company. We do mobile development, and we're specialists in that. But we do full stack software development for entrepreneurs, for emerging companies, and for enterprise ventures," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
We build IoT infrastructure products - when you have to integrate different devices, different systems and cloud you have to build an application to do that but we eliminate the need to build an application. Our products can integrate any device, any system, any cloud regardless of protocol," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
Multiple data types are pouring into IoT deployments. Data is coming in small packages as well as enormous files and data streams of many sizes. Widespread use of mobile devices adds to the total. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists looked at the tools and environments that are being put to use in IoT deployments, as well as the team skills a modern enterprise IT shop needs to keep things running, get a handle on all this data, and deliver...
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
New competitors, disruptive technologies, and growing expectations are pushing every business to both adopt and deliver new digital services. This ‘Digital Transformation’ demands rapid delivery and continuous iteration of new competitive services via multiple channels, which in turn demands new service delivery techniques – including DevOps. In this power panel at @DevOpsSummit 20th Cloud Expo, moderated by DevOps Conference Co-Chair Andi Mann, panelists examined how DevOps helps to meet the de...
When growing capacity and power in the data center, the architectural trade-offs between server scale-up vs. scale-out continue to be debated. Both approaches are valid: scale-out adds multiple, smaller servers running in a distributed computing model, while scale-up adds fewer, more powerful servers that are capable of running larger workloads. It’s worth noting that there are additional, unique advantages that scale-up architectures offer. One big advantage is large memory and compute capacity...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that Silicon India has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Published in Silicon Valley, Silicon India magazine is the premiere platform for CIOs to discuss their innovative enterprise solutions and allows IT vendors to learn about new solutions that can help grow their business.
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
SYS-CON Events announced today that TMC has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo and Big Data at Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Global buyers rely on TMC’s content-driven marketplaces to make purchase decisions and navigate markets. Learn how we can help you reach your marketing goals.