茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729591 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_654.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

爪哇咖啡屋 : [轉貼]JAVA 使用 easyexcel 操作 Excel

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]JAVA 使用 easyexcel 操作 Excel

JAVA使用easyexcel操作Excel

之前寫過一篇《 JAVA操作Excel》,介紹了 jxlpoi讀寫Excel的實現,今天為大家介紹一下使用 easyexcel對Excel進行讀寫,項目主頁地址: https://github.com/alibaba/easyexcel

作者對easyexcel的介紹是:

Java解析、生成Excel比較有名的框架有Apache poi、jxl。但他們都存在一個嚴重的問題就是非常的耗內存,poi有一套SAX模式的API可以一定程度的解決一些內存溢出的問題,但POI還是有一些缺陷,比如07版Excel解壓縮以及解壓後存儲都是在內存中完成的,內存消耗依然很大。easyexcel重寫了poi對07版Excel的解析,能夠原本一個3M的excel用POI sax依然需要100M左右內存降低到KB級別,並且再大的excel不會出現內存溢出,03版依賴POI的sax模式。在上層做了模型轉換的封裝,讓使用者更加簡單方便

使用easyexcel,首先我們需要添加maven依賴:


dependency>
groupId>
artifactId>
version>
dependency>

首先,我們先來看看如何寫Excel,寫入Excel,我們可以通過 com.alibaba.excel.ExcelWriter類實現,下面我們來看一下最簡單的無表頭的實現


package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelWriteTest {
/**
* 每行數據是List<String>無表頭
*
* @throws IOException
*/

@Test
throws IOException {
);) {
ExcelWriter writer = false);
Sheet sheet1 = 0);
sheet1.setSheetName();
List<List<String>> data = new ArrayList<>();
100; i++) {
List<String> item = new ArrayList<>();
item.add( + i);
item.add( + i);
item.add( + i);
data.add(item);
}
writer.write0(data, sheet1);
writer.finish();
}
}
}

生成的Excel樣式如下:
這裡寫圖片描述

很多時候,我們在生成Excel的時候都是需要添加表頭的,使用easyexcel可以很容易的實現,我們可以對上面的例子進行簡單的改造,為其添加表頭


package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelWriteTest {
@Test
throws IOException {
);) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = 0);
sheet1.setSheetName();
List<List<String>> data = new ArrayList<>();
100; i++) {
List<String> item = new ArrayList<>();
item.add( + i);
item.add( + i);
item.add( + i);
data.add(item);
}
List<List<String>> head = new ArrayList<List<String>>();
List<String> headCoulumn1 = new ArrayList<String>();
List<String> headCoulumn2 = new ArrayList<String>();
List<String> headCoulumn3 = new ArrayList<String>();
headCoulumn1.add();
headCoulumn2.add();
headCoulumn3.add();
head.add(headCoulumn1);
head.add(headCoulumn2);
head.add(headCoulumn3);
Table table = 1);
table.setHead(head);
writer.write0(data, sheet1, table);
writer.finish();
}
}
}

效果如下:

這裡寫圖片描述

除了上面添加表頭的方式,我們還可以使用實體類,為其添加 com.alibaba.excel.annotation.ExcelProperty註解來生成表頭,實體類數據作為Excel數據


package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelWriteTest {
@Test
throws IOException {
);) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = 0, ExcelPropertyIndexModel.class);
sheet1.setSheetName();
List<ExcelPropertyIndexModel> data = new ArrayList<>();
100; i++) {
ExcelPropertyIndexModel item = new ExcelPropertyIndexModel();
item.name = + i;
item.age = + i;
item.email = + i;
item.address = + i;
item.sax = + i;
item.heigh = + i;
item.last = + i;
data.add(item);
}
writer.write(data, sheet1);
writer.finish();
}
}
BaseRowModel {

0)
private String name;
1)
private String age;
2)
private String email;
3)
private String address;
4)
private String sax;
5)
private String heigh;
6)
private String last;
getName() {
return name;
}
setName(String name) {
this.name = name;
}
getAge() {
return age;
}
setAge(String age) {
this.age = age;
}
getEmail() {
return email;
}
setEmail(String email) {
this.email = email;
}
getAddress() {
return address;
}
setAddress(String address) {
this.address = address;
}
getSax() {
return sax;
}
setSax(String sax) {
this.sax = sax;
}
getHeigh() {
return heigh;
}
setHeigh(String heigh) {
this.heigh = heigh;
}
getLast() {
return last;
}
setLast(String last) {
this.last = last;
}
}
}

