Java easyreport Import Excel, TXT data simple implementation (i)

Source: Internet
Author: User

have been looking at the blog, but do not know how to write, but always want to write something, hesitated for two or three days, decided to write something, to share with you.

The first half of this year received a demand, specifically what needs not to say, asked to import Excel data, and prompt each line of error messages. After receiving it, look at the previous code for importing Excel reports, and find that only initializing workbook and getting the cell string based on the cell is worth the generic method. Feel that there are too few things to take advantage of, so decided to build a generic implementation of the import Excel Report Toolkit.

The implementation idea is roughly like this:

    1. Provides a template that contains a read start line, a property template collection, a read batch, and a checksum error in the middle end.

    2. Add a property template that contains the title, property name, field index, and property check object.

    3. Provides the Read engine class, initializes the read file input stream, reads the check field according to the template, verifies the error information management, the object assigns the attribute, matches adds the object.

Concrete implementation of the way not to do a detailed narration, only about how to use, I will provide the source code to everyone, the source under the Com.easyReport.read, interested can read, welcome to communicate.

Test using Demo.


First build the basic test class Basictest

Package test;


Import Java.io.FileInputStream;

Import Java.lang.reflect.ParameterizedType;

Import java.util.List;

Import Java.util.Map;


Import Org.junit.Test;


Import Com.easyReport.read.ConvertReader;

Import com.easyReport.read.temp.ReadTemp;


Public abstract class Basictest<t> {

Test Execution methods

@Test

public abstract void test ();

Building a read engine

Protected abstract Convertreader buildreader ();

Build a read template

Protected abstract readtemp bulidreadtemp ();

Output object Information

protected abstract void Writebeanlist (list<t> modellists);

Output map Information

protected abstract void Writemaplist (list<map<string, object>> maplists);


@SuppressWarnings ("Unchecked")

public void Testreadbean (FileInputStream fis) throws Exception {

Convertreader reader = Buildreader ();

Writebeanlist (list<t>) reader.readtobeanlist (FIS, Getmodelclass ()));

}


public void Testreadmap (FileInputStream fis) throws Exception {

Convertreader reader = Buildreader ();

Writemaplist (Reader.readtomaplist (FIS));

}


Protected class<?> Getmodelclass () {

Return (class<?>) ((Parameterizedtype) (GetClass ()

. Getgenericsuperclass ())). Getactualtypearguments () [0];

}

}

Building an Excel base class to implement a read engine

Package test;


Import Com.easyReport.read.ConvertReader;

Import Com.easyReport.read.ExcelReader;

Import Com.easyReport.read.ReaderContext;


Public abstract class Abstractexceltest<t> extends Basictest<t> {


@Override

Protected Convertreader Buildreader () {

return new Excelreader (New Readercontext (Bulidreadtemp ()));

}

}

Build the TXT base class to implement the Read engine

Package test;


Import Com.easyReport.read.ConvertReader;

Import Com.easyReport.read.ReaderContext;

Import Com.easyReport.read.TxtReader;


Public abstract class Abstracttxttest<t> extends Basictest<t> {


@Override

Protected Convertreader Buildreader () {

return new Txtreader (New Readercontext (Bulidreadtemp ()));

}

}

Test.xls Report Data

Name Age Subjects Scores
Bachelor 12 Chinese 80

Excel Import

package example;


Import Java.io.File;

Import Java.io.FileInputStream;

Import java.util.List;

Import Java.util.Map;


Import model. Student;

Import Test. Abstractexceltest;


Import com.easyReport.read.temp.PropertyTemp;

Import com.easyReport.read.temp.ReadTemp;


