项目作者: freegeese

项目描述 :
像SQL一样操作Excel,简化Excel的读写操作
高级语言: Java
项目地址: git://github.com/freegeese/easy-excel.git
创建时间: 2016-11-17T11:54:55Z
项目社区:https://github.com/freegeese/easy-excel

开源协议:

下载


像SQL一样操作Excel

  • 简化对Excel的读写操作
  • 尽可能的实现能够像SQL一样操作来操作Excel

快速上手

对Excel的简单read,使用SimpleReader类完成

  1. package com.geese.plugin.excelMapping.test;
  2. import com.geese.plugin.excelMapping.filter.read.CellAfterReadFilter;
  3. import com.geese.plugin.excelMapping.filter.read.RowAfterReadFilter;
  4. import com.geese.plugin.excelMapping.SimpleReader;
  5. import com.geese.plugin.excelMapping.SimpleWriter;
  6. import com.geese.plugin.excelMapping.config.Point;
  7. import com.geese.plugin.excelMapping.config.Table;
  8. import com.geese.plugin.excelMapping.filter.read.CellBeforeReadFilter;
  9. import com.geese.plugin.excelMapping.filter.read.RowBeforeReadFilter;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.Row;
  12. import org.junit.AfterClass;
  13. import org.junit.BeforeClass;
  14. import org.junit.Test;
  15. import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.net.URL;
  20. import java.util.*;
  21. /**
  22. * SimpleReader 接口测试
  23. *
  24. * @author zhangguangyong <a href="#">1243610991@qq.com</a>
  25. * @date 2016/11/16 18:05
  26. * @sine 0.0.1
  27. */
  28. public class SimpleReaderTest {
  29. private static InputStream input;
  30. @BeforeClass
  31. public static void beforeClass() throws IOException {
  32. URL url = Thread.currentThread().getContextClassLoader().getResource("demo-reader.xlsx");
  33. // 准备数据
  34. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  35. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  36. String names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华";
  37. List<Map> tableData = new ArrayList<>();
  38. Map rowData;
  39. for (String name : names.split("\\s+")) {
  40. rowData = new HashMap();
  41. rowData.put("name", name);
  42. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  43. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  44. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  45. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  46. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  47. tableData.add(rowData);
  48. }
  49. // 构建一个输出流,向被读取的excel写入测试数据
  50. FileOutputStream output = new FileOutputStream(url.getFile());
  51. SimpleWriter.build(output)
  52. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  53. .limit(1)
  54. .addData(tableData)
  55. .execute();
  56. output.flush();
  57. output.close();
  58. // 构建一个输入流,读取excel数据
  59. input = new FileInputStream(url.getFile());
  60. }
  61. @AfterClass
  62. public static void afterClass() throws IOException {
  63. if (null != input) {
  64. input.close();
  65. }
  66. }
  67. /**
  68. * 实例1:快速使用
  69. */
  70. @Test
  71. public void test001() {
  72. Collection result = SimpleReader.build(input)
  73. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  74. .execute();
  75. System.out.println(result);
  76. }
  77. /**
  78. * 实力2:可选配置
  79. */
  80. @Test
  81. public void test002() {
  82. Collection result = SimpleReader.build(input)
  83. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  84. .from("0") // 可选(默认是从第0个sheet读取, [如果是数字会优先使用名称获取sheet,没找到才会使用下标获取sheet])
  85. .limit(3, 5) // 可选(默认从定义0行开始读取,读取所有行)
  86. .execute();
  87. System.out.println(result.size());
  88. }
  89. /**
  90. * 实例3:where条件帅选,使用占位符参数
  91. */
  92. @Test
  93. public void test003() {
  94. // 占位符参数
  95. Collection result = SimpleReader.build(input)
  96. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  97. .where("name like ? and (age > ? and qq like ?) or name in ?")
  98. .addParameter(Arrays.asList("%鲁%", 50, "5%", Arrays.asList("张白萱", "张若云", "张雅畅", "张雅寒", "张雨华")))
  99. .execute();
  100. System.out.println(result.size());
  101. }
  102. /**
  103. * 实例4:where条件帅选,使用命名的参数
  104. */
  105. @Test
  106. public void test004() {
  107. // 命名的参数
  108. Map namedParameter = new HashMap();
  109. namedParameter.put("name", "%鲁%");
  110. namedParameter.put("age", 26);
  111. namedParameter.put("qq", "5%");
  112. namedParameter.put("names", Arrays.asList("张白萱", "张若云", "张雅畅", "张雅寒", "张雨华"));
  113. Collection result = SimpleReader.build(input)
  114. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  115. .where("name like :name and (age > :age and qq like :qq) or name in :names")
  116. .addParameter(namedParameter)
  117. .execute();
  118. System.out.println(result.size());
  119. }
  120. /**
  121. * 实例5:过滤器
  122. */
  123. @Test
  124. public void test005() {
  125. Collection result = SimpleReader.build(input)
  126. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone")
  127. .addFilter(new RowBeforeReadFilter() {
  128. // 可以对 row 进行修改
  129. @Override
  130. public void doFilter(Row target, Object data, Table config) {
  131. System.out.println("<<<<<<<<<<<<<<<<读取行之前过滤:" + data);
  132. }
  133. }, new RowAfterReadFilter() {
  134. // 可以对 data 进行修改
  135. @Override
  136. public void doFilter(Row target, Object data, Table config) {
  137. System.out.println("读取行之后过滤:" + data + ">>>>>>>>>>>>>>>>>");
  138. }
  139. }, new CellBeforeReadFilter() {
  140. // 可以对 cell 进行修改
  141. @Override
  142. public void doFilter(Cell target, Object data, Point config) {
  143. System.out.println("<<<<<<<<<<<<<<<<读单元格之前过滤:" + data);
  144. }
  145. }, new CellAfterReadFilter() {
  146. // 可以对 data 进行修改
  147. @Override
  148. public void doFilter(Cell target, Object data, Point config) {
  149. System.out.println("读单元格之后过滤:" + data + ">>>>>>>>>>>>>>>>>");
  150. }
  151. })
  152. .execute();
  153. System.out.println(result);
  154. }
  155. }

