日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java使用easyExcel操作Excel案例

發布時間:2023/12/10 java 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 {@Testpublic void testRead() throws FileNotFoundException {InputStream inputStream =getInputStream("C:\\Users\\LiGe\\Desktop\\test.xls");try {ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, new AnalysisEventListener() {@Overridepublic void invoke(Object o, AnalysisContext analysisContext) {System.out.println("當前sheet"+analysisContext.getCurrentSheet().getSheetNo()+ " 當前行:" + analysisContext.getCurrentRowNum()+ " data:" + o);}@Overridepublic void doAfterAllAnalysed(AnalysisContext analysisContext) {}});reader.read();}catch (Exception e){e.printStackTrace();}finally {try {inputStream.close();}catch (IOException e){e.printStackTrace();}}}@Testpublic void testWriter() throws FileNotFoundException {OutputStream out = new FileOutputStream("C:\\Users\\LiGe\\Desktop\\test.xls");try {ExcelWriter writer = new ExcelWriter(out,ExcelTypeEnum.XLS);//寫第一個sheetSheet 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案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。