|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1729595
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2019/7/11 16:37 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]Java 使用 easyExcel 操作 Excel 案例
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博客
|
|
討論串
|