public class Testexcelsimple extends Abstractexceltest<student> {

@Override

public void Test () {

FileInputStream FileInputStream;

try {

FileInputStream = new FileInputStream (New File ("Testfile/test.xls"));

Testreadbean (FileInputStream);

Testreadmap (FileInputStream);

Fileinputstream.close ();

} catch (Exception e) {

E.printstacktrace ();

}

}


@Override

Protected Readtemp bulidreadtemp () {

Readtemp readtemp = new Readtemp (1);

Readtemp.addpropertytemp (New Propertytemp ("name", "name"));

Readtemp.addpropertytemp (New Propertytemp ("Age", "ages");

Readtemp.addpropertytemp (New Propertytemp ("Account", "Course.name"));

Readtemp.addpropertytemp (New Propertytemp ("Score", "Course.score"));

return readtemp;

}


@Override

protected void Writebeanlist (list<student> modellists) {

System.out.println ("--------------bean---------------");

for (Student s:modellists) {

System.out.println (S.getname () + "|" +s.getage () +s.getcourse (). GetName () + "|" +s.getcourse (). Getscore ());

}

}


@Override

protected void Writemaplist (list<map<string, object>> maplists) {

SYSTEM.OUT.PRINTLN ("--------------map---------------");

For (map<string, object> m:maplists) {

System.out.println (M.get ("name") + "|" + m.get ("age") + "|" + M.get ("course.name") + "|" + M.get ("Course.score");

}

}


}

Operation Result:

--------------Bean---------------

Bachelor |12 Chinese |80.0

Switch to Testreadmap (FileInputStream) and run the result:

--------------Map---------------

Bachelor |12.0| Chinese |80.0

Test.txt data

name | age | subject | age

Bachelor |12| Chinese |80

TXT Import

package example;


Import Java.io.File;

Import Java.io.FileInputStream;

Import java.util.List;

Import Java.util.Map;


Import model. Student;

Import Test. Abstracttxttest;


Import com.easyReport.read.temp.PropertyTemp;

Import com.easyReport.read.temp.ReadTemp;

Import com.easyReport.read.temp.TxtReadTemp;


public class Testtxtsimple extends Abstracttxttest<student> {

@Override

public void Test () {

FileInputStream FileInputStream;

try {

FileInputStream = new FileInputStream (New File ("Testfile/test.txt"));

Testreadbean (FileInputStream);

Testreadmap (FileInputStream);

Fileinputstream.close ();

} catch (Exception e) {

E.printstacktrace ();

}

}


@Override

Protected Readtemp bulidreadtemp () {

Txtreadtemp readtemp = new Txtreadtemp (1);

Readtemp.setsplit ("\\|"); /default, or, separator

Readtemp.addpropertytemp (New Propertytemp ("name", "name"));

Readtemp.addpropertytemp (New Propertytemp ("Age", "ages");

Readtemp.addpropertytemp (New Propertytemp ("Account", "Course.name"));

Readtemp.addpropertytemp (New Propertytemp ("Score", "Course.score"));

return readtemp;

}


@Override

protected void Writebeanlist (list<student> modellists) {

System.out.println ("--------------bean---------------");

for (Student s:modellists) {

System.out.println (S.getname () + "|"

+ s.getage () +s.getcourse (). GetName () + "|" +s.getcourse (). Getscore ());

}

}


@Override

protected void Writemaplist (list<map<string, object>> maplists) {

SYSTEM.OUT.PRINTLN ("--------------map---------------");

For (map<string, object> m:maplists) {

System.out.println (M.get ("name") + "|" + m.get ("age") + "|" + M.get ("course.name") + "|" + M.get ("Course.score");

}

}


}

Operation Result:

--------------Map---------------

Bachelor |12| Chinese |80


Easyreport Dependent jar Package

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/54/22/wKioL1R5UWDgWumuAADri8pIRM8487.jpg "title=" Easyreport dependent jar package. jpg "alt=" wkiol1r5uwdgwumuaadri8pirm8487.jpg "/>



This article is from "Rookie Bobo" blog, please be sure to keep this source http://zhangxiao2.blog.51cto.com/6342789/1584435

Java easyreport Import Excel, TXT data simple implementation (i)

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.