Struts Development Practice - How to Call Excel

zhaozj2021-02-16  52

(3) Struts Development Practice - How to Call Excel

1. First go to http://jakarta.apache.org/poi/ website to download the POI package. The version I use is: JAKARTA-POI-1.9.0-DEV-20030219. There is also a corresponding document on the website, you can learn.

2. Place the downloaded package in a web-inf / lib directory.

3. Write an Excel's action, Example Code is as follows:

Package test;

Import org.apache.struts.action. *;

Import java.io. *;

Import javax.servlet. *;

Import org.apache.poi.hssf.usermodel. *;

Import org.apache.poi.hssf.util. *;

/ **

* Write an Excel file

* /

Public Class TestexceLAction

EXTENDS ACTION {

Private static final string content_type = "Application / VND.MS-Excel";

Public ActionForward Perform (ActionMApping Mapping, Actionform Form,

HTTPSERVLETREQUEST REQUEST,

HttpservletResponse response) throws oException,

Servletexception {

Response.setContentType (Content_Type);

ActionerroS Errors = new actionerrors ();

Try {

//content

Try {

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"

HSSFFOOTER.NUMPAGES ());

ServletOutputStream OS = response.getOutputStream ();

Wb.write (OS);

Os.flush ();

}

Catch (Exception E) {

System.out.println ("ERROR in JSP");

}

Return NULL;

}

Catch (throwable e) {

E.PrintStackTrace ();

Actionerror Error = New ActionError (E.getMessage ());

Errors.Add (ActionerRors.global_Error, Error);

}

SaveerRors (Request, Errors);

Return New ActionForward (mapping.getinput ());

}

}

4. Call this TESTEXCELACTION is OK.

转载请注明原文地址:https://www.9cbs.com/read-21938.html

New Post(0)