change line sep

This commit is contained in:
Looly
2021-01-20 17:10:45 +08:00
parent 720d24566b
commit 4e38adb32d
1450 changed files with 183940 additions and 183940 deletions

View File

@@ -1,249 +1,249 @@
package cn.hutool.poi.excel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 写出Excel单元测试
*
* @author looly
*/
public class BigExcelWriteTest {
@Test
@Ignore
public void writeTest2() {
List<String> row = CollUtil.newArrayList("姓名", "加班日期", "下班时间", "加班时长", "餐补", "车补次数", "车补", "总计");
BigExcelWriter overtimeWriter = ExcelUtil.getBigWriter("e:/excel/single_line.xlsx");
overtimeWriter.write(row);
overtimeWriter.close();
}
@Test
@Ignore
public void writeTest() {
List<?> row1 = CollUtil.newArrayList("aaaaa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
for(int i=0; i < 400000; i++) {
//超大列表写出测试
rows.add(ObjectUtil.clone(row1));
}
String filePath = "e:/bigWriteTest.xlsx";
FileUtil.del(filePath);
// 通过工具类创建writer
BigExcelWriter writer = ExcelUtil.getBigWriter(filePath);
// // 跳过当前行,即第一行,非必须,在此演示用
// writer.passCurrentRow();
// // 合并单元格后的标题行,使用默认标题样式
// writer.merge(row1.size() - 1, "大数据测试标题");
// 一次性写出内容,使用默认样式
writer.write(rows);
// writer.autoSizeColumn(0, true);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void mergeTest() {
List<?> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
// 通过工具类创建writer
BigExcelWriter writer = ExcelUtil.getBigWriter("e:/mergeTest.xlsx");
CellStyle style = writer.getStyleSet().getHeadCellStyle();
StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND);
// 跳过当前行,即第一行,非必须,在此演示用
writer.passCurrentRow();
// 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "测试标题");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 合并单元格后的标题行,使用默认标题样式
writer.merge(7, 10, 4, 10, "测试Merge", false);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeMapTest() {
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer
String path = "e:/bigWriteMapTest.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
//设置内容字体
Font font = writer.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED);
font.setItalic(true);
writer.getStyleSet().setFont(font, true);
// 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeMapTest2() {
Map<String, Object> row1 = MapUtil.newHashMap(true);
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
// 通过工具类创建writer
String path = "e:/bigWriteMapTest2.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
// 一次性写出内容,使用默认样式
writer.writeRow(row1, true);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeBeanTest() {
cn.hutool.poi.excel.TestBean bean1 = new cn.hutool.poi.excel.TestBean();
bean1.setName("张三");
bean1.setAge(22);
bean1.setPass(true);
bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date());
cn.hutool.poi.excel.TestBean bean2 = new cn.hutool.poi.excel.TestBean();
bean2.setName("李四");
bean2.setAge(28);
bean2.setPass(false);
bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date());
List<cn.hutool.poi.excel.TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通过工具类创建writer
String file = "e:/bigWriteBeanTest.xlsx";
FileUtil.del(file);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
//自定义标题
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("score", "分数");
writer.addHeaderAlias("isPass", "是否通过");
writer.addHeaderAlias("examDate", "考试时间");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(4, "一班成绩单");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeCellValueTest() {
String path = "d:/test/cellValueTest.xlsx";
FileUtil.del(path);
BigExcelWriter writer = new BigExcelWriter(path);
writer.writeCellValue(3, 5, "aaa");
writer.close();
}
@Test
@Ignore
public void closeTest() {
final Map<String, ?> map1 = MapUtil.of("id", "123456");
final Map<String, ?> map2 = MapUtil.of("id", "123457");
final List<?> data = Arrays.asList(map1, map2);
final String destFilePath = "d:/test/closeTest.xlsx";//略
FileUtil.del(destFilePath);
try (ExcelWriter writer = ExcelUtil.getBigWriter(destFilePath)) {
writer.write(data).flush();
}
}
@Test
@Ignore
public void issue1210() {
// 通过工具类创建writer
String path = "d:/test/issue1210.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
writer.addHeaderAlias("id", "SN");
writer.addHeaderAlias("userName", "User Name");
List<Map<String, Object>> list = new ArrayList<>();
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 1);
put("userName", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}});
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 2);
put("userName", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}});
writer.write(list, true);
writer.autoSizeColumnAll();
writer.close();
}
}
package cn.hutool.poi.excel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 写出Excel单元测试
*
* @author looly
*/
public class BigExcelWriteTest {
@Test
@Ignore
public void writeTest2() {
List<String> row = CollUtil.newArrayList("姓名", "加班日期", "下班时间", "加班时长", "餐补", "车补次数", "车补", "总计");
BigExcelWriter overtimeWriter = ExcelUtil.getBigWriter("e:/excel/single_line.xlsx");
overtimeWriter.write(row);
overtimeWriter.close();
}
@Test
@Ignore
public void writeTest() {
List<?> row1 = CollUtil.newArrayList("aaaaa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
for(int i=0; i < 400000; i++) {
//超大列表写出测试
rows.add(ObjectUtil.clone(row1));
}
String filePath = "e:/bigWriteTest.xlsx";
FileUtil.del(filePath);
// 通过工具类创建writer
BigExcelWriter writer = ExcelUtil.getBigWriter(filePath);
// // 跳过当前行,即第一行,非必须,在此演示用
// writer.passCurrentRow();
// // 合并单元格后的标题行,使用默认标题样式
// writer.merge(row1.size() - 1, "大数据测试标题");
// 一次性写出内容,使用默认样式
writer.write(rows);
// writer.autoSizeColumn(0, true);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void mergeTest() {
List<?> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
List<?> row2 = CollUtil.newArrayList("aa1", "bb1", "cc1", "dd1", DateUtil.date(), 250.7676);
List<?> row3 = CollUtil.newArrayList("aa2", "bb2", "cc2", "dd2", DateUtil.date(), 0.111);
List<?> row4 = CollUtil.newArrayList("aa3", "bb3", "cc3", "dd3", DateUtil.date(), 35);
List<?> row5 = CollUtil.newArrayList("aa4", "bb4", "cc4", "dd4", DateUtil.date(), 28.00);
List<List<?>> rows = CollUtil.newArrayList(row1, row2, row3, row4, row5);
// 通过工具类创建writer
BigExcelWriter writer = ExcelUtil.getBigWriter("e:/mergeTest.xlsx");
CellStyle style = writer.getStyleSet().getHeadCellStyle();
StyleUtil.setColor(style, IndexedColors.RED, FillPatternType.SOLID_FOREGROUND);
// 跳过当前行,即第一行,非必须,在此演示用
writer.passCurrentRow();
// 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "测试标题");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 合并单元格后的标题行,使用默认标题样式
writer.merge(7, 10, 4, 10, "测试Merge", false);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeMapTest() {
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("姓名", "李四");
row2.put("年龄", 33);
row2.put("成绩", 59.50);
row2.put("是否合格", false);
row2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
// 通过工具类创建writer
String path = "e:/bigWriteMapTest.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
//设置内容字体
Font font = writer.createFont();
font.setBold(true);
font.setColor(Font.COLOR_RED);
font.setItalic(true);
writer.getStyleSet().setFont(font, true);
// 合并单元格后的标题行,使用默认标题样式
writer.merge(row1.size() - 1, "一班成绩单");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeMapTest2() {
Map<String, Object> row1 = MapUtil.newHashMap(true);
row1.put("姓名", "张三");
row1.put("年龄", 23);
row1.put("成绩", 88.32);
row1.put("是否合格", true);
row1.put("考试日期", DateUtil.date());
// 通过工具类创建writer
String path = "e:/bigWriteMapTest2.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
// 一次性写出内容,使用默认样式
writer.writeRow(row1, true);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeBeanTest() {
cn.hutool.poi.excel.TestBean bean1 = new cn.hutool.poi.excel.TestBean();
bean1.setName("张三");
bean1.setAge(22);
bean1.setPass(true);
bean1.setScore(66.30);
bean1.setExamDate(DateUtil.date());
cn.hutool.poi.excel.TestBean bean2 = new cn.hutool.poi.excel.TestBean();
bean2.setName("李四");
bean2.setAge(28);
bean2.setPass(false);
bean2.setScore(38.50);
bean2.setExamDate(DateUtil.date());
List<cn.hutool.poi.excel.TestBean> rows = CollUtil.newArrayList(bean1, bean2);
// 通过工具类创建writer
String file = "e:/bigWriteBeanTest.xlsx";
FileUtil.del(file);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
//自定义标题
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("age", "年龄");
writer.addHeaderAlias("score", "分数");
writer.addHeaderAlias("isPass", "是否通过");
writer.addHeaderAlias("examDate", "考试时间");
// 合并单元格后的标题行,使用默认标题样式
writer.merge(4, "一班成绩单");
// 一次性写出内容,使用默认样式
writer.write(rows);
// 关闭writer释放内存
writer.close();
}
@Test
@Ignore
public void writeCellValueTest() {
String path = "d:/test/cellValueTest.xlsx";
FileUtil.del(path);
BigExcelWriter writer = new BigExcelWriter(path);
writer.writeCellValue(3, 5, "aaa");
writer.close();
}
@Test
@Ignore
public void closeTest() {
final Map<String, ?> map1 = MapUtil.of("id", "123456");
final Map<String, ?> map2 = MapUtil.of("id", "123457");
final List<?> data = Arrays.asList(map1, map2);
final String destFilePath = "d:/test/closeTest.xlsx";//略
FileUtil.del(destFilePath);
try (ExcelWriter writer = ExcelUtil.getBigWriter(destFilePath)) {
writer.write(data).flush();
}
}
@Test
@Ignore
public void issue1210() {
// 通过工具类创建writer
String path = "d:/test/issue1210.xlsx";
FileUtil.del(path);
BigExcelWriter writer = ExcelUtil.getBigWriter(path);
writer.addHeaderAlias("id", "SN");
writer.addHeaderAlias("userName", "User Name");
List<Map<String, Object>> list = new ArrayList<>();
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 1);
put("userName", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}});
list.add(new HashMap<String, Object>() {
private static final long serialVersionUID = 1L;
{
put("id", 2);
put("userName", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}});
writer.write(list, true);
writer.autoSizeColumnAll();
writer.close();
}
}

View File

@@ -1,19 +1,19 @@
package cn.hutool.poi.excel;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.lang.Console;
public class CellUtilTest {
@Test
@Ignore
public void isDateTest() {
String[] all = BuiltinFormats.getAll();
for(int i = 0 ; i < all.length; i++) {
Console.log("{} {}", i, all[i]);
}
}
}
package cn.hutool.poi.excel;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.lang.Console;
public class CellUtilTest {
@Test
@Ignore
public void isDateTest() {
String[] all = BuiltinFormats.getAll();
for(int i = 0 ; i < all.length; i++) {
Console.log("{} {}", i, all[i]);
}
}
}

View File

@@ -1,220 +1,220 @@
package cn.hutool.poi.excel;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* Excel读取单元测试
*
* @author Looly
*/
public class ExcelReadTest {
@Test
public void aliasTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("alias.xlsx"));
//读取单个单元格内容测试
Object value = reader.readCellValue(1, 2);
Assert.assertEquals("仓库", value);
Map<String, String> headerAlias = MapUtil.newHashMap();
headerAlias.put("用户姓名", "userName");
headerAlias.put("库房", "storageName");
headerAlias.put("盘点权限", "checkPerm");
headerAlias.put("领料审批权限", "allotAuditPerm");
reader.setHeaderAlias(headerAlias);
// 读取list时默认首个非空行为标题
List<List<Object>> read = reader.read();
Assert.assertEquals("userName", read.get(0).get(0));
Assert.assertEquals("storageName", read.get(0).get(1));
Assert.assertEquals("checkPerm", read.get(0).get(2));
Assert.assertEquals("allotAuditPerm", read.get(0).get(3));
List<Map<String, Object>> readAll = reader.readAll();
for (Map<String, Object> map : readAll) {
Assert.assertTrue(map.containsKey("userName"));
Assert.assertTrue(map.containsKey("storageName"));
Assert.assertTrue(map.containsKey("checkPerm"));
Assert.assertTrue(map.containsKey("allotAuditPerm"));
}
}
@Test
public void excelReadTestOfEmptyLine() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("priceIndex.xls"));
List<Map<String, Object>> readAll = reader.readAll();
Assert.assertEquals(4, readAll.size());
}
@Test
public void excelReadTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
List<List<Object>> readAll = reader.read();
// 标题
Assert.assertEquals("姓名", readAll.get(0).get(0));
Assert.assertEquals("性别", readAll.get(0).get(1));
Assert.assertEquals("年龄", readAll.get(0).get(2));
Assert.assertEquals("鞋码", readAll.get(0).get(3));
// 第一行
Assert.assertEquals("张三", readAll.get(1).get(0));
Assert.assertEquals("", readAll.get(1).get(1));
Assert.assertEquals(11L, readAll.get(1).get(2));
Assert.assertEquals(41.5D, readAll.get(1).get(3));
}
@Test
public void excelReadAsTextTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
Assert.assertNotNull(reader.readAsText(false));
}
@Test
public void excel03ReadTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xls"));
List<List<Object>> readAll = reader.read();
// for (List<Object> list : readAll) {
// Console.log(list);
// }
// 标题
Assert.assertEquals("姓名", readAll.get(0).get(0));
Assert.assertEquals("性别", readAll.get(0).get(1));
Assert.assertEquals("年龄", readAll.get(0).get(2));
Assert.assertEquals("分数", readAll.get(0).get(3));
// 第一行
Assert.assertEquals("张三", readAll.get(1).get(0));
Assert.assertEquals("", readAll.get(1).get(1));
Assert.assertEquals(11L, readAll.get(1).get(2));
Assert.assertEquals(33.2D, readAll.get(1).get(3));
}
@Test
public void excel03ReadTest2() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xls"), "校园入学");
List<List<Object>> readAll = reader.read();
// 标题
Assert.assertEquals("班级", readAll.get(0).get(0));
Assert.assertEquals("年级", readAll.get(0).get(1));
Assert.assertEquals("学校", readAll.get(0).get(2));
Assert.assertEquals("入学时间", readAll.get(0).get(3));
Assert.assertEquals("更新时间", readAll.get(0).get(4));
}
@Test
public void excelReadToMapListTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
List<Map<String, Object>> readAll = reader.readAll();
Assert.assertEquals("张三", readAll.get(0).get("姓名"));
Assert.assertEquals("", readAll.get(0).get("性别"));
Assert.assertEquals(11L, readAll.get(0).get("年龄"));
}
@Test
public void excelReadToBeanListTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
reader.addHeaderAlias("姓名", "name");
reader.addHeaderAlias("年龄", "age");
reader.addHeaderAlias("性别", "gender");
reader.addHeaderAlias("鞋码", "shoeSize");
List<Person> all = reader.readAll(Person.class);
Assert.assertEquals("张三", all.get(0).getName());
Assert.assertEquals("", all.get(0).getGender());
Assert.assertEquals(Integer.valueOf(11), all.get(0).getAge());
Assert.assertEquals(new BigDecimal("41.5"), all.get(0).getShoeSize());
}
@Test
@Ignore
public void excelReadToBeanListTest2() {
ExcelReader reader = ExcelUtil.getReader("f:/test/toBean.xlsx");
reader.addHeaderAlias("姓名", "name");
reader.addHeaderAlias("年龄", "age");
reader.addHeaderAlias("性别", "gender");
List<Person> all = reader.read(0, 2, Person.class);
for (Person person : all) {
Console.log(person);
}
}
@Data
public static class Person {
private String name;
private String gender;
private Integer age;
private BigDecimal shoeSize;
}
@Test
@Ignore
public void readDoubleTest() {
ExcelReader reader = ExcelUtil.getReader("f:/test/doubleTest.xls");
final List<List<Object>> read = reader.read();
for (List<Object> list : read) {
Console.log(list.get(8));
}
}
@Test
public void mergeReadTest() {
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
final List<List<Object>> read = reader.read();
// 验证合并单元格在两行中都可以取到值
Assert.assertEquals(11L, read.get(1).get(2));
Assert.assertEquals(11L, read.get(2).get(2));
}
@Test
@Ignore
public void readCellsTest() {
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
reader.read((cell, value)-> Console.log("{}, {} {}", cell.getRowIndex(), cell.getColumnIndex(), value));
}
@Test
@Ignore
public void readTest() {
// 测试合并单元格是否可以正常读到第一个单元格的值
final ExcelReader reader = ExcelUtil.getReader("d:/test/人员体检信息表.xlsx");
final List<List<Object>> read = reader.read();
for (List<Object> list : read) {
Console.log(list);
}
}
@Test
public void nullValueEditTest(){
final ExcelReader reader = ExcelUtil.getReader("null_cell_test.xlsx");
reader.setCellEditor((cell, value)-> ObjectUtil.defaultIfNull(value, "#"));
final List<List<Object>> read = reader.read();
// 对于任意一个单元格有值的情况下之前的单元格值按照null处理
Assert.assertEquals(1, read.get(1).size());
Assert.assertEquals(2, read.get(2).size());
Assert.assertEquals(3, read.get(3).size());
Assert.assertEquals("#", read.get(2).get(0));
Assert.assertEquals("#", read.get(3).get(0));
Assert.assertEquals("#", read.get(3).get(1));
}
}
package cn.hutool.poi.excel;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* Excel读取单元测试
*
* @author Looly
*/
public class ExcelReadTest {
@Test
public void aliasTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("alias.xlsx"));
//读取单个单元格内容测试
Object value = reader.readCellValue(1, 2);
Assert.assertEquals("仓库", value);
Map<String, String> headerAlias = MapUtil.newHashMap();
headerAlias.put("用户姓名", "userName");
headerAlias.put("库房", "storageName");
headerAlias.put("盘点权限", "checkPerm");
headerAlias.put("领料审批权限", "allotAuditPerm");
reader.setHeaderAlias(headerAlias);
// 读取list时默认首个非空行为标题
List<List<Object>> read = reader.read();
Assert.assertEquals("userName", read.get(0).get(0));
Assert.assertEquals("storageName", read.get(0).get(1));
Assert.assertEquals("checkPerm", read.get(0).get(2));
Assert.assertEquals("allotAuditPerm", read.get(0).get(3));
List<Map<String, Object>> readAll = reader.readAll();
for (Map<String, Object> map : readAll) {
Assert.assertTrue(map.containsKey("userName"));
Assert.assertTrue(map.containsKey("storageName"));
Assert.assertTrue(map.containsKey("checkPerm"));
Assert.assertTrue(map.containsKey("allotAuditPerm"));
}
}
@Test
public void excelReadTestOfEmptyLine() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("priceIndex.xls"));
List<Map<String, Object>> readAll = reader.readAll();
Assert.assertEquals(4, readAll.size());
}
@Test
public void excelReadTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
List<List<Object>> readAll = reader.read();
// 标题
Assert.assertEquals("姓名", readAll.get(0).get(0));
Assert.assertEquals("性别", readAll.get(0).get(1));
Assert.assertEquals("年龄", readAll.get(0).get(2));
Assert.assertEquals("鞋码", readAll.get(0).get(3));
// 第一行
Assert.assertEquals("张三", readAll.get(1).get(0));
Assert.assertEquals("", readAll.get(1).get(1));
Assert.assertEquals(11L, readAll.get(1).get(2));
Assert.assertEquals(41.5D, readAll.get(1).get(3));
}
@Test
public void excelReadAsTextTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
Assert.assertNotNull(reader.readAsText(false));
}
@Test
public void excel03ReadTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xls"));
List<List<Object>> readAll = reader.read();
// for (List<Object> list : readAll) {
// Console.log(list);
// }
// 标题
Assert.assertEquals("姓名", readAll.get(0).get(0));
Assert.assertEquals("性别", readAll.get(0).get(1));
Assert.assertEquals("年龄", readAll.get(0).get(2));
Assert.assertEquals("分数", readAll.get(0).get(3));
// 第一行
Assert.assertEquals("张三", readAll.get(1).get(0));
Assert.assertEquals("", readAll.get(1).get(1));
Assert.assertEquals(11L, readAll.get(1).get(2));
Assert.assertEquals(33.2D, readAll.get(1).get(3));
}
@Test
public void excel03ReadTest2() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xls"), "校园入学");
List<List<Object>> readAll = reader.read();
// 标题
Assert.assertEquals("班级", readAll.get(0).get(0));
Assert.assertEquals("年级", readAll.get(0).get(1));
Assert.assertEquals("学校", readAll.get(0).get(2));
Assert.assertEquals("入学时间", readAll.get(0).get(3));
Assert.assertEquals("更新时间", readAll.get(0).get(4));
}
@Test
public void excelReadToMapListTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
List<Map<String, Object>> readAll = reader.readAll();
Assert.assertEquals("张三", readAll.get(0).get("姓名"));
Assert.assertEquals("", readAll.get(0).get("性别"));
Assert.assertEquals(11L, readAll.get(0).get("年龄"));
}
@Test
public void excelReadToBeanListTest() {
ExcelReader reader = ExcelUtil.getReader(ResourceUtil.getStream("aaa.xlsx"));
reader.addHeaderAlias("姓名", "name");
reader.addHeaderAlias("年龄", "age");
reader.addHeaderAlias("性别", "gender");
reader.addHeaderAlias("鞋码", "shoeSize");
List<Person> all = reader.readAll(Person.class);
Assert.assertEquals("张三", all.get(0).getName());
Assert.assertEquals("", all.get(0).getGender());
Assert.assertEquals(Integer.valueOf(11), all.get(0).getAge());
Assert.assertEquals(new BigDecimal("41.5"), all.get(0).getShoeSize());
}
@Test
@Ignore
public void excelReadToBeanListTest2() {
ExcelReader reader = ExcelUtil.getReader("f:/test/toBean.xlsx");
reader.addHeaderAlias("姓名", "name");
reader.addHeaderAlias("年龄", "age");
reader.addHeaderAlias("性别", "gender");
List<Person> all = reader.read(0, 2, Person.class);
for (Person person : all) {
Console.log(person);
}
}
@Data
public static class Person {
private String name;
private String gender;
private Integer age;
private BigDecimal shoeSize;
}
@Test
@Ignore
public void readDoubleTest() {
ExcelReader reader = ExcelUtil.getReader("f:/test/doubleTest.xls");
final List<List<Object>> read = reader.read();
for (List<Object> list : read) {
Console.log(list.get(8));
}
}
@Test
public void mergeReadTest() {
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
final List<List<Object>> read = reader.read();
// 验证合并单元格在两行中都可以取到值
Assert.assertEquals(11L, read.get(1).get(2));
Assert.assertEquals(11L, read.get(2).get(2));
}
@Test
@Ignore
public void readCellsTest() {
final ExcelReader reader = ExcelUtil.getReader("merge_test.xlsx");
reader.read((cell, value)-> Console.log("{}, {} {}", cell.getRowIndex(), cell.getColumnIndex(), value));
}
@Test
@Ignore
public void readTest() {
// 测试合并单元格是否可以正常读到第一个单元格的值
final ExcelReader reader = ExcelUtil.getReader("d:/test/人员体检信息表.xlsx");
final List<List<Object>> read = reader.read();
for (List<Object> list : read) {
Console.log(list);
}
}
@Test
public void nullValueEditTest(){
final ExcelReader reader = ExcelUtil.getReader("null_cell_test.xlsx");
reader.setCellEditor((cell, value)-> ObjectUtil.defaultIfNull(value, "#"));
final List<List<Object>> read = reader.read();
// 对于任意一个单元格有值的情况下之前的单元格值按照null处理
Assert.assertEquals(1, read.get(1).size());
Assert.assertEquals(2, read.get(2).size());
Assert.assertEquals(3, read.get(3).size());
Assert.assertEquals("#", read.get(2).get(0));
Assert.assertEquals("#", read.get(3).get(0));
Assert.assertEquals("#", read.get(3).get(1));
}
}