对Excel的简单write,使用SimpleWriter类完成

  1. package com.geese.plugin.excelMapping.test;
  2. import com.geese.plugin.excelMapping.SimpleWriter;
  3. import com.geese.plugin.excelMapping.config.Point;
  4. import com.geese.plugin.excelMapping.config.Table;
  5. import com.geese.plugin.excelMapping.filter.CellWriteFilter;
  6. import com.geese.plugin.excelMapping.filter.RowWriteFilter;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. import java.io.*;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * SimpleWriter 接口测试
  19. *
  20. * @author zhangguangyong <a href="#">1243610991@qq.com</a>
  21. * @date 2016/11/16 18:06
  22. * @sine 0.0.1
  23. */
  24. public class SimpleWriterTest {
  25. private static OutputStream output;
  26. private static InputStream template;
  27. @BeforeClass
  28. public static void beforeClass() throws IOException {
  29. // 输出
  30. URL url = Thread.currentThread().getContextClassLoader().getResource("demo-writer.xlsx");
  31. output = new FileOutputStream(url.getFile());
  32. // 模板
  33. url = Thread.currentThread().getContextClassLoader().getResource("demo-writer-template.xlsx");
  34. template = new FileInputStream(url.getFile());
  35. }
  36. /**
  37. * 实例1:快速使用
  38. */
  39. @Test
  40. public void test001() {
  41. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  42. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  43. String names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华";
  44. List<Map> tableData = new ArrayList<>();
  45. Map rowData;
  46. for (String name : names.split("\\s+")) {
  47. rowData = new HashMap();
  48. rowData.put("name", name);
  49. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  50. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  51. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  52. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  53. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  54. tableData.add(rowData);
  55. }
  56. // 通过SimpleWriter类操作
  57. SimpleWriter.build(output) // 必选,将生成的excel输出到什么地方
  58. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone") // 必选,数据Key与Excel列的映射
  59. .addData(tableData) // 必选,插入的数据
  60. .execute(); // 执行
  61. }
  62. /**
  63. * 实例2:使用可选配置
  64. */
  65. @Test
  66. public void test002() {
  67. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  68. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  69. String names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华";
  70. List<Map> tableData = new ArrayList<>();
  71. Map rowData;
  72. for (String name : names.split("\\s+")) {
  73. rowData = new HashMap();
  74. rowData.put("name", name);
  75. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  76. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  77. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  78. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  79. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  80. tableData.add(rowData);
  81. }
  82. // 通过SimpleWriter类操作
  83. SimpleWriter.build(output) // 必选,将生成的excel输出到什么地方
  84. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone") // 必选,数据Key与Excel列的映射
  85. .into("0xx") // 可选(默认:插入到第0个sheet, 可以指定名称,比如:xx数据报表)
  86. .limit(0, 10) // 可选(参数1:从哪行开始插入,参数2:插入多少航,默认:0,tableData.size())
  87. .addData(tableData) // 必选,插入的数据
  88. .execute(); // 执行
  89. }
  90. /**
  91. * 实例3:使用单元格过滤器和行过滤器
  92. */
  93. @Test
  94. public void test003() {
  95. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  96. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  97. String names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华";
  98. List<Map> tableData = new ArrayList<>();
  99. Map rowData;
  100. for (String name : names.split("\\s+")) {
  101. rowData = new HashMap();
  102. rowData.put("name", name);
  103. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  104. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  105. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  106. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  107. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  108. tableData.add(rowData);
  109. }
  110. /**
  111. * 写入到row之前过滤,可对row和data进行修改
  112. */
  113. RowWriteFilter rowWriteFilter = new RowWriteFilter() {
  114. @Override
  115. public void doFilter(Row target, Object data, Table config) {
  116. System.out.println(data);
  117. }
  118. };
  119. /**
  120. * 写入到cell之前过滤,可对cell和data进行修改
  121. */
  122. CellWriteFilter cellWriteFilter = new CellWriteFilter() {
  123. @Override
  124. public void doFilter(Cell target, Object data, Point config) {
  125. System.out.println(data);
  126. }
  127. };
  128. // 通过SimpleWriter类操作
  129. SimpleWriter.build(output) // 必选,将生成的excel输出到什么地方
  130. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone") // 必选,数据Key与Excel列的映射
  131. .addFilter(rowWriteFilter, cellWriteFilter)
  132. .addData(tableData) // 必选,插入的数据
  133. .execute(); // 执行
  134. }
  135. /**
  136. * 实例4:使用模板
  137. */
  138. @Test
  139. public void test004() {
  140. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  141. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  142. String names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华";
  143. List<Map> tableData = new ArrayList<>();
  144. Map rowData;
  145. for (String name : names.split("\\s+")) {
  146. rowData = new HashMap();
  147. rowData.put("name", name);
  148. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  149. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  150. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  151. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  152. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  153. tableData.add(rowData);
  154. }
  155. // 通过SimpleWriter类操作
  156. SimpleWriter.build(output, template) // 必选,将按照模板去生成excel
  157. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone") // 必选,数据Key与Excel列的映射
  158. .limit(1)
  159. .addData(tableData) // 必选,插入的数据
  160. .execute(); // 执行
  161. }
  162. }

