Sometimes we need to enter a lot of data in the project, Excel operations

Source: Internet
Author: User

Sometimes we need to enter a lot of data in the project, Excel operations

1: First you need to import a rack package into your project:

Jxl.jar,

Poi-3.6-20091214.jar
Poi-3.8-beta5-20111217.jar
Poi-examples-3.6-20091214.jar
Poi-examples-3.8-beta5-20111217.jar
Poi-excelant-3.8-beta5-20111217.jar
Poi-ooxml-3.6-20091214.jar
Poi-ooxml-3.8-beta5-20111217.jar
Poi-ooxml-schemas-3.6-20091214.jar
Poi-ooxml-schemas-3.8-beta5-20111217.jar
Poi-scratchpad-3.6-20091214.jar
Poi-scratchpad-3.8-beta5-20111217.jar

2: Need a tool entity class to manipulate Excel

 PackageCom.fxr.po;Importjava.util.ArrayList;Importjava.util.List; Public classExcelworksheet<t>{    PrivateString SheetName;//page    PrivateList<t>data=NewArraylist<t> ();//Data    Privatelist<string>columns;//column    /**     * @returnThe sheetname*/     PublicString Getsheetname () {returnSheetName; }    /**     * @paramSheetName the sheetname to set*/     Public voidsetsheetname (String sheetname) { This. SheetName =SheetName; }    /**     * @returnThe data*/     PublicList<t>GetData () {returndata; }    /**     * @paramdata The data to set*/     Public voidSetData (list<t>data) {         This. data =data; }    /**     * @returnThe columns*/     PublicList<string>GetColumns () {returncolumns; }    /**     * @paramcolumns The columns to set*/     Public voidSetColumns (list<string>columns) {         This. Columns =columns; }        }

Write a specific logical implementation in action

 Packagecom.fxr.action;ImportCom.opensymphony.xwork2.ActionContext;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.fxr.dao.SubjectDAO;ImportCom.fxr.dao.SubjectDAOImpl;ImportCom.fxr.po.ExcelWorkSheet;ImportCom.fxr.po.Subject;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;Importjava.io.UnsupportedEncodingException;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook;ImportOrg.apache.struts2.ServletActionContext; Public classImportexcelactionextendsactionsupport{PrivateFile Excelfile;//Excel File    PrivateString Excelfilefilename;//the name of the file    PrivateExcelworksheet<subject> Excelworksheet;//page    PrivateWorkbook Createworkbook (InputStream is)throwsexception{if(Getexcelfilefilename (). toLowerCase (). EndsWith ("xls")){            return NewHssfworkbook (IS); }        if(Getexcelfilefilename (). toLowerCase (). EndsWith ("xlsx"))        {            return NewXssfworkbook (IS); }        return NULL; }             Publicimportexcelaction () {} @Override PublicString Execute ()throwsException {httpservletrequest request=servletactioncontext.getrequest ();         SYSTEM.OUT.PRINTLN (Request); Servletactioncontext.getrequest (). setcharacterencoding ("Utf-8"); Workbook Workbook=createworkbook (NewFileInputStream (excelfile)); Sheet Sheet= (Sheet) workbook.getsheetat (0); Excelworksheet=NewExcelworksheet<subject>();       Excelworksheet.setsheetname (Sheet.getsheetname ()); Row FirstRow=sheet.getrow (0); Iterator<org.apache.poi.ss.usermodel.cell>iterator=Firstrow.iterator (); List<string>cellnames=NewArraylist<string>();  while(Iterator.hasnext ()) {Cellnames.add (Iterator.next (). Getstringcellvalue ());        } excelworksheet.setcolumns (Cellnames);  for(inti = 1; I <=sheet.getlastrownum (); i++) {row row=Sheet.getrow (i); Subject Subject=NewSubject (); Subject.setsubjecttitle (Row.getcell (0). Getstringcellvalue ()); Subject.setsubjectoptiona (Row.getcell (1). Getstringcellvalue ()); Subject.setsubjectoptionb (Row.getcell (2). Getstringcellvalue ()); Subject.setsubjectoptionc (Row.getcell (3). Getstringcellvalue ()); Subject.setsubjectoptiond (Row.getcell (4). Getstringcellvalue ()); Subject.setsubjectanswer (Row.getcell (5). Getstringcellvalue ()); Subject.setsubjectparse (Row.getcell (6). Getstringcellvalue ()); Subjectdao Subjectdao=NewSubjectdaoimpl ();            Subjectdao.addsubject (subject);        Excelworksheet.getdata (). Add (subject); }        returnSUCCESS; }    /**     * @returnThe excelfile*/     PublicFile Getexcelfile () {returnExcelfile; }    /**     * @paramExcelfile the excelfile to set*/     Public voidsetexcelfile (File excelfile) { This. Excelfile =Excelfile; }      PublicExcelworksheet<subject>Getexcelworksheet () {returnExcelworksheet; }    /**     * @paramExcelworksheet the Excelworksheet to set*/     Public voidSetexcelworksheet (excelworksheet<subject>Excelworksheet) {         This. Excelworksheet =Excelworksheet; }    /**     * @returnThe excelfilefilename*/     PublicString Getexcelfilefilename () {returnExcelfilefilename; }    /**     * @paramExcelfilefilename the excelfilefilename to set*/     Public voidsetexcelfilefilename (String excelfilefilename) { This. Excelfilefilename =Excelfilefilename; }}

Front-desk JSP

<% @page contenttype= "text/html" pageencoding= "UTF-8"%><% @taglib  prefix= "s" uri= "/struts-tags"%> <! DOCTYPE html>        #ge {height:20px; Background-color: #AAEC79;}         </style>    

Insert a successful JSP

<% @page contenttype= "text/html" pageencoding= "UTF-8"%><% @taglib prefix= "s" uri= "/struts-tags"%><! DOCTYPE html>#ge {height:20px; background-color: #AAEC79;} </style> 

Sometimes we need to enter a lot of data in the project, Excel operations

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.