View File

@@ -1,201 +1,201 @@
package cn.hutool.poi.excel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.FormulaCellValue;
import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Excel sax方式读取
*
* @author looly
*/
public class ExcelSaxReadTest {
@Test
public void excel07Test() {
// 工具化快速读取
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
}
@Test
public void excel07FromStreamTest() {
// issue#1225 非markSupport的流读取会错误
ExcelUtil.readBySax(IoUtil.toStream(FileUtil.file("aaa.xlsx")), 0, createRowHandler());
}
@Test
public void excel03Test() {
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", 1);
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
ExcelUtil.readBySax("aaa.xls", 1, createRowHandler());
}
@Test
@Ignore
public void readBlankLineTest() {
ExcelUtil.readBySax("e:/ExcelBlankLine.xlsx", 0, (sheetIndex, rowIndex, rowList) -> {
if (StrUtil.isAllEmpty(Convert.toStrArray(rowList))) {
return;
}
Console.log(rowList);
});
}
@Test
public void readBySaxTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", "0", createRowHandler());
}
@Test
public void readBySaxByRidTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", 0, createRowHandler());
}
@Test
public void readBySaxByNameTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", "Sheet1", createRowHandler());
}
@Test
@Ignore
public void readBySaxTest2() {
ExcelUtil.readBySax("d:/test/456789.xlsx", "0", (sheetIndex, rowIndex, rowList) -> Console.log(rowList));
}
private RowHandler createRowHandler() {
return (sheetIndex, rowIndex, rowlist) -> {
// Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
if (5 != rowIndex && 6 != rowIndex) {
// 测试样例中除第五行、第六行都为非空行
Assert.assertTrue(CollUtil.isNotEmpty(rowlist));
}
};
}
@Test
@Ignore
public void handle07CellTest() {
ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() {
@Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value);
}
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
}
);
}
@Test
@Ignore
public void handle03CellTest() {
ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() {
@Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value);
}
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
}
);
}
@Test
public void formulaRead03Test() {
Console.log(FileUtil.file("data_for_sax_test.xls"));
List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
if(list.size() > 1){
rows.add(list.get(1));
} else{
rows.add("");
}
});
Assert.assertEquals(50L, rows.get(3));
}
@Test
public void formulaRead07Test() {
List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
rows.add(list.get(1)));
final FormulaCellValue value = (FormulaCellValue) rows.get(3);
Assert.assertEquals(50L, value.getResult());
}
@Test
public void dateReadXlsTest() {
List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
);
Assert.assertEquals("2020-10-09 00:00:00", rows.get(1));
// 非日期格式不做转换
Assert.assertEquals("112233", rows.get(2));
Assert.assertEquals("1000.0", rows.get(3));
Assert.assertEquals("2012-12-21 00:00:00", rows.get(4));
}
@Test
public void dateReadXlsxTest() {
List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
);
Assert.assertEquals("2020-10-09 00:00:00", rows.get(1));
// 非日期格式不做转换
Assert.assertEquals("112233", rows.get(2));
Assert.assertEquals("1000.0", rows.get(3));
Assert.assertEquals("2012-12-21 00:00:00", rows.get(4));
}
@Test
@Ignore
public void dateReadXlsxTest2() {
ExcelUtil.readBySax("d:/test/custom_date_format2.xlsx", 0,
(i, i1, list) -> Console.log(list)
);
}
@Test
@Ignore
public void readBlankTest() {
File file = new File("D:/test/b.xlsx");
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
ExcelUtil.getReader(file).read().forEach(Console::log);
}
@Test
@Ignore
public void readXlsmTest(){
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1,
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
}
}
package cn.hutool.poi.excel;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.cell.FormulaCellValue;
import cn.hutool.poi.excel.sax.Excel03SaxReader;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.apache.poi.ss.usermodel.CellStyle;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Excel sax方式读取
*
* @author looly
*/
public class ExcelSaxReadTest {
@Test
public void excel07Test() {
// 工具化快速读取
ExcelUtil.readBySax("aaa.xlsx", 0, createRowHandler());
}
@Test
public void excel07FromStreamTest() {
// issue#1225 非markSupport的流读取会错误
ExcelUtil.readBySax(IoUtil.toStream(FileUtil.file("aaa.xlsx")), 0, createRowHandler());
}
@Test
public void excel03Test() {
Excel03SaxReader reader = new Excel03SaxReader(createRowHandler());
reader.read("aaa.xls", 1);
// Console.log("Sheet index: [{}], Sheet name: [{}]", reader.getSheetIndex(), reader.getSheetName());
ExcelUtil.readBySax("aaa.xls", 1, createRowHandler());
}
@Test
@Ignore
public void readBlankLineTest() {
ExcelUtil.readBySax("e:/ExcelBlankLine.xlsx", 0, (sheetIndex, rowIndex, rowList) -> {
if (StrUtil.isAllEmpty(Convert.toStrArray(rowList))) {
return;
}
Console.log(rowList);
});
}
@Test
public void readBySaxTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", "0", createRowHandler());
}
@Test
public void readBySaxByRidTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", 0, createRowHandler());
}
@Test
public void readBySaxByNameTest() {
ExcelUtil.readBySax("blankAndDateTest.xlsx", "Sheet1", createRowHandler());
}
@Test
@Ignore
public void readBySaxTest2() {
ExcelUtil.readBySax("d:/test/456789.xlsx", "0", (sheetIndex, rowIndex, rowList) -> Console.log(rowList));
}
private RowHandler createRowHandler() {
return (sheetIndex, rowIndex, rowlist) -> {
// Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
if (5 != rowIndex && 6 != rowIndex) {
// 测试样例中除第五行、第六行都为非空行
Assert.assertTrue(CollUtil.isNotEmpty(rowlist));
}
};
}
@Test
@Ignore
public void handle07CellTest() {
ExcelUtil.readBySax("d:/test/test.xlsx", -1, new RowHandler() {
@Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value);
}
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
}
);
}
@Test
@Ignore
public void handle03CellTest() {
ExcelUtil.readBySax("d:/test/test.xls", -1, new RowHandler() {
@Override
public void handleCell(int sheetIndex, long rowIndex, int cellIndex, Object value, CellStyle xssfCellStyle) {
Console.log("{} {} {}", rowIndex, cellIndex, value);
}
@Override
public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
}
}
);
}
@Test
public void formulaRead03Test() {
Console.log(FileUtil.file("data_for_sax_test.xls"));
List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", -1, (i, i1, list) -> {
if(list.size() > 1){
rows.add(list.get(1));
} else{
rows.add("");
}
});
Assert.assertEquals(50L, rows.get(3));
}
@Test
public void formulaRead07Test() {
List<Object> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0, (i, i1, list) ->
rows.add(list.get(1)));
final FormulaCellValue value = (FormulaCellValue) rows.get(3);
Assert.assertEquals(50L, value.getResult());
}
@Test
public void dateReadXlsTest() {
List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xls", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
);
Assert.assertEquals("2020-10-09 00:00:00", rows.get(1));
// 非日期格式不做转换
Assert.assertEquals("112233", rows.get(2));
Assert.assertEquals("1000.0", rows.get(3));
Assert.assertEquals("2012-12-21 00:00:00", rows.get(4));
}
@Test
public void dateReadXlsxTest() {
List<String> rows = new ArrayList<>();
ExcelUtil.readBySax("data_for_sax_test.xlsx", 0,
(i, i1, list) -> rows.add(StrUtil.toString(list.get(0)))
);
Assert.assertEquals("2020-10-09 00:00:00", rows.get(1));
// 非日期格式不做转换
Assert.assertEquals("112233", rows.get(2));
Assert.assertEquals("1000.0", rows.get(3));
Assert.assertEquals("2012-12-21 00:00:00", rows.get(4));
}
@Test
@Ignore
public void dateReadXlsxTest2() {
ExcelUtil.readBySax("d:/test/custom_date_format2.xlsx", 0,
(i, i1, list) -> Console.log(list)
);
}
@Test
@Ignore
public void readBlankTest() {
File file = new File("D:/test/b.xlsx");
ExcelUtil.readBySax(file, 0, (sheetIndex, rowIndex, rowList) -> rowList.forEach(Console::log));
ExcelUtil.getReader(file).read().forEach(Console::log);
}
@Test
@Ignore
public void readXlsmTest(){
ExcelUtil.readBySax("d:/test/WhiteListTemplate.xlsm", -1,
(sheetIndex, rowIndex, rowlist) -> Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist));
}
}

