mirror of https://github.com/alibaba/easyexcel
是仪
4 years ago
11 changed files with 301 additions and 25 deletions
@ -0,0 +1,51 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.lang.reflect.Field; |
||||
|
||||
/** |
||||
* Field utils |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class FieldUtils { |
||||
|
||||
private static final int START_RESOLVE_FIELD_LENGTH = 2; |
||||
|
||||
/** |
||||
* Parsing the name matching cglib。 |
||||
* <ul> |
||||
* <ul>null -> null</ul> |
||||
* <ul>string1 -> string1</ul> |
||||
* <ul>String2 -> string2</ul> |
||||
* <ul>sTring3 -> STring3</ul> |
||||
* <ul>STring4 -> STring4</ul> |
||||
* <ul>STRING5 -> STRING5</ul> |
||||
* <ul>STRing6 -> STRing6</ul> |
||||
* </ul> |
||||
* |
||||
* @param field field |
||||
* @return field name. |
||||
*/ |
||||
public static String resolveCglibFieldName(Field field) { |
||||
if (field == null) { |
||||
return null; |
||||
} |
||||
String fieldName = field.getName(); |
||||
if (StringUtils.isBlank(fieldName) || fieldName.length() < START_RESOLVE_FIELD_LENGTH) { |
||||
return fieldName; |
||||
} |
||||
char firstChar = fieldName.charAt(0); |
||||
char secondChar = fieldName.charAt(1); |
||||
if (Character.isUpperCase(firstChar) == Character.isUpperCase(secondChar)) { |
||||
return fieldName; |
||||
} |
||||
if (Character.isUpperCase(firstChar)) { |
||||
return buildFieldName(Character.toLowerCase(firstChar), fieldName); |
||||
} |
||||
return buildFieldName(Character.toUpperCase(firstChar), fieldName); |
||||
} |
||||
|
||||
private static String buildFieldName(char firstChar, String fieldName) { |
||||
return firstChar + fieldName.substring(1); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.alibaba.easyexcel.test.core.noncamel; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class UnCamelData { |
||||
private String string1; |
||||
private String String2; |
||||
private String sTring3; |
||||
private String STring4; |
||||
private String STRING5; |
||||
private String STRing6; |
||||
} |
@ -0,0 +1,49 @@
|
||||
package com.alibaba.easyexcel.test.core.noncamel; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.junit.Assert; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Slf4j |
||||
public class UnCamelDataListener extends AnalysisEventListener<UnCamelData> { |
||||
List<UnCamelData> list = new ArrayList<>(); |
||||
|
||||
@Override |
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { |
||||
log.debug("Head is:{}", JSON.toJSONString(headMap)); |
||||
Assert.assertEquals(headMap.get(0), "string1"); |
||||
Assert.assertEquals(headMap.get(1), "String2"); |
||||
Assert.assertEquals(headMap.get(2), "sTring3"); |
||||
Assert.assertEquals(headMap.get(3), "STring4"); |
||||
Assert.assertEquals(headMap.get(4), "STRING5"); |
||||
Assert.assertEquals(headMap.get(5), "STRing6"); |
||||
} |
||||
|
||||
@Override |
||||
public void invoke(UnCamelData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 10); |
||||
UnCamelData unCamelData = list.get(0); |
||||
Assert.assertEquals(unCamelData.getString1(), "string1"); |
||||
Assert.assertEquals(unCamelData.getString2(), "string2"); |
||||
Assert.assertEquals(unCamelData.getSTring3(), "string3"); |
||||
Assert.assertEquals(unCamelData.getSTring4(), "string4"); |
||||
Assert.assertEquals(unCamelData.getSTRING5(), "string5"); |
||||
Assert.assertEquals(unCamelData.getSTRing6(), "string6"); |
||||
log.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,59 @@
|
||||
package com.alibaba.easyexcel.test.core.noncamel; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class UnCamelDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("unCame07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("unCame03.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void t01ReadAndWrite07() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void t02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcel.write(file, UnCamelData.class).sheet().doWrite(data()); |
||||
EasyExcel.read(file, UnCamelData.class, new UnCamelDataListener()).sheet().doRead(); |
||||
} |
||||
|
||||
private List<UnCamelData> data() { |
||||
List<UnCamelData> list = new ArrayList<>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
UnCamelData unCamelData = new UnCamelData(); |
||||
unCamelData.setString1("string1"); |
||||
unCamelData.setString2("string2"); |
||||
unCamelData.setSTring3("string3"); |
||||
unCamelData.setSTring4("string4"); |
||||
unCamelData.setSTRING5("string5"); |
||||
unCamelData.setSTRing6("string6"); |
||||
list.add(unCamelData); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* TODO |
||||
* |
||||
* @author 是仪 |
||||
*/ |
||||
@Data |
||||
public class CamlData { |
||||
private String string1; |
||||
private String String2; |
||||
private String sTring3; |
||||
private String STring4; |
||||
private String STRING5; |
||||
private String STRing6; |
||||
} |
Loading…
Reference in new issue