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 use EasyExcel to import and export excel in Java

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Java how to use EasyExcel to achieve import and export excel, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

I. preparatory work

1. Guide package org.apache.poi poi 3.17 org.apache.poi poi-ooxml-schemas 3.17 org.apache.poi poi-ooxml 3.17 com.alibaba easyexcel 2.1.7 II.

1. Common annotation field annotation @ ColumnWith (column width) @ ColumnWidth (global column width) @ ExcelProperty (field configuration) @ HeadFontStyle (header style)

@ HeadRowHeight (title height)

@ ContentFontStyle (content font style)

@ ContentRowHeight (content height) 2, @ ExcelProperty comments

A necessary note, in which there are three parameters, value,index, which are listed and serial numbers respectively.

Value and index can only choose one of the two, and usually there is no need to set converter

1.value corresponds through the title text

2.index corresponds by text line number

@ ExcelProperty (value = "number", index = 0) private Long id;3, @ ColumnWith comments

Set the column width. Only one parameter value,value is in character length. You can set a maximum of 255characters, because the maximum number of characters that can be written in an excel cell is 255characters.

Public class ImeiEncrypt {@ ColumnWidth (value = 255) / / excel maximum length of a single cell 255 private String message;} 4, @ ContentFontStyle annotation

Notes for formatting cell content fonts

Parameter meaning fontName font name fontHeightInPoints font height italic whether italic strikeout setting delete horizontal line color font color typeOffset offset underline underline bold whether bold charset encoding format 5, @ ContentStyle annotation

Set content format comment

Parameter meaning dataFormat date format hidden setting cell use this style to hide the locked setting cell use this style to lock quotePrefix add `symbol in front of the cell, numbers or formulas will show whether the horizontalAlignment setting is centered horizontally and whether the wrapped setting text should wrap. Set this flag to true to make everything in the cell visible by displaying on multiple lines whether the verticalAlignment setting is vertically centered rotation sets the rotation angle of the text in the cell. 03 version of Excel rotation angle range is-90 °90 °, 07 version of Excel rotation angle range is 0 °180 °indent setting the number of spaces in the cell indented text borderLeft setting left border style borderRight setting right border style borderTop setting upper border style leftBorderColor setting left border color rightBorderColor setting upper border color topBorderColor setting top border color bottomBorderColor setting bottom border color fillPatternType setting filling type setting background color shrinkToFit setting automatic cell size 6 @ HeadFontStyle

Used to customize the title font format

Parameter meaning fontName sets font name fontHeightInPoints sets font height italic sets font whether italics strikeout sets delete line color sets font color typeOffset sets offset underline sets underline charset sets font encoding bold sets font whether bold 7, ExcelIgnore notes

Do not convert this field to Excel

III. Coding

1. Mapping entity class-example package com.pingou.admin.bean.param;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.annotation.format.DateTimeFormat;import com.alibaba.excel.annotation.write.style.ColumnWidth;import com.alibaba.excel.annotation.write.style.ContentRowHeight;import com.alibaba.excel.annotation.write.style.HeadRowHeight;import lombok.Data;import java.math.BigDecimal;import java.util.Date @ Data@ContentRowHeight (35) / / text line height @ HeadRowHeight (40) / / title height @ ColumnWidth (40) public class OrderExcel {/ / set the excel header name @ ExcelProperty (value = "number", index = 0) private Long id; @ DateTimeFormat ("yyyyyear mm minutes ss seconds at HH")) @ ExcelProperty (value = "creation time", index = 1) private Date createTime;}

The above is a simple example. If you have more attributes, just write them one by one, and then insert them into the entity class.

2. Generate excelpublic void excel () {/ / the data result set List excel = new ArrayList () of the excel to be exported; / / omit the operation of inserting data into the result set / / UUID generates a unique name String name = UUID.randomUUID (). ToString (). ReplaceAll ("-", ") +" .xlsx " / / implement excel write operation / / 1 set write folder address and excel file name String filename = "/ path" + name; JSONObject json = new JSONObject () Try {/ / 2 calls the method in easyexcel to implement the write operation / / write method with two parameters: the first parameter file path name and the second parameter entity class class EasyExcel.write (filename, OrderExcel.class) .sheet ("name") .doWrite (excel) / / if uploading to fastdfs, only local machine can find it. Generate excel File file = new File (filename) under the above path; String path = fastDFSClient.upload (new FileInputStream (file), name, null); path = (this.fastdfsDomain + path); json.put ("url", path) } catch (IOException e) {e.printStackTrace ();} finally {new File (filename) .delete () }} Thank you for reading this article carefully. I hope the article "Java how to use EasyExcel to achieve Import and Export excel" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report