JSP exports an Excel file, jsp exports an excel file

Source: Internet
Author: User
Tags tld

JSP exports an Excel file, jsp exports an excel file
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<% @ Taglib prefix = "ai" uri = "/WEB-INF/tag/ai-tags.tld" %>
<% @ Taglib uri = "/WEB-INF/tag/ElTag. tld" prefix = "el" %>
<% @ Page import = "java. text. SimpleDateFormat" %>
<% @ Page import = "java. util. Date" %>
<%
SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String dateStr = format. format (new Date ());
Response. setContentType ("Application/msexcel; charset = UTF-8 ");
System. out. println ("dateStr =" + dateStr );
String fileName = "promotion fee" + dateStr;
FileName = new String (fileName. getBytes ("GBK"), "iso-8859-1 ");
Response. setHeader ("Content-disposition", "attachment; filename =" + fileName + ". xls ");
%>
<Div class = "big_main" id = "sos_list_div" style = "width: 1400px; overflow: hidden">
<Div class = "query_table">
<Table width = "100%" border = "1" cellspacing = "1">
<Tr align = "center" valign = "middle">
<Td height = "35" colspan = "14" align = "center" style = "border-bottom: 2px # ddd solid "> <span class =" STYLE4 STYLE6 ">$ {title} promotion fee </span> </td>
</Tr>
<Tr align = "center" valign = "middle">
<Td height = "35" colspan = "2"> statistical time: $ {sj} </td>
<Td width = "20%" colspan = "10"> & nbsp; </td>
<Td width = "7%" colspan = "2"> <span class = "STYLE4"> total sales fee: $ {totalAmount} RMB </td>
</Tr>
</Div>
<Div class = "query_table" style = "margin-top: 0px">
<Table width = "100%" border = "0" cellspacing = "1">
<Tr align = "center" valign = "middle" bgcolor = "# e3f0f6" style = "border-top: 1px solid # d1d3d5;">
<Td width = "6%" height = "35"> Sales Promotion Month </td>
<Td width = "6%" height = "35"> date of acceptance </td>
<Td width = "7%"> activity code </td>
<Td width = "6%" height = "35"> activity name </td>
<Td width = "6%" height = "35"> sales quantity </td>
<Td width = "9%" height = "35"> promotion fee (RMB) </td>
<Td width = "7%" height = "35"> salesperson Code </td>
<Td width = "7%" height = "35"> salesperson </td>
<Td width = "6%" height = "35"> developer id </td>
<Td width = "7%" height = "35"> developer name </td>
<Td width = "6%" height = "35"> store code </td>
<Td width = "10%"> store name </td>
<Td width = "6%" height = "35"> <span class = "STYLE4"> company code </span> </td>
<Td width = "8%" height = "35"> <span class = "lan_zi"> company name </span> </td>
</Tr>

<! -- Display data cyclically -->
<C: forEach items = "$ {pageInfo. result}" var = "v" varStatus = "vs" begin = "0" step = "1">
<Tr>
<Td height = "35" align = "center" valign = "middle" >$ {v. protoMonth} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. dealTime} </td>
<Td align = "center" valign = "middle" >$ {v. productId} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. productName} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. sellNum} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. protoworkflow} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. optOperId} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. optOperName} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. devId} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. devName} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. storeId} </td>
<Td align = "center" valign = "middle" >$ {v. storeName} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. componentId} </td>
<Td height = "35" align = "center" valign = "middle" >$ {v. componentName} </td>
</Tr>
</C: forEach>
</Table>
</Div>
<! ------- Query the end of the table area -------->
</Div>
How can I import and export data to an Excel file through jsp? What should I do?

Public static void crExcel (List list, String absoluteFileString)
Throws Exception {

// Font format
WritableFont wfc0 = new WritableFont (WritableFont. ARIAL, 22, WritableFont. BOLD, false, UnderlineStyle. NO_UNDERLINE, jxl. format. Colour. BLACK );
WritableCellFormat wcfFC0 = new WritableCellFormat (wfc0 );
WcfFC0.setAlignment (jxl. format. Alignment. CENTRE );
WcfFC0.setVerticalAlignment (jxl. format. verticalignment. CENTRE );
WcfFC0.setBorder (Border. ALL, BorderLineStyle. THIN, jxl. format. Colour. GRAY_25 );
// Header font
WritableFont wfc8 = new WritableFont (WritableFont. ARIAL, 12, WritableFont. BOLD, false, UnderlineStyle. NO_UNDERLINE, jxl. format. Colour. BLACK );
WritableCellFormat wcfFC8 = new WritableCellFormat (wfc8 );
WcfFC8.setAlignment (jxl. format. Alignment. CENTRE );
WcfFC8.setVerticalAlignment (jxl. format. verticalignment. CENTRE );
WcfFC8.setBorder (Border. ALL, BorderLineStyle. THIN, jxl. format. Colour. GRAY_25 );
WritableFont wfc1 = new WritableFont (WritableFont. ARIAL, 10, WritableFont. NO_BOLD, false, UnderlineStyle. NO_UNDERLINE, jxl. format. Colour. BLACK );
WritableCellFormat wcfFC1 = new WritableCellFormat (wfc1 );
WcfFC1.setAlignment (jxl. format. Alignment. CENTRE );
WcfFC1.setBorder (Border. ALL, BorderLineStyle. THIN, jxl. format. Colour. GRAY_25 );

// Create an excel file
OutputStream OS = new FileOutputStream (absoluteFileString );

String readPath = I18nMessages. getText ("efine. excel. dir ");

InputStream input = new FileInputStream (readPath
+ "FKBalanceReport. x... the remaining full text>

No, let's take a look at the jsp page to export excel files.

1. Put the data (which you implement) in scopt first
2. Add a button to the page and request the following jsp

// The guide package is incomplete. These poi packages are enough. You can export other lists, streams, and other packages by yourself.
<% @ Page pageEncoding = "UTF-8" %>
<% @ Page import = "org. apache. poi. hssf. usermodel. HSSFWorkbook" %>
<% @ Page import = "org. apache. poi. hssf. usermodel. HSSFSheet" %>
<% @ Page import = "org. apache. poi. hssf. usermodel. HSSFRow" %>
<% @ Page import = "org. apache. poi. hssf. usermodel. HSSFCell" %>
<% @ Page import = "org. apache. poi. hssf. usermodel. HSSFCellStyle" %>

<Html>
<Head>
</Head>
<Body>
<%
String result = "...";

// System. out. println ("in export. jsp ~~ Result = "+ result );
Request. getSession (true );
List contracts = (List) session. getAttribute ("contractsss"); // This is where data is stored everywhere.

// System. out. println (contracts = null? "Null": contracts. size ());
If (contracts! = Null ){

String xlsFile = "" // put it in a place first. You can find a temporary path on the server and use it for downloading.

// System. out. println (xlsFile );
HSSFWorkbook workbook = new HSSFWorkbook (); // a new xls document
HSSFSheet sheet = workbook. createSheet (); // create a sheet
Workbook. setSheetName (0, "firstSheet", HSSFWorkbook. ENCODING_UTF_16); // The sheet name. The method name is simple!

HSSFRow row0 = sheet. createRow (short) 0); // get a row as the header

HSSFCell cell1 = row0.createCell (short) 0); // The first column. The (shot) 0 is id
Cell1.setCellType (H... the remaining full text>

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.