View File

@@ -1,45 +1,45 @@
package cn.hutool.poi.excel;
import cn.hutool.poi.excel.cell.CellLocation;
import org.junit.Assert;
import org.junit.Test;
public class ExcelUtilTest {
@Test
public void indexToColNameTest() {
Assert.assertEquals("A", ExcelUtil.indexToColName(0));
Assert.assertEquals("B", ExcelUtil.indexToColName(1));
Assert.assertEquals("C", ExcelUtil.indexToColName(2));
Assert.assertEquals("AA", ExcelUtil.indexToColName(26));
Assert.assertEquals("AB", ExcelUtil.indexToColName(27));
Assert.assertEquals("AC", ExcelUtil.indexToColName(28));
Assert.assertEquals("AAA", ExcelUtil.indexToColName(702));
Assert.assertEquals("AAB", ExcelUtil.indexToColName(703));
Assert.assertEquals("AAC", ExcelUtil.indexToColName(704));
}
@Test
public void colNameToIndexTest() {
Assert.assertEquals(704, ExcelUtil.colNameToIndex("AAC"));
Assert.assertEquals(703, ExcelUtil.colNameToIndex("AAB"));
Assert.assertEquals(702, ExcelUtil.colNameToIndex("AAA"));
Assert.assertEquals(28, ExcelUtil.colNameToIndex("AC"));
Assert.assertEquals(27, ExcelUtil.colNameToIndex("AB"));
Assert.assertEquals(26, ExcelUtil.colNameToIndex("AA"));
Assert.assertEquals(2, ExcelUtil.colNameToIndex("C"));
Assert.assertEquals(1, ExcelUtil.colNameToIndex("B"));
Assert.assertEquals(0, ExcelUtil.colNameToIndex("A"));
}
@Test
public void toLocationTest(){
final CellLocation a11 = ExcelUtil.toLocation("A11");
Assert.assertEquals(0, a11.getX());
Assert.assertEquals(10, a11.getY());
}
}
package cn.hutool.poi.excel;
import cn.hutool.poi.excel.cell.CellLocation;
import org.junit.Assert;
import org.junit.Test;
public class ExcelUtilTest {
@Test
public void indexToColNameTest() {
Assert.assertEquals("A", ExcelUtil.indexToColName(0));
Assert.assertEquals("B", ExcelUtil.indexToColName(1));
Assert.assertEquals("C", ExcelUtil.indexToColName(2));
Assert.assertEquals("AA", ExcelUtil.indexToColName(26));
Assert.assertEquals("AB", ExcelUtil.indexToColName(27));
Assert.assertEquals("AC", ExcelUtil.indexToColName(28));
Assert.assertEquals("AAA", ExcelUtil.indexToColName(702));
Assert.assertEquals("AAB", ExcelUtil.indexToColName(703));
Assert.assertEquals("AAC", ExcelUtil.indexToColName(704));
}
@Test
public void colNameToIndexTest() {
Assert.assertEquals(704, ExcelUtil.colNameToIndex("AAC"));
Assert.assertEquals(703, ExcelUtil.colNameToIndex("AAB"));
Assert.assertEquals(702, ExcelUtil.colNameToIndex("AAA"));
Assert.assertEquals(28, ExcelUtil.colNameToIndex("AC"));
Assert.assertEquals(27, ExcelUtil.colNameToIndex("AB"));
Assert.assertEquals(26, ExcelUtil.colNameToIndex("AA"));
Assert.assertEquals(2, ExcelUtil.colNameToIndex("C"));
Assert.assertEquals(1, ExcelUtil.colNameToIndex("B"));
Assert.assertEquals(0, ExcelUtil.colNameToIndex("A"));
}
@Test
public void toLocationTest(){
final CellLocation a11 = ExcelUtil.toLocation("A11");
Assert.assertEquals(0, a11.getX());
Assert.assertEquals(10, a11.getY());
}
}

