This commit is contained in:
lxyer 2020-04-27 22:44:22 +08:00
parent b083db50ac
commit a667e888f0
5 changed files with 32 additions and 26 deletions

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lxyer</groupId>
<groupId>net.tccn</groupId>
<artifactId>excel</artifactId>
<version>0.1.1</version>
<packaging>jar</packaging>
@ -40,8 +40,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>

View File

@ -51,7 +51,7 @@ public class ExcelKit {
* @date 2015-6-16 下午5:56:56
*/
//map:data,sheetName,hds,hdNames,
public static <T> Workbook exportExcels(List<Map<String, Object>> list) {
public static <T> Workbook exportExcels(List<Map> list) {
Workbook wb = new SXSSFWorkbook(); //创建工作薄
for (int i = 0; i < list.size(); i++) {

View File

@ -1,10 +1,13 @@
package com.lxyer.excel;
package net.tccn.excel.test;
import net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import static java.util.Arrays.asList;
@ -15,7 +18,7 @@ import static java.util.Arrays.asList;
public class ExcelKitTest {
@Test
public void mapExport(){
public void mapExport() {
// 数据List<T> T可以是map也可以是某个Javabean
Map m1 = new HashMap();
m1.put("name", "张三");
@ -57,14 +60,14 @@ public class ExcelKitTest {
System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", ""));
*/
String[] fields ={"name", "age"};
String[] fields = {"name", "age"};
File file = new File("target/abx.xls");
List<Map> list = ExcelKit.readExcel(file, fields);
System.out.println("list.size: " + list.size());
list.forEach(x->{
x.forEach((k,v)->{
list.forEach(x -> {
x.forEach((k, v) -> {
System.out.print(String.format("%s:%s \t", k, v));
});
System.out.println();
@ -77,8 +80,8 @@ public class ExcelKitTest {
// 在不传 String[] fields 会把值从左到右分别赋值给 a...z,这样的key上
List<Map> list = ExcelKit.readExcel(new File("target/abx.xls"));
list.forEach(x->{
x.forEach((k,v)->{
list.forEach(x -> {
x.forEach((k, v) -> {
System.out.print(String.format("%s:%s \t", k, v));
});
System.out.println();
@ -88,6 +91,7 @@ public class ExcelKitTest {
//--------------------------- 通用导入案例 --------------------------------
static Properties properties = new Properties();
static {
try {
// 读取导入配置文件
@ -99,6 +103,7 @@ public class ExcelKitTest {
/**
* 构建入库语句
*
* @param list 数据list<Map>
* @param heads 数据库需要入库的字段
* @param table 实体表

View File

@ -1,11 +1,12 @@
package com.lxyer.excel;
package net.tccn.excel.test;
import net.tccn.kit.poi.ExcelExportKit;
import net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.io.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiFunction;
@ -13,23 +14,21 @@ import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static java.util.Arrays.asList;
/**
* 以下均为非poi导出使用示例
* 以下均为非poi导出使用示例
* 1导出List<javabean>示例
* 2导出List<Map> 示例
* 3javax.servlet web项目中开发使用示例
* 4导出压缩的excel示例
* 5redkale web项目导出示例
*
* <p>
* Created by liangxianyou at 2018/7/6 10:29.
*/
public class ExportKitTest {
//1导出List<javabean>示例
//@Test
public void exportBean(){
public void exportBean() {
Map<String, String> heads = new LinkedHashMap();
heads.put("姓名", "name");
heads.put("年龄", "age");
@ -56,7 +55,7 @@ public class ExportKitTest {
//2导出List<Map> 示例
//@Test
public void exportMapToZip(){
public void exportMapToZip() {
Map<String, String> heads = new LinkedHashMap();
heads.put("姓名", "name");
@ -128,6 +127,7 @@ public class ExportKitTest {
/**
* 4导出压缩的excel示例
*
* @param head
* @param rows
* @param file
@ -135,12 +135,12 @@ public class ExportKitTest {
* @param <T>
* @throws IOException
*/
public <T,U,R> void exportExcelZip(Map<String,String> head, List<T> rows, File file, BiFunction<T, U, R> fun) throws IOException {
public <T, U, R> void exportExcelZip(Map<String, String> head, List<T> rows, File file, BiFunction<T, U, R> fun) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(zos);
ZipEntry zipEntry = new ZipEntry(file.getName().split("[.]")[0]+".xls");
ZipEntry zipEntry = new ZipEntry(file.getName().split("[.]")[0] + ".xls");
zos.putNextEntry(zipEntry);
StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun);
@ -153,6 +153,7 @@ public class ExportKitTest {
/**
* 5redkale web项目导出示例
*
* @return
* @throws IOException
*/
@ -167,7 +168,6 @@ public class ExportKitTest {
response.finish(buf.toString().getBytes());
}
*/
@Test
public void t() {
Object t = new Person();
@ -227,6 +227,7 @@ public class ExportKitTest {
/**
* 对象属性处理
*
* @param o
* @param function
* @auther lxyer

View File

@ -1,4 +1,4 @@
package com.lxyer.excel;
package net.tccn.excel.test;
import java.util.Date;