Make
Excel Reports of JSP ( Java Web )
The following steps:
1. Create a new project , or if you already have a project that is ready to be used for the
practice of opening its first project in Netbeans .
2. Then create a new jsp files also for export to excelnya code in the usual way right-click Web Pages - Add New - select JSP and then assign a file name with lapObat ( for
example, that you want to create the report is a report drug data ) and then finish .
3. Once the file is open jsp edit the source code as follows :
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<html>
<head>
<title>Display data from MySQL</title>
</head>
<body>
<h2>Laporan Data Obat</h2>
<%
try {
//deklaration url database
String url = "jdbc:mysql://localhost:3306/sik";
Connection con = null;
Statement stat = null;
ResultSet rs = null;
//load jdbc driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, "root", "");
stat = con.createStatement();
//make query
String query = "Select * from obat";
rs = stat.executeQuery(query);
%>
<%
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "Obat.xls");
}
%>
<table border="1">
<tr>
<th>Kode Obat</th>
<th>Nama Obat</th>
<th>Harga Obat</th>
</tr>
<% while (rs.next())
{
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%
}
%>
<%
//close conection
rs.close();
stat.close();
con.close();
}
catch (Exception ex)
{
out.println ("Unable to connect to database");
}
%>
</table>
</body>
</html>
<!DOCTYPE html>
<%@page import="java.sql.*" %>
<%@page import="java.io.*" %>
<html>
<head>
<title>Display data from MySQL</title>
</head>
<body>
<h2>Laporan Data Obat</h2>
<%
try {
//deklaration url database
String url = "jdbc:mysql://localhost:3306/sik";
Connection con = null;
Statement stat = null;
ResultSet rs = null;
//load jdbc driver
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, "root", "");
stat = con.createStatement();
//make query
String query = "Select * from obat";
rs = stat.executeQuery(query);
%>
<%
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "Obat.xls");
}
%>
<table border="1">
<tr>
<th>Kode Obat</th>
<th>Nama Obat</th>
<th>Harga Obat</th>
</tr>
<% while (rs.next())
{
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
</tr>
<%
}
%>
<%
//close conection
rs.close();
stat.close();
con.close();
}
catch (Exception ex)
{
out.println ("Unable to connect to database");
}
%>
</table>
</body>
</html>
Important!!!
Adjust the database name and table name with the database and tables belonging to
friends own
4. Back On index.jsp file or files jsp friends who want given link Excel reports and edit the
source code as follows :
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Praktek Membuat Laporan Excel dari JSP (Java Web)</h1>
<h3>By. IRNAWATI</h3>
<br> <a href="lapObat.jsp?exportToExcel=YES">Laporan Excel</a> <br>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Praktek Membuat Laporan Excel dari JSP (Java Web)</h1>
<h3>By. IRNAWATI</h3>
<br> <a href="lapObat.jsp?exportToExcel=YES">Laporan Excel</a> <br>
</body>
</html>
5. Do not forget Add Lib Driver Libraries MySQL her so that his views as follows :
6. If you've tried to run the program , had I Alhamdulillah smoothly without error so that the view page is initially as follows :
If the link is clicked , the Excel report will download excelnya file , and then click OK
and if the file is opened excelnya the results as follows :
NB : if it appears a text box warning just click Yes .
the interest in his e - Book could be in line e - Book ,
there are usually looking for his project too ,
please Download Database Project + FREE ( using Netbeans IDE
7.2 and XAMPP / WAMP )
Comments
Post a Comment