View File

@@ -1,10 +1,10 @@
package cn.hutool.poi.excel;
import lombok.Data;
@Data
public class OrderExcel {
private String id;
private String num;
private String body;
}
package cn.hutool.poi.excel;
import lombok.Data;
@Data
public class OrderExcel {
private String id;
private String num;
private String body;
}

View File

@@ -1,14 +1,14 @@
package cn.hutool.poi.excel;
import lombok.Data;
import java.util.Date;
@Data
public class TestBean {
private String name;
private int age;
private double score;
private boolean isPass;
private Date examDate;
}
package cn.hutool.poi.excel;
import lombok.Data;
import java.util.Date;
@Data
public class TestBean {
private String name;
private int age;
private double score;
private boolean isPass;
private Date examDate;
}

View File

@@ -1,16 +1,16 @@
package cn.hutool.poi.ofd;
import cn.hutool.core.io.FileUtil;
import org.junit.Ignore;
import org.junit.Test;
public class OfdWriterTest {
@Test
@Ignore
public void writeTest(){
final OfdWriter ofdWriter = new OfdWriter(FileUtil.file("d:/test/test.ofd"));
ofdWriter.addText(null, "测试文本");
ofdWriter.close();
}
}
package cn.hutool.poi.ofd;
import cn.hutool.core.io.FileUtil;
import org.junit.Ignore;
import org.junit.Test;
public class OfdWriterTest {
@Test
@Ignore
public void writeTest(){
final OfdWriter ofdWriter = new OfdWriter(FileUtil.file("d:/test/test.ofd"));
ofdWriter.addText(null, "测试文本");
ofdWriter.close();
}
}

