-
Notifications
You must be signed in to change notification settings - Fork 30
feilong office csv
Reduce development, Release ideas (减少开发,释放思想)
#Welcome to feilong-office-csv
csv工具类
- csv是开发中常见的功能,使用 feilong csv 可以大幅度减少开发工作量
<dependency>
<groupId>com.github.ifeilong</groupId>
<artifactId>feilong</artifactId>
<version>3.0.10</version>
</dependency>
示例代码:
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite(){
String path = "/home/feilong/date/${date}/feilongid_pix_demand.csv";
path = path.replace("${date}", DateUtil.toString(DateUtil.addDay(new Date(), -1), DatePattern.COMMON_DATE));
LOGGER.info(path);
String[] columnTitles = { "a", "b" };
List<Object[]> list = new ArrayList<Object[]>();
for (int i = 0; i < 20; i++){
list.add(ConvertUtil.toArray(i + "金,鑫", i + "jin'\"xin"));
}
csvWrite.write(path, columnTitles, list, new CsvConfig(CharsetType.GBK));
}
核心思想,在于 构造 String[] columnTitles
和 List<Object[]> list
生成 csv 内容如下:
"a","b"
"0金,鑫","0jin'""xin"
"1金,鑫","1jin'""xin"
"2金,鑫","2jin'""xin"
"3金,鑫","3jin'""xin"
"4金,鑫","4jin'""xin"
此时可以使用 excel 完美打开
黄总:
我有一个 List, User对象里面有 username ,name ,age 三个属性字段,能自动转成CSV格式吗?以属性做header,以值做记录行
feilong:
嗯, 小伙子有追求,棒棒哒,来来来,我们这么玩儿
User
public class User{
private String username;
private String name;
private int age;
public User(String username, String name, int age){
this.username = username;
this.name = name;
this.age = age;
}
//setter 略
//getter 略
}
再准备下测试数据 :
private List<User> buildTestData(){
return ConvertUtil.toList(
new User("zhangsan1", "张三1", 18),
new User("zhangsan5", "张三5", 28),
new User("zhangsan4", "张三4", 38),
new User("zhangsan6", "张三6", 48));
}
示例代码:
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite2(){
String url = "d:/writeBean.csv";
csvWrite.write(url, buildTestData());
DesktopUtil.open(url);
}
生成 csv 内容如下:
"age","name","username"
"18","张三1","zhangsan1"
"28","张三5","zhangsan5"
"38","张三4","zhangsan4"
"48","张三6","zhangsan6"
此时可以使用 excel 打开
黄总:
咦,能打开, 乱码啦~~
feilong:
莫急,往下看
默认输出的csv编码格式是utf-8
,有时候由于操作系统原因,可能使用excel打开csv文件,会显示乱码,这时候我们可以设置编码来解决
示例代码:
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite3(){
String url = "d:/writeBean2-encode.csv";
BeanCsvConfig<User> beanCsvConfig = new BeanCsvConfig<>(User.class);
beanCsvConfig.setEncode(CharsetType.GBK);
csvWrite.write(url, buildTestData(), beanCsvConfig);
DesktopUtil.open(url);
}
此时可以使用 excel 完美打开
黄总:
我想要对 生成的header 重命名以及排序呢?
feilong:
这个要求不过分~,来来来,我们这么玩儿
默认程序以propertyName
的顺序排序,如果需要定制生成的csv的标题以及顺序,可以使用 @CsvColumn
,
PS:目前该注解只支持标注在 field上面
public class User{
@CsvColumn(name = "usern ame ",order = 5)
private String username;
@CsvColumn(name = "名字",order = 16)
private String name;
@CsvColumn(name = "a ge",order = 10)
private int age;
public User(String username, String name, int age){
this.username = username;
this.name = name;
this.age = age;
}
//setter 略
//getter 略
}
示例代码(不变):
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite3(){
String url = "d:/writeBean-encode.csv";
BeanCsvConfig<User> beanCsvConfig = new BeanCsvConfig<>(User.class);
beanCsvConfig.setEncode(CharsetType.GBK);
csvWrite.write(url, buildTestData(), beanCsvConfig);
DesktopUtil.open(url);
}
生成 csv 内容如下:
"usern ame","a ge","名字"
"zhangsan1","18","张三1"
"zhangsan5","28","张三5"
"zhangsan4","38","张三4"
"zhangsan6","48","张三6"
此时可以使用 excel 完美打开
黄总:
我有个需求,输出csv的时候,不需要 header,咋整?
feilong:
没毛病,小Case
可以通过设置 beanCsvConfig isPrintHeaderLine
属性
示例代码:
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite4(){
String url = "d:/writeBean-encode-noheader.csv";
BeanCsvConfig<User> beanCsvConfig = new BeanCsvConfig<>(User.class);
beanCsvConfig.setEncode(CharsetType.GBK);
beanCsvConfig.setIsPrintHeaderLine(false);
csvWrite.write(url, buildTestData(), beanCsvConfig);
DesktopUtil.open(url);
}
生成 csv 内容如下:
"zhangsan1","18","张三1"
"zhangsan5","28","张三5"
"zhangsan4","38","张三4"
"zhangsan6","48","张三6"
此时可以使用 excel 完美打开
feilong:
我们还可以通过 设置 beanCsvConfig
excludePropertyNames
属性来设置输出CVS的时候排除某些属性
示例代码:
private final CsvWrite csvWrite = new DefaultCsvWrite();
@Test
public void testWrite5(){
String url = "d:/writeBean-encode-noheader-exclude.csv";
BeanCsvConfig<User> beanCsvConfig = new BeanCsvConfig<>(User.class);
beanCsvConfig.setEncode(CharsetType.GBK);
beanCsvConfig.setIsPrintHeaderLine(false);
beanCsvConfig.setExcludePropertyNames("name", "age");
csvWrite.write(url, buildTestData(), beanCsvConfig);
DesktopUtil.open(url);
}
生成 csv 内容如下:
"zhangsan1"
"zhangsan5"
"zhangsan4"
"zhangsan6"
此时可以使用 excel 完美打开
#特别说明
以上代码 ,所有
private final CsvWrite csvWrite = new DefaultCsvWrite();
的地方, 都可以使用spring IOC来管理
core