Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Configure simple and powerful excel tool classes to handle excel import and export tool classes (1)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

For J2EE project import and export Excel is the most common and practical function. This tool class has simple steps and powerful functions. It only needs simple annotations for entity classes to achieve the import and export function.

Take a look at what this class does:

1. Entity attributes can be exported to excel with annotations configured, and each attribute corresponds to a column.

two。 Column names can be configured through annotations.

3. Which column to export to can be configured through annotations.

4. The prompt information when you move the mouse over the column can be configured through annotations.

5. With annotation settings, you can only drop down and choose not to fill in the function at will.

6. Use annotations to set whether to export only the title and not the content, which is useful when exporting content as a template for users to fill in.

Please take a look at the effect picture:

Please take a look at the steps to use:

1. Write an entity class and set the annotation configuration.

two。 Instantiate an ExcelUtil object and call the exportExcel or importExcel method.

Please look at a demo.

1. Write an entity class and set the annotation configuration.

Package com.tgb.lk.test03

Import com.tgb.lk.util.ExcelVOAttribute

Public class StudentVO {

@ ExcelVOAttribute (name = "serial number", column = "A")

Private int id

@ ExcelVOAttribute (name = "name", column = "B", isExport = true) private String name;@ExcelVOAttribute (name = "age", column = "C", prompt = "age secret!", isExport = false) private int age;@ExcelVOAttribute (name = "class", column = "D", combo = {"fifth improvement class", "sixth improvement class", "seventh improvement class"}) private String clazz @ ExcelVOAttribute (name= "Company", column = "F") private String company;//get and set methods (briefly). @ Overridepublic String toString () {return "StudentVO [id=" + id + ", name=" + name + ", company=" + company + ", age=" + age + ", clazz=" + clazz + "]";}

}

two。 Instantiate an ExcelUtil object and call the exportExcel or importExcel method.

(1) Export

Package com.tgb.lk.test03

Import java.io.FileNotFoundException

Import java.io.FileOutputStream

Import java.util.ArrayList

Import java.util.List

Import com.tgb.lk.util.ExcelUtil

/ *

Use steps: 1. Create a new class, such as StudentVO.2. Set which properties need to be exported and which need to be prompted. 3. Set up entity data

4. Call the exportExcel method.

, /

Public class ExportTest03 {

Public static void main (String [] args) {

/ / initialize data

List list = new ArrayList ()

StudentVO vo = new StudentVO (); vo.setId (1); vo.setName (Li Kun); vo.setAge (26); vo.setClazz (Fifth Enhancement Class); vo.setCompany (Tianrongxin); list.add (vo); StudentVO vo2 = new StudentVO (); vo2.setId (2); vo2.setName (Cao Guisheng); vo2.setClazz (Fifth Enhancement Class); vo2.setCompany (Bank of China); list.add (vo2); StudentVO vo3 = new StudentVO () Vo3.setId (3); vo3.setName ("Liu Bo"); vo3.setClazz ("five improvement classes"); list.add (vo3); FileOutputStream out = null;try {out = new FileOutputStream ("d:\\ success3.xls");} catch (FileNotFoundException e) {e.printStackTrace ();} ExcelUtil util = new ExcelUtil (StudentVO.class); / / create tool class .util.exportExcel (list, "Student Information", 65536, out) / / Export System.out.println ("- execution completed -")

}

}

(2) Import

Package com.tgb.lk.test03

Import java.io.FileInputStream

Import java.io.FileNotFoundException

Import java.util.List

Import com.tgb.lk.util.ExcelUtil

Public class ImportTest03 {

Public static void main (String [] args) {

FileInputStream fis = null

Try {

Fis = new FileInputStream ("d:\ success3.xls")

ExcelUtil util = new ExcelUtil (

StudentVO.class); / / create an excel utility class

List list = util.importExcel ("Student Information 0", fis); / / Import

System.out.println (list)

} catch (FileNotFoundException e) {

E.printStackTrace ()

}

}

}

After reading the steps, you can't wait for the encapsulated class. Please read on:

(1) Annotation implementation class:

Package com.tgb.lk.util

Import java.lang.annotation.Retention

Import java.lang.annotation.RetentionPolicy

Import java.lang.annotation.Target

@ Retention (RetentionPolicy.RUNTIME)

@ Target ({java.lang.annotation.ElementType.FIELD})

Public @ interface ExcelVOAttribute {

/ * the name exported to Excel. * / public abstract String name (); / * the name of the configuration column, which corresponds to A _ Magi B _ M C M D.. * / public abstract String column (); / * * prompt * / public abstract String prompt () default ""; / * set to select only columns that cannot be entered. * / public abstract String [] combo () default {}; / * whether to export data to meet the demand: sometimes we need to export a template, which is required for the title, but the content needs to be filled in manually. * / public abstract boolean isExport () default true

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report