View File

@@ -1,85 +1,85 @@
package cn.hutool.poi.word;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import org.junit.Ignore;
import org.junit.Test;
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
public class WordWriterTest {
@Test
@Ignore
public void writeTest() {
Word07Writer writer = new Word07Writer();
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
writer.flush(FileUtil.file("e:/wordWrite.docx"));
writer.close();
Console.log("OK");
}
@Test
@Ignore
public void writePicTest() {
Word07Writer writer = new Word07Writer();
writer.addPicture(new File("d:\\test\\qrcodeCustom.jpg"), 100, 200);
// 写出到文件
writer.flush(FileUtil.file("d:/test/writePic.docx"));
// 关闭
writer.close();
}
@Test
@Ignore
public void writeTableTest(){
final Word07Writer writer = new Word07Writer();
Map<String, Object> map = new LinkedHashMap<>();
map.put("姓名", "张三");
map.put("年龄", "23");
map.put("成绩", 88.32);
map.put("是否合格", true);
writer.addTable(CollUtil.newArrayList(map));
writer.flush(FileUtil.file("d:/test/test.docx"));
}
@Test
@Ignore
public void writeMapAsTableTest() {
Word07Writer writer = new Word07Writer();
Map<String, Object> data = new LinkedHashMap<>();
data.put("姓名", "张三");
data.put("年龄", 23);
data.put("成绩", 80.5);
data.put("是否合格", true);
data.put("考试日期", DateUtil.date());
Map<String, Object> data2 = new LinkedHashMap<>();
data2.put("姓名", "李四");
data2.put("年龄", 4);
data2.put("成绩", 59);
data2.put("是否合格", false);
data2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> mapArrayList = CollUtil.newArrayList(data, data2);
// 添加段落(标题)
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分");
// 添加段落(正文)
writer.addText(new Font("宋体", Font.PLAIN, 13), "我是正文第一部分");
writer.addTable(mapArrayList);
// 写出到文件
writer.flush(FileUtil.file("d:/test/a.docx"));
// 关闭
writer.close();
}
}
package cn.hutool.poi.word;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import org.junit.Ignore;
import org.junit.Test;
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
public class WordWriterTest {
@Test
@Ignore
public void writeTest() {
Word07Writer writer = new Word07Writer();
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
writer.flush(FileUtil.file("e:/wordWrite.docx"));
writer.close();
Console.log("OK");
}
@Test
@Ignore
public void writePicTest() {
Word07Writer writer = new Word07Writer();
writer.addPicture(new File("d:\\test\\qrcodeCustom.jpg"), 100, 200);
// 写出到文件
writer.flush(FileUtil.file("d:/test/writePic.docx"));
// 关闭
writer.close();
}
@Test
@Ignore
public void writeTableTest(){
final Word07Writer writer = new Word07Writer();
Map<String, Object> map = new LinkedHashMap<>();
map.put("姓名", "张三");
map.put("年龄", "23");
map.put("成绩", 88.32);
map.put("是否合格", true);
writer.addTable(CollUtil.newArrayList(map));
writer.flush(FileUtil.file("d:/test/test.docx"));
}
@Test
@Ignore
public void writeMapAsTableTest() {
Word07Writer writer = new Word07Writer();
Map<String, Object> data = new LinkedHashMap<>();
data.put("姓名", "张三");
data.put("年龄", 23);
data.put("成绩", 80.5);
data.put("是否合格", true);
data.put("考试日期", DateUtil.date());
Map<String, Object> data2 = new LinkedHashMap<>();
data2.put("姓名", "李四");
data2.put("年龄", 4);
data2.put("成绩", 59);
data2.put("是否合格", false);
data2.put("考试日期", DateUtil.date());
ArrayList<Map<String, Object>> mapArrayList = CollUtil.newArrayList(data, data2);
// 添加段落(标题)
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分");
// 添加段落(正文)
writer.addText(new Font("宋体", Font.PLAIN, 13), "我是正文第一部分");
writer.addTable(mapArrayList);
// 写出到文件
writer.flush(FileUtil.file("d:/test/a.docx"));
// 关闭
writer.close();
}
}