效果如下:
這裡寫圖片描述

如果單行表頭表頭還不滿足需求,沒關係,還可以使用多行複雜的表頭


package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelWriteTest {
@Test
throws IOException {
);) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = 0, MultiLineHeadExcelModel.class);
sheet1.setSheetName();
List<MultiLineHeadExcelModel> data = new ArrayList<>();
100; i++) {
MultiLineHeadExcelModel item = new MultiLineHeadExcelModel();
item.p1 = + i;
item.p2 = + i;
item.p3 = + i;
item.p4 = + i;
item.p5 = + i;
item.p6 = + i;
item.p7 = + i;
item.p8 = + i;
item.p9 = + i;
data.add(item);
}
writer.write(data, sheet1);
writer.finish();
}
}
BaseRowModel {

0)
private String p1;
1)
private String p2;
2)
private String p3;
3)
private String p4;
4)
private String p5;
5)
private String p6;
6)
private String p7;
7)
private String p8;
8)
private String p9;
getP1() {
return p1;
}
setP1(String p1) {
this.p1 = p1;
}
getP2() {
return p2;
}
setP2(String p2) {
this.p2 = p2;
}
getP3() {
return p3;
}
setP3(String p3) {
this.p3 = p3;
}
getP4() {
return p4;
}
setP4(String p4) {
this.p4 = p4;
}
getP5() {
return p5;
}
setP5(String p5) {
this.p5 = p5;
}
getP6() {
return p6;
}
setP6(String p6) {
this.p6 = p6;
}
getP7() {
return p7;
}
setP7(String p7) {
this.p7 = p7;
}
getP8() {
return p8;
}
setP8(String p8) {
this.p8 = p8;
}
getP9() {
return p9;
}
setP9(String p9) {
this.p9 = p9;
}
}
}

效果如下:
這裡寫圖片描述

怎麼樣,這些已經基本滿足我們的日常需求了,easyexcel不僅支持上述幾種形式,還支持在一個sheet中添加多個表


package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelWriteTest {
@Test
throws IOException {
);) {
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
Sheet sheet1 = 0);
sheet1.setSheetName();
// 數據全是List<String> 無模型映射關係
Table table1 = 1);
List<List<String>> data1 = new ArrayList<>();
5; i++) {
List<String> item = new ArrayList<>();
item.add( + i);
item.add( + i);
item.add( + i);
data1.add(item);
}
writer.write0(data1, sheet1, table1);
// 模型上有表頭的註解
Table table2 = 2);
table2.setClazz(MultiLineHeadExcelModel.class);
List<MultiLineHeadExcelModel> data2 = new ArrayList<>();
5; i++) {
MultiLineHeadExcelModel item = new MultiLineHeadExcelModel();
item.p1 = + i;
item.p2 = + i;
item.p3 = + i;
item.p4 = + i;
item.p5 = + i;
item.p6 = + i;
item.p7 = + i;
item.p8 = + i;
item.p9 = + i;
data2.add(item);
}
writer.write(data2, sheet1, table2);
// 模型上沒有註解,表頭數據動態傳入,此情況下模型field順序與excel現實順序一致
List<List<String>> head = new ArrayList<List<String>>();
List<String> headCoulumn1 = new ArrayList<String>();
List<String> headCoulumn2 = new ArrayList<String>();
List<String> headCoulumn3 = new ArrayList<String>();
headCoulumn1.add();
headCoulumn2.add();
headCoulumn3.add();
head.add(headCoulumn1);
head.add(headCoulumn2);
head.add(headCoulumn3);
Table table3 = 3);
table3.setHead(head);
writer.write0(data1, sheet1, table3);
writer.finish();
}
}
BaseRowModel {

0)
private String p1;
1)
private String p2;
2)
private String p3;
3)
private String p4;
4)
private String p5;
5)
private String p6;
6)
private String p7;
7)
private String p8;
8)
private String p9;
getP1() {
return p1;
}
setP1(String p1) {
this.p1 = p1;
}
getP2() {
return p2;
}
setP2(String p2) {
this.p2 = p2;
}
getP3() {
return p3;
}
setP3(String p3) {
this.p3 = p3;
}
getP4() {
return p4;
}
setP4(String p4) {
this.p4 = p4;
}
getP5() {
return p5;
}
setP5(String p5) {
this.p5 = p5;
}
getP6() {
return p6;
}
setP6(String p6) {
this.p6 = p6;
}
getP7() {
return p7;
}
setP7(String p7) {
this.p7 = p7;
}
getP8() {
return p8;
}
setP8(String p8) {
this.p8 = p8;
}
getP9() {
return p9;
}
setP9(String p9) {
this.p9 = p9;
}
}
}

