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

How to import excel using easypoi

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to use easypoi to import excel, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Import excel using easypoi

Easypoi importing excel requires the introduction of dependencies:

Cn.afterturn easypoi-base 3.2.0 cn.afterturn easypoi-web 3.2.0 cn.afterturn easypoi-annotation 3.2.0

First we prepare an excel template with some data

Step 2: prepare a solid object

Package com.xash.quartzDemo.entity;import cn.afterturn.easypoi.excel.annotation.Excel;import cn.afterturn.easypoi.excel.annotation.ExcelTarget;import com.fasterxml.jackson.annotation.JsonFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;@ExcelTarget ("Layer") public class Layer {private String layerId; @ Excel (name = "regulatory name", isImportField = "true_st") private String layerName @ Excel (name = "regulatory description", isImportField = "true_st") private String description; @ Excel (name = "regulatory release date", importFormat = "yyyy-MM-dd") private String releaseTime; @ Excel (name = "regulatory upload date", importFormat = "yyyy-MM-dd") private String recordTime; private String fileName; public String getLayerId () {return layerId } public void setLayerId (String layerId) {this.layerId = layerId = = null? Null: layerId.trim ();} public String getLayerName () {return layerName;} public void setLayerName (String layerName) {this.layerName = layerName = = null? Null: layerName.trim ();} public String getDescription () {return description;} public void setDescription (String description) {this.description = description = = null? Null: description.trim ();} SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); public String getReleaseTime () {return releaseTime;} public void setReleaseTime (Date releaseTime) throws ParseException {this.releaseTime = sdf.format (releaseTime);} public String getRecordTime () {return recordTime;} public void setRecordTime (Date recordTime) throws ParseException {this.recordTime = sdf.format (recordTime) } public String getFileName () {return fileName;} public void setFileName (String fileName) {this.fileName = fileName = = null? Null: fileName.trim ();}}

Here, the date format object excel to the entity cannot be automatically converted by format, and I don't know why, so the date is formatted manually.

Step three

Create an import tool class

Package com.xash.quartzDemo.util;import java.io.File;import java.util.List;import org.apache.commons.lang3.StringUtils;import org.springframework.web.multipart.MultipartFile;import cn.afterturn.easypoi.excel.ExcelImportUtil;import cn.afterturn.easypoi.excel.entity.ImportParams Public class Upload {public static String executeUpload1 (String uploadDir,MultipartFile file,String fileName) throws Exception {/ / File suffix String suffix = file.getOriginalFilename () .substring (file.getOriginalFilename () .lastIndexOf (".")); / / upload file name String filename = fileName + suffix / / the file object saved on the server side File serverFile = new File (uploadDir + filename); / / write the uploaded file to file.transferTo (serverFile) in the server side file; return filename } public static List importExcel (String filePath, Integer titleRows, Integer headerRows, Class pojoClass) {if (StringUtils.isBlank (filePath)) {return null;} ImportParams params = new ImportParams (); params.setTitleRows (titleRows); params.setHeadRows (headerRows); List list = null Try {list = ExcelImportUtil.importExcel (new File (filePath), pojoClass, params);} catch (Exception e) {e.printStackTrace ();} return list } public static List importExcel (MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass) {if (file = = null) {return null;} ImportParams params = new ImportParams (); params.setTitleRows (titleRows); params.setHeadRows (headerRows); List list = null Try {list = ExcelImportUtil.importExcel (file.getInputStream (), pojoClass, params);} catch (Exception e) {e.printStackTrace ();} return list;}}

This example uses the public static List importExcel (String filePath, Integer titleRows, Integer headerRows, Class pojoClass) method

OK, after the preparatory work is done, you can import excel.

Prepare a controller front-end controller

Package com.xash.quartzDemo.controller;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.UUID;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.xash.quartzDemo.entity.Layer;import com.xash.quartzDemo.util.Upload;import cn.afterturn.easypoi.excel.entity.ImportParams @ RestControllerpublic class Eecelmport {@ RequestMapping ("/ excel") public List excel () {String fliename= "template Import .xlsx"; Listlist=null;; try {list=Upload.importExcel ("C:/Users/gaofei/Desktop/" + fliename, 1,3, Layer.class) } catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return list;}}

Front-end access:

Http://192.168.2.81:8082/excel, the backend responds and returns:

[{"layerId": null, "layerName": "hello", "description": "hello", "releaseTime": "2015-07-17", "recordTime": "2015-09-17", "fileName": null}, {"layerId": null, "layerName": "world", "description": "world", "releaseTime": "2015-08-17", "recordTime": "2015-10-17", "fileName": null}] share here on how to import excel using easypoi I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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