Java implementation Exports Excel, Word, pdf

Source: Internet
Author: User

After a few days of learning can finally be implemented in Java to save the query results as Excel, save the page word, the HTML page saved as a PDF of the features, this is my first blog, in order to share the code, but also for the future of their own learning results will not be lost, now put these several features on the Internet.
First, the query results saved as Excel, my graduation design is the supermarket information management system, a page will show the product information, and now save the results as Excel
product.jsp page
<title>this is my excel</title>
<script type= "Text/javascript" >
function Genexcel () {
window.open ("productmanage/createxcel.jsp");
}
function Genword () {
window.open ("productmanage/creatword.jsp");
}
function Genpdf () {
window.open ("productmanage/creatpdf.jsp");
}
</script>
<body>
<form>
<input type= "button" onclick= "Genexcel ()" value= "Generate Excel" >
<input type= "button" onclick= "Genword ()" value= "Generate word" >
<input type= "button" onclick= "Genpdf ()" value= "Generate PDF" >
</form>
</body>
*************************************************
createxcel.jsp
<% @page import= "Com.bean.Productbean"%>
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ page import= "com.dao.*"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' createxcel.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >
-
<%
Response.setheader ("Content-disposition", "Attachment;filename=product.xls");
Response.setcontenttype ("application/vnd.ms-excel");
Createxcel CE = new Createxcel ();
Out.clear ();
out = Pagecontext.pushbody ();
Ce.getexcel ("Product.xls", Response.getoutputstream ());


%>

<body>

</body>
*************************************************
Createxcel.java
Package Com.dao;
Import java.io.IOException;
Import Java.io.OutputStream;
Import java.util.ArrayList;

Import Org.apache.poi.hssf.usermodel.HSSFCell;
Import Org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import Org.apache.poi.hssf.usermodel.HSSFRow;
Import Org.apache.poi.hssf.usermodel.HSSFSheet;
Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;

Import Com.bean.Productbean;

public class Createxcel {
public void Getexcel (String sheetname,outputstream output) {

int ipage = 1;
int ipagesize = 10000;
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet Sheet1 = Wb.createsheet ("Sheet1");
Hssfcellstyle SetBorder = Wb.createcellstyle ();
Centering settings
Setborder.setalignment (Hssfcellstyle.align_center);
Hssfrow row = Sheet1.createrow ((short) 0);
Set the width of the cell
Sheet1.setcolumnwidth (0, 10 * 256);
Sheet1.setcolumnwidth (1, 10 * 256);
Sheet1.setcolumnwidth (2, 10 * 256);
Sheet1.setcolumnwidth (3, 10 * 256);
Sheet1.setcolumnwidth (4, 10 * 256);
Sheet1.setcolumnwidth (5, 10 * 256);
Sheet1.setcolumnwidth (6, 10 * 256);
Sheet1.setcolumnwidth (7, 10 * 256);
Sheet1.setcolumnwidth (8, 10 * 256);
Hssfcell cell = Row.createcell (0);
Row.createcell (0). Setcellvalue ("Product number");
Row.createcell (1). Setcellvalue ("trade name");
Row.createcell (2). Setcellvalue ("Incoming price");
Row.createcell (3). Setcellvalue ("Sale");
Row.createcell (4). Setcellvalue ("category");
Row.createcell (5). Setcellvalue ("unit");
Row.createcell (6). Setcellvalue ("stock");
Row.createcell (7). Setcellvalue ("origin");
Productdao PD = new Productdao ();
ArrayList plist = Pd.getallpro (-1);
if (Plist.size () >0) {
for (int i=0;i<plist.size () -1;i++) {
row = Sheet1.createrow (i + 1);
Row.createcell (0). Setcellvalue (((Productbean) (Plist.get (i))). GETPRO_ID () + "");
Row.createcell (1). Setcellvalue (((Productbean) (Plist.get (i))). Getpro_name () + "");
Row.createcell (2). Setcellvalue (((Productbean) (Plist.get (i))). Getc_price () + "");
Row.createcell (3). Setcellvalue (((Productbean) (Plist.get (i))). GetPrice () + "");
Row.createcell (4). Setcellvalue (((Productbean) (Plist.get (i))). Getpro_varid () + "");
Row.createcell (5). Setcellvalue (((Productbean) (Plist.get (i))). Getpro_unit () + "");
Row.createcell (6). Setcellvalue (((Productbean) (Plist.get (i))). Getpro_count () + "");
Row.createcell (7). Setcellvalue (((Productbean) (Plist.get (i))). Getpro_field () + "");


}
}

try {
Output.flush ();
Wb.write (output);
Output.close ();
} catch (IOException e) {
TODO auto-generated Catch block

}

}
}
***********************************************
The following generated word PDF is left in the next article, and the package used is placed in the attachment

    • Jar package. RAR (7.1 MB)
    • Download number of times: 4

Java implementation Exports Excel, Word, pdf

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.