效果如下:
這裡寫圖片描述

如果表頭的樣式不滿足我們的需求,需要調整,我們可以使用 com.alibaba.excel.metadata.TableStyle定義我們需要的樣式,然後調用table對象的 setTableStyle方法進行設置。

好了,到這裡寫入excel就基本介紹完了,下面我們就來看看如何讀取excel,實際上現在的這個版本( 1.0.1)在讀取的時候是有BUG的,讀取03版的 .xls格式的excel正常,但是讀取07版的 .xlsx版的excel就會出異常,原因是在解析的時候sheet臨時文件路徑拼裝有誤,下面是我針對這個版本修復後的實現,大家可以替換掉原包中的實現


package com.alibaba.excel.read;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.xml.parsers.ParserConfigurationException;
import com.alibaba.excel.read.v07.RowHandler;
import com.alibaba.excel.read.v07.XmlParserFactory;
import com.alibaba.excel.read.v07.XMLTempFile;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.util.FileUtil;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
BaseSaxAnalyser {
private SharedStringsTable sharedStringsTable;
new LinkedList<String>();
new ArrayList<SheetSource>();
false;
final String path;
private File tmpFile;
private String workBookXMLFilePath;
private String sharedStringXMLFilePath;
throws Exception {
this.analysisContext = analysisContext;
this.path = XMLTempFile.createPath();
new File(XMLTempFile.getTmpFilePath(path));
this.workBookXMLFilePath = XMLTempFile.getWorkBookFilePath(path);
this.sharedStringXMLFilePath = XMLTempFile.getSharedStringFilePath(path);
start();
}
@Override
execute() {
try {
Sheet sheet = analysisContext.getCurrentSheet();
if (!isAnalysisAllSheets(sheet)) {
0) {
return;
}
InputStream sheetInputStream = 1).getInputStream();
parseXmlSource(sheetInputStream);
return;
}
0;
this.sheetSourceList) {
i++;
new Sheet(i));
parseXmlSource(sheetSource.getInputStream());
}
} catch (Exception e) {
stop();
new ExcelAnalysisException(e);
} finally {
}
}
isAnalysisAllSheets(Sheet sheet) {
null) {
true;
}
0) {
true;
}
false;
}
stop() {
FileUtil.deletefile(path);
}
parseXmlSource(InputStream inputStream) {
try {
ContentHandler handler = this.analysisContext,
sharedStringList);
XmlParserFactory.parse(inputStream, handler);
inputStream.close();
} catch (Exception e) {
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new ExcelAnalysisException(e);
}
}
getSheets() {
List<Sheet> sheets = new ArrayList<Sheet>();
try {
1;
this.sheetSourceList) {
Sheet sheet = 0);
sheet.setSheetName(sheetSource.getSheetName());
i++;
sheets.add(sheet);
}
} catch (Exception e) {
stop();
new ExcelAnalysisException(e);
} finally {
}
return sheets;
}
throws IOException, XmlException, ParserConfigurationException, SAXException {
createTmpFile();
unZipTempFile();
initSharedStringsTable();
initUse1904WindowDate();
initSheetSourceList();
}
throws FileNotFoundException {
FileUtil.writeFile(tmpFile, analysisContext.getInputStream());
}
throws IOException {
FileUtil.doUnZip(path, tmpFile);
}
throws IOException, ParserConfigurationException, SAXException {
new ArrayList<SheetSource>();
InputStream workbookXml = this.workBookXMLFilePath);
XmlParserFactory.parse(workbookXml, new DefaultHandler() {
@Override
throws SAXException {
)) {
String name = null;
0;
0; i < attrs.getLength(); i++) {
)) {
name = attrs.getValue(i);
} id = Integer.parseInt(attrs.getValue(i).replaceAll("rId", ""));
try {
InputStream inputStream = new FileInputStream(XMLTempFile.getSheetFilePath(path, id));
sheetSourceList.add(new SheetSource(id, name, inputStream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} **/

//應該使用sheetId屬性
)) {
id = Integer.parseInt(attrs.getValue(i));
try {
InputStream inputStream = new FileInputStream(XMLTempFile.getSheetFilePath(path, id));
sheetSourceList.add(new SheetSource(id, name, inputStream));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
});
workbookXml.close();
// 排序後是倒序,不符合實際要求
// Collections.sort(sheetSourceList);
Collections.sort(sheetSourceList, new Comparator<SheetSource>() {
@Override
compare(SheetSource o1, SheetSource o2) {
return o1.id - o2.id;
}
});
}
throws IOException, XmlException {
InputStream workbookXml = new FileInputStream(workBookXMLFilePath);
WorkbookDocument ctWorkbook = WorkbookDocument.Factory.parse(workbookXml);
CTWorkbook wb = ctWorkbook.getWorkbook();
CTWorkbookPr prefix = wb.getWorkbookPr();
null) {
this.use1904WindowDate = prefix.getDate1904();
}
this.analysisContext.setUse1904WindowDate(use1904WindowDate);
workbookXml.close();
}
throws IOException, ParserConfigurationException, SAXException {
//因為sharedStrings.xml文件不一定存在,所以在處理之前增加判斷
File sharedStringXMLFile = this.sharedStringXMLFilePath);
if (!sharedStringXMLFile.exists()) {
return;
}
InputStream inputStream = this.sharedStringXMLFilePath);
//this.sharedStringsTable = new SharedStringsTable();
//this.sharedStringsTable.readFrom(inputStream);
XmlParserFactory.parse(inputStream, new DefaultHandler() {
@Override
int length) {
sharedStringList.add(new String(ch, start, length));
}
});
inputStream.close();
}
SheetSource> {

int id;
private String sheetName;
private InputStream inputStream;
int id, String sheetName, InputStream inputStream) {
this.id = id;
this.sheetName = sheetName;
this.inputStream = inputStream;
}
getSheetName() {
return sheetName;
}
setSheetName(String sheetName) {
this.sheetName = sheetName;
}
getInputStream() {
return inputStream;
}
setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
getId() {
return id;
}
int id) {
this.id = id;
}
compareTo(SheetSource o) {
this.id) {
0;
} this.id) {
1;
} else {
1;
}
}
}
}

