Page file <% @ Taglib URI = "/ Web-inf / struts-html.tld" prefix = "html"%> <% @ page language = "java" contenttype = "text / html; charSet = GBK"%>
html: form> html: html>
Function DownloadExcel () {location.href = "http:// localhost: 8080 / downfile / download / excel.do";
script>
Java files Action
Package com.action;
Imports.Apache.struts.Apache.Struts. *; Import javax.servlet.http. *; import java.io. *; import com.bean. *; import org.apache.log4j. *; import org.apache.struts.upload. *;
/ ** *
Title: p> * *
description: p> * *
Copyright: Copyright (c) 2004 p> * *
Company: P > * * @Author NOT Attributable * @version 1.0 * /
Public Class DownFile Extends action {public static logger logger = logger.getlogger (downfile.class);
Public ActionForward Execute (ActionMapping Mapping, ActionMApping Mapping, Actionform Form, HttpservletRequest Request, httpservletResponse response "throws exception {actionForward myforward = null; try {
String myAction = mapping.getParameter (); if ( "EXCEL" .equalsIgnoreCase (myAction)) {myForward = performDownExcel (mapping, form, request, response);}} catch (Exception e) {throw e;} return myForward;} Public ActionForward PerformDowntxt (ActionMapping Mapping, Actionform Form, HttpservletRequest Request, httpservletResponse response) throws exception {
String webpath = System.getProperty ( "DOWNFILE.WEBPATH"); System.out.println ( "webpath:" webpath); System.out.println ( "LOG4J.PROPERTY" System.getProperty ( "LOG4J.PROPERTY") ); Logger.info (Request.getServletPath ()); OutputStream OS = response.getOutputStream (); // acquired output flow // string filename = contextPath "/system.txt"; //system.out.println (FileName );
Try {system.out.println ("success"); logger.debug ("success"); string fname = "test"; // txt file name response.reset (); // Empty output stream Response.setHeader (" Content-disposition "," attachment; filename = " fname " .txt "); // Set the output file header response.setContentType (" unknown "); // Define the output type TXTBean TB = new txtbean (); TB .exporttxt (os); // Call Generate TXT file bean} catch (exception e) {system.out.println (e);} finally {os.close ();
Return mapping.findforward ("display");}
public ActionForward performDownExcel (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {OutputStream os = response.getOutputStream (); // obtaining the output stream try {String fname = "test"; // txt file name response.reset (); // Empty output stream Response.setContentType ("Application / VND.MS-Excel"); // Defines Output Type Excelbean EB = New Excelbean (); Eb.Expordexcel (OS); // Call Generate Excel file bean System.Setout (New PrintStream (OS)); Os.Flush ();
} catch (exception e) {system.out.println (e);} finally {os.close ();} return mapping.findforward ("display");}
public ActionForward performUpLoadExcel (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {System.out.println ( "dafdsafsa"); ExcelForm excelForm = (ExcelForm) form; // output stream to obtain
Try {formfile file = excelForm.Getexcelfile (); system.out.println ("------" new string (file.getFileName (). getBytes ("ISO8859_1"), "GB2312"); JXL. Workbook wb = jxl.Workbook.getWorkbook (file.getinputstream ());
JXL.SHEET Sheet = wb.getsheet (0); System.out.Println ("OK" Sheet.getcell (0,0) .getContents ());} catch (Exception E) {System.out.Println (E );} Return maping.findforward ("display");}}}}}}} The production of Excel has code package com.bean;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel. HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf. Util.hssfcolor; import org.apache.poi.hssf.usermodel.hssffooter; import org.apache.poi.hssf.usermodel.hssfheader; import java.io. *;
Public class excelbean {
Public void ExpordExcel (OutputStream OS) throws exception {
HSSFWORKBOOK WB = New HSSFWORKBOOK ();
HSSFSHEET Sheet = Wb.createsheet ("New Sheet"); hssfheader header = sheet.getHeader ();
Header.SetCenter ("Salary Report);
HSSFrow Row1 = Sheet.Createrow ((Short) 0);
HSSFCell Cell11 = Row1.createcell ((Short) 0);
Cell11.setencoding (hssfcell.encoding_utf_16);
Cell11.setcellValue ("Number");
HSSFCell Cell12 = Row1.createcell ((Short) 1);
Cell12.setencoding (hssfcell.encoding_utf_16);
Cell12.SetCellvalue ("Department");
HSSFCell Cell13 = Row1.createcell ((Short) 2);
Cell13.setencoding (hssfcell.encoding_utf_16);
Cell13.setcellValue ("Name");
HSSFCell Cell14 = Row1.createcell ((Short) 3);
Cell14.setencoding (hssfcell.encoding_utf_16);
Cell14.setcellValue ("Standard Wages");
HSSFCell Cell15 = Row1.createcell ((Short) 4);
Cell15.setencoding (hssfcell.encoding_utf_16);
Cell15.setcellValue ("Basic Wage"); HSSFCell Cell16 = Row1.createcell ((Short) 5);
Cell16.setencoding (hssfcell.encoding_utf_16);
Cell16.setcellValue ("Post pay");
HSSFCell Cell17 = Row1.createcell ((Short) 6);
Cell17.setencoding (hssfcell.encoding_utf_16);
Cell17.setcellValue ("bonus");
Sheet.setgridsprinted (TRUE);
HSSFFOOTER FOOTER = Sheet.Getfooter ();
Footer.setright ("Page" HSSFFOOTER.PAGE () "of"
HSSFFOTER.NUMPAGES ()); WB.WRITE (OS);
}
Public static void main (String [] args) {
FileOutputStream Fos = NULL; try {excelbean eb = new Excelbean (); fos = new fileoutputstream ("test.xls"); Eb.Expordexcel (FOS);
} catCH (Exception E) {E.PrintStackTrace ();} finally {try {fos.close ();} catch (exception e) {E.PrintStackTrace ();}}}}