2007 version of Excel multiple sheet page data is batched into the database through Ibatis

Source: Internet
Author: User
Tags cdata

Page section

<form method= "POST" name = "Test" enctype= "Multipart/form-data" >

<input type= "File" name= "file"/>

</form>

Entity class section

Public Class test{

Private String ID;

private String name;

Private String address;

The following is the Get, set method (omitted)

}

Action section

Get file by strusts2

private file file;
Private String FileName;
Private String Filecontenttype;

Add a Get, set method

Get Workbook Object

Xssfworkbook WorkBook = new Xssfworkbook (File.getinputstream ());

define Worksheet

Xssfsheet workSheet = null;

Defines the data obtained from Excel to be stitched into a list object

List<test> listtest=new arraylist<test> ();

Get the number of sheet pages

int sheetcount = Workbook.getnumberofsheets ();

for (int i=0;i<sheetcount;i++) {

Get the first I sheet

WorkSheet = Workbook.getsheetat (i);

Gets the number of rows for the sheet page

for (int j=0;j<worksheet.getphysicalnumberofrows (); j + +) {

Get Line J

Xssfrow row = Worksheet.getrow (j);

Test test=new test ();

Row.getcell (0) Get data for cell 1th of row J

Test.setid (Row.getcell (0) ==null? "": Row.getcell (0). toString (). Trim ());

Judging if the 2nd Act number format turns into a string

if (Row.getcell (1)!=null && Row.getcell (1). Getcelltype () = = Xssfcell.cell_type_numeric) {
Row.getcell (1). Setcelltype (cell.cell_type_string);
}

Test.setname (Row.getcell (1) ==null? "": Row.getcell (1). toString (). Trim ());

Test.setaddress (Row.getcell (2) ==null? "": Row.getcell (2). toString (). Trim ());

Listtest.add (test);

}

}

Ibatis part

<insert id= "Export" parameterclass= "Java.util.List" >

<! [cdata[

Insert All

]]>

<iterate conjunction= "" >

<! [cdata[

into Test (id,name,address)

Values

(#listTest [].id#, #listTest [].name#, #listTest [].address#]

]]>

</iterate>

<! [cdata[

Select 1 from dual

]]>

</insert>

2007 version of Excel multiple sheet page data is batched into the database through Ibatis

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.