另外,使用easyexcel讀取excel的時候需要設置excel的版本,但是有些時候我們無法預知excel的版本,所以個人感覺這樣不是太好,所以模仿 poi寫了一個用於獲取 com.alibaba.excel.ExcelReader對象的工具類


package com.alibaba.excel.read;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import org.apache.poi.EmptyFileException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
ExcelReaderFactory {
/**
* @param in
* 文件輸入流
* @param customContent
* 自定義模型可以在
* {@link AnalysisEventListener#invoke(Object, AnalysisContext) }
* AnalysisContext中獲取用於監聽者回調使用
* @param eventListener
* 用戶監聽
* @throws IOException
* @throws EmptyFileException
* @throws InvalidFormatException
*/

getExcelReader(InputStream in, Object customContent,
AnalysisEventListener<?> eventListener) throws EmptyFileException, IOException, InvalidFormatException {
// 如果輸入流不支持mark/reset,需要對其進行包裹
if (!in.markSupported()) {
in = 8);
}
// 確保至少有一些數據
byte[] header8 = IOUtils.peekFirst8Bytes(in);
ExcelTypeEnum excelTypeEnum = null;
if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
excelTypeEnum = ExcelTypeEnum.XLS;
}
if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
excelTypeEnum = ExcelTypeEnum.XLSX;
}
null) {
new ExcelReader(in, excelTypeEnum, customContent, eventListener);
}
);
}
/**
* @param in
* 文件輸入流
* @param customContent
* 自定義模型可以在
* {@link AnalysisEventListener#invoke(Object, AnalysisContext) }
* AnalysisContext中獲取用於監聽者回調使用
* @param eventListener
* 用戶監聽
* @param trim
* 是否對解析的String做trim()默認true,用於防止 excel中空格引起的裝換報錯。
* @throws IOException
* @throws EmptyFileException
* @throws InvalidFormatException
*/

