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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>com.lxyer</groupId> <groupId>net.tccn</groupId>
<artifactId>excel</artifactId> <artifactId>excel</artifactId>
<version>0.1.1</version> <version>0.1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
@ -40,8 +40,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.8</source> <source>14</source>
<target>1.8</target> <target>14</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -51,7 +51,7 @@ public class ExcelKit {
* @date 2015-6-16 下午5:56:56 * @date 2015-6-16 下午5:56:56
*/ */
//map:data,sheetName,hds,hdNames, //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(); //创建工作薄 Workbook wb = new SXSSFWorkbook(); //创建工作薄
for (int i = 0; i < list.size(); i++) { 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 net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test; 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 java.util.*;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
@ -15,7 +18,7 @@ import static java.util.Arrays.asList;
public class ExcelKitTest { public class ExcelKitTest {
@Test @Test
public void mapExport(){ public void mapExport() {
// 数据List<T> T可以是map也可以是某个Javabean // 数据List<T> T可以是map也可以是某个Javabean
Map m1 = new HashMap(); Map m1 = new HashMap();
m1.put("name", "张三"); m1.put("name", "张三");
@ -57,14 +60,14 @@ public class ExcelKitTest {
System.out.println(sql1.substring(0,sql1.indexOf("from")).replace("select", "").replace(" ", "")); 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"); File file = new File("target/abx.xls");
List<Map> list = ExcelKit.readExcel(file, fields); List<Map> list = ExcelKit.readExcel(file, fields);
System.out.println("list.size: " + list.size()); System.out.println("list.size: " + list.size());
list.forEach(x->{ list.forEach(x -> {
x.forEach((k,v)->{ x.forEach((k, v) -> {
System.out.print(String.format("%s:%s \t", k, v)); System.out.print(String.format("%s:%s \t", k, v));
}); });
System.out.println(); System.out.println();
@ -77,8 +80,8 @@ public class ExcelKitTest {
// 在不传 String[] fields 会把值从左到右分别赋值给 a...z,这样的key上 // 在不传 String[] fields 会把值从左到右分别赋值给 a...z,这样的key上
List<Map> list = ExcelKit.readExcel(new File("target/abx.xls")); List<Map> list = ExcelKit.readExcel(new File("target/abx.xls"));
list.forEach(x->{ list.forEach(x -> {
x.forEach((k,v)->{ x.forEach((k, v) -> {
System.out.print(String.format("%s:%s \t", k, v)); System.out.print(String.format("%s:%s \t", k, v));
}); });
System.out.println(); System.out.println();
@ -88,6 +91,7 @@ public class ExcelKitTest {
//--------------------------- 通用导入案例 -------------------------------- //--------------------------- 通用导入案例 --------------------------------
static Properties properties = new Properties(); static Properties properties = new Properties();
static { static {
try { try {
// 读取导入配置文件 // 读取导入配置文件
@ -99,6 +103,7 @@ public class ExcelKitTest {
/** /**
* 构建入库语句 * 构建入库语句
*
* @param list 数据list<Map> * @param list 数据list<Map>
* @param heads 数据库需要入库的字段 * @param heads 数据库需要入库的字段
* @param table 实体表 * @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.ExcelExportKit;
import net.tccn.kit.poi.ExcelKit;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test; 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.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@ -13,8 +14,6 @@ import java.util.function.Function;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import static java.util.Arrays.asList;
/** /**
* 以下均为非poi导出使用示例 * 以下均为非poi导出使用示例
* 1导出List<javabean>示例 * 1导出List<javabean>示例
@ -22,14 +21,14 @@ import static java.util.Arrays.asList;
* 3javax.servlet web项目中开发使用示例 * 3javax.servlet web项目中开发使用示例
* 4导出压缩的excel示例 * 4导出压缩的excel示例
* 5redkale web项目导出示例 * 5redkale web项目导出示例
* * <p>
* Created by liangxianyou at 2018/7/6 10:29. * Created by liangxianyou at 2018/7/6 10:29.
*/ */
public class ExportKitTest { public class ExportKitTest {
//1导出List<javabean>示例 //1导出List<javabean>示例
//@Test //@Test
public void exportBean(){ public void exportBean() {
Map<String, String> heads = new LinkedHashMap(); Map<String, String> heads = new LinkedHashMap();
heads.put("姓名", "name"); heads.put("姓名", "name");
heads.put("年龄", "age"); heads.put("年龄", "age");
@ -56,7 +55,7 @@ public class ExportKitTest {
//2导出List<Map> 示例 //2导出List<Map> 示例
//@Test //@Test
public void exportMapToZip(){ public void exportMapToZip() {
Map<String, String> heads = new LinkedHashMap(); Map<String, String> heads = new LinkedHashMap();
heads.put("姓名", "name"); heads.put("姓名", "name");
@ -128,6 +127,7 @@ public class ExportKitTest {
/** /**
* 4导出压缩的excel示例 * 4导出压缩的excel示例
*
* @param head * @param head
* @param rows * @param rows
* @param file * @param file
@ -135,12 +135,12 @@ public class ExportKitTest {
* @param <T> * @param <T>
* @throws IOException * @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)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(zos); 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); zos.putNextEntry(zipEntry);
StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun); StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun);
@ -153,6 +153,7 @@ public class ExportKitTest {
/** /**
* 5redkale web项目导出示例 * 5redkale web项目导出示例
*
* @return * @return
* @throws IOException * @throws IOException
*/ */
@ -167,7 +168,6 @@ public class ExportKitTest {
response.finish(buf.toString().getBytes()); response.finish(buf.toString().getBytes());
} }
*/ */
@Test @Test
public void t() { public void t() {
Object t = new Person(); Object t = new Person();
@ -227,6 +227,7 @@ public class ExportKitTest {
/** /**
* 对象属性处理 * 对象属性处理
*
* @param o * @param o
* @param function * @param function
* @auther lxyer * @auther lxyer

View File

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