接口说明

  • 暂无

高级部分

StandardReader 接口使用

  1. package com.geese.plugin.excelMapping.test;
  2. import com.geese.plugin.excelMapping.StandardReader;
  3. import com.geese.plugin.excelMapping.StandardWriter;
  4. import com.geese.plugin.excelMapping.config.Point;
  5. import com.geese.plugin.excelMapping.config.Table;
  6. import com.geese.plugin.excelMapping.filter.read.CellAfterReadFilter;
  7. import com.geese.plugin.excelMapping.filter.read.CellBeforeReadFilter;
  8. import com.geese.plugin.excelMapping.filter.read.RowAfterReadFilter;
  9. import com.geese.plugin.excelMapping.filter.read.RowBeforeReadFilter;
  10. import org.apache.poi.ss.usermodel.Cell;
  11. import org.apache.poi.ss.usermodel.Row;
  12. import org.junit.BeforeClass;
  13. import org.junit.Test;
  14. import java.io.FileInputStream;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.net.URL;
  19. import java.util.*;
  20. /**
  21. * <p> 标准Excel读取接口测试 <br>
  22. *
  23. * @author zhangguangyong <a href="#">1243610991@qq.com</a>
  24. * @date 2016/11/16 22:36
  25. * @sine 0.0.1
  26. */
  27. public class StandardReaderTest {
  28. static InputStream input;
  29. @BeforeClass
  30. public static void beforeClass() throws IOException {
  31. URL url = Thread.currentThread().getContextClassLoader().getResource("demo-reader.xlsx");
  32. // 准备数据
  33. // Excel 表头: 姓名 | 年龄 | 身份证号 | QQ | 邮箱 | 手机
  34. // 准备数据, 每一行是一个Map, 每一个表格是一个List<Map>
  35. String[] names = "鲁沛儿 鲁天薇 鲁飞雨 鲁天纵 鲁白梦 鲁嘉胜 鲁盼巧 鲁访天 鲁清妍 鲁盼晴 张馨蓉 张白萱 张若云 张雅畅 张雅寒 张雨华".split("\\s+");
  36. List<Map> tableData = new ArrayList<>();
  37. Map rowData;
  38. for (int i = 0; i < 100; i++) {
  39. rowData = new HashMap();
  40. rowData.put("name", names[Double.valueOf(Math.random() * names.length).intValue()]);
  41. rowData.put("age", Double.valueOf(Math.random() * 100).intValue());
  42. rowData.put("idCard", Double.valueOf(Math.random() * 1000000000).longValue());
  43. rowData.put("qq", Double.valueOf(Math.random() * 1000000000).longValue());
  44. rowData.put("email", Double.valueOf(Math.random() * 1000000000).longValue() + "@163.com");
  45. rowData.put("phone", Double.valueOf(Math.random() * 1000000000).longValue());
  46. tableData.add(rowData);
  47. }
  48. // 构建一个输出流,向被读取的excel写入测试数据
  49. FileOutputStream output = new FileOutputStream(url.getFile());
  50. StandardWriter.build(output)
  51. .insert(
  52. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into 0",
  53. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into 1"
  54. )
  55. .addData("0", 0, tableData)
  56. .addData("1", 0, tableData)
  57. .execute();
  58. output.flush();
  59. output.close();
  60. // 构建一个输入流,读取excel数据
  61. input = new FileInputStream(url.getFile());
  62. }
  63. /**
  64. * 实例1:快速上手
  65. */
  66. @Test
  67. public void test001() {
  68. Object result = StandardReader
  69. .build(input)
  70. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone from 0")
  71. .execute();
  72. System.out.println(result);
  73. }
  74. /**
  75. * 实例2:可选配置
  76. */
  77. @Test
  78. public void test002() {
  79. StandardReader
  80. .build(input)
  81. // limit: [startRow, size] 从哪行开始读,读取多少行
  82. .select("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone from 0 limit 0, 10")
  83. // 绑定过滤器到一个sheet上
  84. .addFilter("0", new RowBeforeReadFilter() {
  85. @Override
  86. public void doFilter(Row target, Object data, Table config) {
  87. System.out.println("<><><><><><><>读取Row之前过滤:" + data + "<><><><><><><>");
  88. }
  89. }, new RowAfterReadFilter() {
  90. @Override
  91. public void doFilter(Row target, Object data, Table config) {
  92. System.out.println("<><><><><><><>读取Row之后过滤:" + data + "<><><><><><><>");
  93. }
  94. }, new CellBeforeReadFilter() {
  95. @Override
  96. public void doFilter(Cell target, Object data, Point config) {
  97. System.out.println("<><><><><><><>读取Cell之前过滤:" + data + "<><><><><><><>");
  98. }
  99. }, new CellAfterReadFilter() {
  100. @Override
  101. public void doFilter(Cell target, Object data, Point config) {
  102. System.out.println("<><><><><><><>读取Cell之后过滤:" + data + "<><><><><><><>");
  103. }
  104. })
  105. .execute();
  106. }
  107. /**
  108. * 实例3:where 条件过滤
  109. */
  110. @Test
  111. public void test003() {
  112. Map namedParameter = new HashMap();
  113. namedParameter.put("name", "鲁%");
  114. namedParameter.put("age", 20);
  115. namedParameter.put("qq", "%12%");
  116. namedParameter.put("names", Arrays.asList("张馨蓉", "张白萱", "张若云"));
  117. Object result = StandardReader
  118. .build(input)
  119. // where 条件过滤,支持占位符和命名参数
  120. .select(
  121. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone from 0 where name like ? and (age > ? or qq like ? or name in ?)",
  122. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone from 1 where name like :name and (age > :age or qq like :qq or name in :names)"
  123. )
  124. // 添加占位符参数
  125. .addParameter("0", 0, Arrays.asList("鲁%", 20, "%12%", Arrays.asList("张馨蓉", "张白萱", "张若云")))
  126. // 添加命名的参数
  127. .addParameter("1", 0, namedParameter)
  128. .execute();
  129. System.out.println(result);
  130. }
  131. /**
  132. * 实例4:散列点
  133. */
  134. @Test
  135. public void test004() {
  136. Object result = StandardReader
  137. .build(input)
  138. .select(
  139. "{0-1 name, 0-2 age from 0}",
  140. "{1-1 name, 1-2 age from 1}"
  141. )
  142. .execute();
  143. System.out.println(result);
  144. }
  145. }