getExcelReader(InputStream in, Object customContent,
AnalysisEventListener<?> eventListener, boolean trim)
throws EmptyFileException, IOException, InvalidFormatException {
// 如果輸入流不支持mark/reset,需要對其進行包裹
if (!in.markSupported()) {
in = 8);
}
// 確保至少有一些數據
byte[] header8 = IOUtils.peekFirst8Bytes(in);
ExcelTypeEnum excelTypeEnum = null;
if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
excelTypeEnum = ExcelTypeEnum.XLS;
}
if (DocumentFactoryHelper.hasOOXMLHeader(in)) {
excelTypeEnum = ExcelTypeEnum.XLSX;
}
null) {
new ExcelReader(in, excelTypeEnum, customContent, eventListener, trim);
}
);
}
}

下面我們就來寫一個簡單的讀取Excel的示例:


package test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.ExcelReaderFactory;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
ExcelReadTest {
@Test
throws Exception {
);) {
AnalysisEventListener<List<String>> listener = new AnalysisEventListener<List<String>>() {
@Override
invoke(List<String> object, AnalysisContext context) {
System.err.println( + object);
}
@Override
doAfterAllAnalysed(AnalysisContext context) {
System.err.println();
}
};
ExcelReader excelReader = ExcelReaderFactory.getExcelReader(in, null, listener);
excelReader.read();
}
}
}

正如寫入Excel的時候可以使用數據模型一樣,在讀取Excel的時候也可以直接將數據映射為模型對象,區別在於要使用 ExcelReader #read的重載方法。


package test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.ExcelReaderFactory;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
ExcelReadTest {
@Test
throws Exception {
);) {
AnalysisEventListener<ExcelPropertyIndexModel> listener = new AnalysisEventListener<ExcelPropertyIndexModel>() {
@Override
invoke(ExcelPropertyIndexModel object, AnalysisContext context) {
System.err.println( + object);
}
@Override
doAfterAllAnalysed(AnalysisContext context) {
System.err.println();
}
};
ExcelReader excelReader = ExcelReaderFactory.getExcelReader(in, null, listener);
// 第二個參數為表頭行數,按照實際設置
excelReader.read(1, ExcelPropertyIndexModel.class));
}
}
BaseRowModel {

0)
private String name;
1)
private String age;
2)
private String email;
3)
private String address;
4)
private String sax;
5)
private String heigh;
6)
private String last;
getName() {
return name;
}
setName(String name) {
this.name = name;
}
getAge() {
return age;
}
setAge(String age) {
this.age = age;
}
getEmail() {
return email;
}
setEmail(String email) {
this.email = email;
}
getAddress() {
return address;
}
setAddress(String address) {
this.address = address;
}
getSax() {
return sax;
}
setSax(String sax) {
this.sax = sax;
}
getHeigh() {
return heigh;
}
setHeigh(String heigh) {
this.heigh = heigh;
}
getLast() {
return last;
}
setLast(String last) {
this.last = last;
}
@Override
toString() {
+ address
+ ;
}
}
}

以上就是關於easyexcel的使用方法介紹,如有疑問,歡迎交流指正。


原文出處: JAVA使用easyexcel操作Excel - 蒋固金(jianggujin)的专栏 - CSDN博客
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|