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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00270.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/7/11 16:37:55

Java使用easyExcel操作Excel案例

這兩天一直在玩些小工具,今天整了下阿里巴巴的easyExcel,下面是案例:



import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TestExcel {
@Test
public void testRead() throws FileNotFoundException {
InputStream inputStream =getInputStream("C:\\Users\\LiGe\\Desktop\\test.xls");
try {
ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, new AnalysisEventListener() {
@Override
public void invoke(Object o, AnalysisContext analysisContext) {
System.out.println("當前sheet"+analysisContext.getCurrentSheet().getSheetNo()+ " 當前行:" + analysisContext.getCurrentRowNum()
+ " data:" + o);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
});
reader.read();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
inputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
@Test
public void testWriter() throws FileNotFoundException {
OutputStream out = new FileOutputStream("C:\\Users\\LiGe\\Desktop\\test.xls");
try {
ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLS);
//寫第一個sheet
Sheet sheet = new Sheet(2,3,ImportInfo.class);
writer.write(getDate(),sheet);
for (ImportInfo in: getDate()
) {
System.out.println(in.getName());
}
writer.finish();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public List<ImportInfo> getDate(){
List<ImportInfo> list = new ArrayList<ImportInfo>();
ImportInfo info = new ImportInfo();
info.setAge(12);
info.setName("zhangsan");
info.setEmail("11111@qq.com");
ImportInfo info1 = new ImportInfo();
info1.setAge(12);
info1.setName("zhangsan1");
info1.setEmail("11111@qq.com");
ImportInfo info2 = new ImportInfo();
info2.setAge(12);
info2.setName("zhangsan2");
info2.setEmail("11111@qq.com");
list.add(info);list.add(info1);list.add(info2);
return list;
}
private InputStream getInputStream(String fileName) {
try {
return new FileInputStream(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}

上面是測試類,這是實體類:



import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
public class ImportInfo extends BaseRowModel {
@ExcelProperty(index = 0)
private String name;
@ExcelProperty(index = 1)
private Integer age;
@ExcelProperty(index = 2)
private String email;
/*
通過 @ExcelProperty 註解與 index 變量可以標注成員變量所映射的列
作為Excel的模型對像,需要setter方法
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}


原文出處: Java使用easyExcel操作Excel案例 - IT人利哥的博客 - CSDN博客
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

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