StandardWriter 接口使用

  1. package com.geese.plugin.excelMapping.test;
  2. import com.geese.plugin.excelMapping.StandardWriter;
  3. import com.geese.plugin.excelMapping.config.Point;
  4. import com.geese.plugin.excelMapping.config.Table;
  5. import com.geese.plugin.excelMapping.filter.CellWriteFilter;
  6. import com.geese.plugin.excelMapping.filter.RowWriteFilter;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.Row;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. import java.io.*;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * <p> 标准Excel写入接口测试 <br>
  19. *
  20. * @author zhangguangyong <a href="#">1243610991@qq.com</a>
  21. * @date 2016/11/16 21:41
  22. * @sine 0.0.1
  23. */
  24. public class StandardWriterTest {
  25. private static OutputStream output;
  26. private static InputStream template;
  27. @BeforeClass
  28. public static void beforeClass() throws IOException {
  29. // 输出
  30. URL url = Thread.currentThread().getContextClassLoader().getResource("demo-writer.xlsx");
  31. output = new FileOutputStream(url.getFile());
  32. // 模板
  33. url = Thread.currentThread().getContextClassLoader().getResource("demo-writer-template.xlsx");
  34. template = new FileInputStream(url.getFile());
  35. }
  36. /**
  37. * 实例1:快速上手
  38. */
  39. @Test
  40. public void test001() {
  41. // 准备表格数据
  42. List tableData = new ArrayList();
  43. Map rowData;
  44. for (int i = 0; i < 50; i++) {
  45. rowData = new HashMap();
  46. rowData.put("name", "隔壁老王" + i);
  47. rowData.put("age", Double.valueOf((Math.random() * 100)).intValue());
  48. rowData.put("idCard", Double.valueOf((Math.random() * 1000000000)).longValue());
  49. rowData.put("qq", Double.valueOf((Math.random() * 100000000)).longValue());
  50. rowData.put("email", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  51. rowData.put("phone", Double.valueOf((Math.random() * 1000000000)).longValue());
  52. tableData.add(rowData);
  53. }
  54. // 把数据插入到excel对应的位置
  55. StandardWriter.build(output)
  56. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into Sheet1")
  57. .addData("Sheet1", 0, tableData)
  58. .execute();
  59. }
  60. /**
  61. * 实例11:可选配置项
  62. */
  63. @Test
  64. public void test002() {
  65. // 准备表格数据
  66. List tableData = new ArrayList();
  67. Map rowData;
  68. for (int i = 0; i < 50; i++) {
  69. rowData = new HashMap();
  70. rowData.put("name", "隔壁老王" + i);
  71. rowData.put("age", Double.valueOf((Math.random() * 100)).intValue());
  72. rowData.put("idCard", Double.valueOf((Math.random() * 1000000000)).longValue());
  73. rowData.put("qq", Double.valueOf((Math.random() * 100000000)).longValue());
  74. rowData.put("email", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  75. rowData.put("phone", Double.valueOf((Math.random() * 1000000000)).longValue());
  76. tableData.add(rowData);
  77. }
  78. // 把数据插入到excel对应的位置
  79. StandardWriter
  80. // template: 使用模板来接收写入的数据
  81. .build(output, template)
  82. // limit:[startRow, size] 从哪行开始写,写多少行 默认:[0, tableData.size()]
  83. .insert("0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into Sheet1 limit 1, 30")
  84. // filter: 在数据写入到row或cell之前,可以对数据进行过滤修改, 过滤器需要绑定到某个Sheet上执行
  85. .addFilter("Sheet1", new RowWriteFilter() {
  86. @Override
  87. public void doFilter(Row target, Object data, Table config) {
  88. System.out.println("<<<<<<<<<<<<写入Row之前过滤:" + data + ">>>>>>>>>>>");
  89. }
  90. }, new CellWriteFilter() {
  91. @Override
  92. public void doFilter(Cell target, Object data, Point config) {
  93. System.out.println("<<<<<<<<<<<<写入Cell之前过滤:" + data + ">>>>>>>>>>>");
  94. }
  95. })
  96. .addData("Sheet1", 0, tableData)
  97. .execute();
  98. }
  99. /**
  100. * 实例3:列表 + 散列点
  101. */
  102. @Test
  103. public void test003() {
  104. // 准备表格数据
  105. List tableData = new ArrayList();
  106. Map rowData;
  107. for (int i = 0; i < 50; i++) {
  108. rowData = new HashMap();
  109. rowData.put("name", "隔壁老王" + i);
  110. rowData.put("age", Double.valueOf((Math.random() * 100)).intValue());
  111. rowData.put("idCard", Double.valueOf((Math.random() * 1000000000)).longValue());
  112. rowData.put("qq", Double.valueOf((Math.random() * 100000000)).longValue());
  113. rowData.put("email", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  114. rowData.put("phone", Double.valueOf((Math.random() * 1000000000)).longValue());
  115. tableData.add(rowData);
  116. }
  117. // 准备散列点数据 EASY-EXCEL
  118. Map pointData = new HashMap();
  119. pointData.put("e", "E");
  120. pointData.put("a", "A");
  121. pointData.put("s", "S");
  122. pointData.put("y", "Y");
  123. pointData.put("x", "X");
  124. pointData.put("c", "C");
  125. pointData.put("l", "L");
  126. // 把数据插入到excel对应的位置
  127. StandardWriter.build(output)
  128. .insert(
  129. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into Sheet1",
  130. "{0-8 e, 0-9 a, 0-10 s, 0-11 y, 1-8 e, 1-9 x, 1-10 c, 1-11 e, 1-12 l into Sheet1}"
  131. )
  132. .addData("Sheet1", 0, tableData)
  133. .addData("Sheet1", pointData)
  134. .execute();
  135. }
  136. /**
  137. * 实例4:多个表格 + 散列点
  138. */
  139. @Test
  140. public void test004() {
  141. // 准备表格数据
  142. List tableData1 = new ArrayList();
  143. Map rowData;
  144. for (int i = 0; i < 50; i++) {
  145. rowData = new HashMap();
  146. rowData.put("name", "隔壁老王" + i);
  147. rowData.put("age", Double.valueOf((Math.random() * 100)).intValue());
  148. rowData.put("idCard", Double.valueOf((Math.random() * 1000000000)).longValue());
  149. rowData.put("qq", Double.valueOf((Math.random() * 100000000)).longValue());
  150. rowData.put("email", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  151. rowData.put("phone", Double.valueOf((Math.random() * 1000000000)).longValue());
  152. tableData1.add(rowData);
  153. }
  154. List tableData2 = new ArrayList();
  155. for (int i = 0; i < 20; i++) {
  156. rowData = new HashMap();
  157. rowData.put("name1", "隔壁老王" + i);
  158. rowData.put("age1", Double.valueOf((Math.random() * 100)).intValue());
  159. rowData.put("idCard1", Double.valueOf((Math.random() * 1000000000)).longValue());
  160. rowData.put("qq1", Double.valueOf((Math.random() * 100000000)).longValue());
  161. rowData.put("email1", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  162. rowData.put("phone1", Double.valueOf((Math.random() * 1000000000)).longValue());
  163. tableData2.add(rowData);
  164. }
  165. // 准备散列点数据 EASY-EXCEL
  166. Map pointData = new HashMap();
  167. pointData.put("e", "E");
  168. pointData.put("a", "A");
  169. pointData.put("s", "S");
  170. pointData.put("y", "Y");
  171. pointData.put("x", "X");
  172. pointData.put("c", "C");
  173. pointData.put("l", "L");
  174. // 把数据插入到excel对应的位置
  175. StandardWriter.build(output)
  176. .insert(
  177. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into Sheet1",
  178. "7 name1, 8 age1, 9 idCard1, 10 qq1, 11 email1, 12 phone1 into Sheet1 limit 5",
  179. "{0-8 e, 0-9 a, 0-10 s, 0-11 y, 1-8 e, 1-9 x, 1-10 c, 1-11 e, 1-12 l into Sheet1}"
  180. )
  181. .addData("Sheet1", 0, tableData1)
  182. .addData("Sheet1", 1, tableData2)
  183. .addData("Sheet1", pointData)
  184. .execute();
  185. }
  186. /**
  187. * 实例5:多sheet插入
  188. */
  189. @Test
  190. public void test005() {
  191. // 准备表格数据
  192. List tableData1 = new ArrayList();
  193. Map rowData;
  194. for (int i = 0; i < 50; i++) {
  195. rowData = new HashMap();
  196. rowData.put("name", "隔壁老王" + i);
  197. rowData.put("age", Double.valueOf((Math.random() * 100)).intValue());
  198. rowData.put("idCard", Double.valueOf((Math.random() * 1000000000)).longValue());
  199. rowData.put("qq", Double.valueOf((Math.random() * 100000000)).longValue());
  200. rowData.put("email", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  201. rowData.put("phone", Double.valueOf((Math.random() * 1000000000)).longValue());
  202. tableData1.add(rowData);
  203. }
  204. List tableData2 = new ArrayList();
  205. for (int i = 0; i < 20; i++) {
  206. rowData = new HashMap();
  207. rowData.put("name1", "隔壁老王" + i);
  208. rowData.put("age1", Double.valueOf((Math.random() * 100)).intValue());
  209. rowData.put("idCard1", Double.valueOf((Math.random() * 1000000000)).longValue());
  210. rowData.put("qq1", Double.valueOf((Math.random() * 100000000)).longValue());
  211. rowData.put("email1", Double.valueOf((Math.random() * 1000000)).longValue() + "@qq.com");
  212. rowData.put("phone1", Double.valueOf((Math.random() * 1000000000)).longValue());
  213. tableData2.add(rowData);
  214. }
  215. // 准备散列点数据 EASY-EXCEL
  216. Map pointData = new HashMap();
  217. pointData.put("e", "E");
  218. pointData.put("a", "A");
  219. pointData.put("s", "S");
  220. pointData.put("y", "Y");
  221. pointData.put("x", "X");
  222. pointData.put("c", "C");
  223. pointData.put("l", "L");
  224. // 把数据插入到excel对应的位置
  225. StandardWriter.build(output)
  226. .insert(
  227. "0 name, 1 age, 2 idCard, 3 qq, 4 email, 5 phone into 0",
  228. "7 name1, 8 age1, 9 idCard1, 10 qq1, 11 email1, 12 phone1 into 1 limit 5",
  229. "{0-8 e, 0-9 a, 0-10 s, 0-11 y, 1-8 e, 1-9 x, 1-10 c, 1-11 e, 1-12 l into 0}",
  230. "{0-8 e, 0-9 a, 0-10 s, 0-11 y, 1-8 e, 1-9 x, 1-10 c, 1-11 e, 1-12 l into 1}"
  231. )
  232. .addData("0", 0, tableData1)
  233. .addData("1", 0, tableData2)
  234. .addData("0", pointData)
  235. .addData("1", pointData)
  236. .execute();
  237. }
  238. }