Implementing the Java Export Excel table

Source: Internet
Author: User

Implement Java export of Excel table 1, projects need to import packages

Where Jxl.jar is the core package that operates the Excel table, and the other packages are struts2 packages


2, the structure of the project



Excelaction.java

Package Com.jxl.action;import Java.io.InputStream;  Import Java.io.unsupportedencodingexception;import Com.jxl.service.excelserviceimpl;import Com.jxl.service.iexcelservice;public class Excelaction {    private String fileName;    Private InputStream Excelstream;      public string GetFileName () {    try {              fileName = new String (Filename.getbytes (), "iso8859-1");          } catch ( Unsupportedencodingexception e) {              e.printstacktrace ();          }          return fileName + ". xls";} public void Setfilename (String fileName) {this.filename = FileName;} Public InputStream Getexcelstream () {return excelstream;} public void Setexcelstream (InputStream excelstream) {this.excelstream = Excelstream;}      Public String Exportexcel () {          Iexcelservice es = new Excelserviceimpl ();          Excelstream = Es.getexcelinputstream ();        This.filename = "Student score table";        return "Excel";      }}  

Iexcelservice.java

Package Com.jxl.service;import Java.io.inputstream;public Interface Iexcelservice {/** * get input stream of Excel table * @return InputStream *                input stream  */public inputstream getexcelinputstream ();    }

Excelserviceimpl.java

Package Com.jxl.service;import Java.io.ByteArrayInputStream;  Import Java.io.ByteArrayOutputStream;  Import Java.io.InputStream;    Import Java.io.OutputStream; Import JXL.  Workbook;  Import Jxl.write.WritableSheet;  Import Jxl.write.WritableWorkbook;  Import Com.jxl.service.IExcelService;          public class Excelserviceimpl implements Iexcelservice {@Override public inputstream Getexcelinputstream () {          Convert OutputStream to InputStream bytearrayoutputstream out = new Bytearrayoutputstream ();          Putdataonoutputstream (out);      return new Bytearrayinputstream (Out.tobytearray ());        } private void Putdataonoutputstream (OutputStream os) {Jxl.write.Label label1;        Jxl.write.Label Label2;          Writableworkbook workbook;              try {workbook = workbook.createworkbook (OS);              Writablesheet Sheet1 = Workbook.createsheet ("Computer class 01", 0);            Label1 = new Jxl.write.Label (0, 0, "Computer 01-Class Excel Score table");  Sheet1.addcell (Label1);              Writablesheet Sheet2 = Workbook.createsheet ("Computer class 02", 0);              Label2 = new Jxl.write.Label (0, 0, "Computer 02-Class Excel Score table");                          Sheet2.addcell (Label2);              Workbook.write ();          Workbook.close ();          } catch (Exception e) {e.printstacktrace ();  }      }  }

Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ STRUTS-2.1.DTD ><struts> <!--Configure constants in Struts 2 apps--<constant name= "struts.i18n.encoding" value= " UTF-8 "/> <package name=" Platform-default "extends=" Struts-default "> <action name=" Excel "class=" Com.jxl.action.ExcelAction "method=" Exportexcel "> <!--download the file type definition, defined as" stream "and <res Ult name= "Excel" Type= "stream" > <!--application/vnd.ms-excel represents an Excel file--&lt                ;p Aram Name= "ContentType" > Application/vnd.ms-excel </param> <!--download file output stream definition-<!--the value values for the InputName element here are Excelstream, there must be a corresponding getexcelstream in the action ( ) Method--<param name= "InputName" >excelStream</param> <!--download file processing method--<param name= "Contentdisposition" > <!--attachment means attachment mode, that is, open the Save dialog window when downloading, Filena Me indicates the file name that was displayed when the download was downloaded-<!--if the attachment is not written, or is written inline; is inline, it tries to open the downloaded file in the browser instead of downloading the attachment;filename=${filename} <!--the FileName property and G in the object's Aciton Etfilename () Method-</param> <!--download buffer size-<param name      = "BufferSize" >1024</param> </result> </action> </package> </struts>

Web. XML

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">  <!--set Struts 2 Filter--  <filter>      < Filter-name>struts 2</filter-name>      <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>  </filter>  <filter-mapping>      <filter-name>struts 2</filter-name>      <url-pattern>/*</ url-pattern>  </filter-mapping>    <!--settings Welcome page--  <welcome-file-list>    < welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <!--session timeout definition in minutes-- >  <session-config><session-timeout>30</session-timeout>  </session-config>  </web-app>

index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

After completing all of the above steps, publish the project to the server, with the browser access to the index.jsp, which has a function entry


This example only realizes the most simple operation, the function of downloading Excel table, as for more detailed Excel table's property setting, can refer to http://merrygrass.iteye.com/blog/558274 and http:// Www.blogjava.net/Vincent-chenxj/articles/242512.html


Implementing Java Export Excel table

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.