改善代码实现逻辑

This commit is contained in:
lxyer 2018-12-02 14:03:46 +08:00
parent 40a64e6392
commit 26b7123ebc
4 changed files with 359 additions and 392 deletions

62
pom.xml
View File

@ -5,9 +5,34 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.lxyer</groupId> <groupId>com.lxyer</groupId>
<artifactId>jdk-demo</artifactId> <artifactId>excel</artifactId>
<version>1.0-SNAPSHOT</version> <version>0.1.0</version>
<packaging>jar</packaging>
<dependencies>
<!-- poi支持 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build> <build>
<finalName>JExcel</finalName>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory> <testSourceDirectory>test</testSourceDirectory>
<resources> <resources>
@ -26,37 +51,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.redkale</groupId>
<artifactId>redkale</artifactId>
<version>1.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.37</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

View File

@ -5,135 +5,15 @@ import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
/** /**
* 使用poi报表导出工具类 下载示例
* 把poi的一个调用接口抽出来便于导出功能的管理
* @author LiangXianYou lxy208@126.com
* @param
*
*/
public class ExcelKit {
/**
* <b>导出list中map做载体的数据到excel</b><br>
* 参数说明<br>
* list:存放了Map数据的集合<br>
* hdNames:表头列名<br>
* hds:对应表头的数据KEY<br>
* xlsName:导出文件名<br>
* @author LiangXianYou
* @date 2015-6-25 上午10:22:51
*/
public static <T> boolean exportByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
Workbook wb = new XSSFWorkbook(); //创建工作薄
Sheet sheet = wb.createSheet(); //创建工作表
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
//写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
for(int i=0;i<hdNames.length;i++){
Cell nCell = nRow.createCell(i); //创建单元格
nCell.setCellValue(hdNames[i]);
}
//写入每一行数据---一条记录就是一行数据
for(int i=0;i<list.size();i++){
for(int j=0;j<hds.length;j++){
Object o = list.get(i).get(hds[j]); //得到列的值
data2Excel(sheet, o, i+1, j); //将值写入Excel
}
}
return downloadExcel(wb, xlsName, request, response);
}
/**
* Excels导出多个sheet
* @author LiangXianYou
* @date 2015-6-16 下午5:56:56
*/
//map:data,sheetName,hds,hdNames,xlsName;request,response
public static <T> boolean exportExcels(List<Map<String, Object>> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
Workbook wb = new XSSFWorkbook(); //创建工作薄
for(int i=0;i<list.size();i++){
Map<String, Object> map = list.get(i);
Sheet sheet = wb.createSheet(); //创建工作表
String sheetName = (String) map.get("sheetName");
wb.setSheetName(i, sheetName);
//写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
String[] hdNames = (String[]) map.get("hdNames");
for(int j=0;j<hdNames.length;j++){
Cell nCell = nRow.createCell(j); //创建单元格
nCell.setCellValue(hdNames[j]);
}
//写入每一行数据---一条记录就是一行数据
@SuppressWarnings("unchecked")
List<T> data = (List<T>) map.get("data");
String[] hds = (String[]) map.get("hds");
for(int j=0;j<data.size();j++){
for(int k=0;k<hds.length;k++){
Object o = getFieldValue(data.get(j), hds[k]); //得到列的值
data2Excel(sheet, o, j+1, k); //将值写入Excel
}
}
}
return downloadExcel(wb, xlsName, request, response);
}
/**
* 参数说明
* listlist数据集合
* hdNames表头需要显示的名称
* hds表头对应的对象属性名称 hdNames一一对应
* xlsName导出Excel的预定义名称
* request: HttpServletRequest
* responseHttpServletResponse
* 通过数据构建Excel
* @author LiangXianYou
* @throws Exception
* @date 2015-3-13 上午11:00:30
*/
public static <T> boolean exportExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
Workbook wb = new XSSFWorkbook(); //创建工作薄
Sheet sheet = wb.createSheet(); //创建工作表
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
//写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
for(int i=0;i<hdNames.length;i++){
Cell nCell = nRow.createCell(i); //创建单元格
nCell.setCellValue(hdNames[i]);
}
//写入每一行数据---一条记录就是一行数据
for(int i=0;i<list.size();i++){
for(int j=0;j<hds.length;j++){
Object o = getFieldValue(list.get(i), hds[j]); //得到列的值
data2Excel(sheet, o, i+1, j); //将值写入Excel
}
}
return downloadExcel(wb, xlsName, request, response);
}
/**
* 传递一个Wookbook给定文件名以及request和response下载Excel文档
* @author LiangXianYou
* @throws IOException
* @date 2015-3-13 上午9:00:36
*/
private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{ private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{
if(request.getHeader("user-agent").indexOf("MSIE") != -1) { if(request.getHeader("user-agent").indexOf("MSIE") != -1) {
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls"; xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
@ -148,42 +28,133 @@ public class ExcelKit {
return true; return true;
} }
* 使用poi报表导出工具类
* 把poi的一个调用接口抽出来便于导出功能的管理
* @author LiangXianYou lxy208@126.com
* @param
*
*/
public class ExcelKit {
/**
* Excels导出多个sheet
*
* @author LiangXianYou
* @date 2015-6-16 下午5:56:56
*/
//map:data,sheetName,hds,hdNames,xlsName;
public static <T> Workbook exportExcels(List<Map<String, Object>> list) throws Exception {
Workbook wb = new XSSFWorkbook(); //创建工作薄
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
Sheet sheet = wb.createSheet(); //创建工作表
String sheetName = (String) map.get("sheetName");
wb.setSheetName(i, sheetName);
//写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
String[] hdNames = (String[]) map.get("hdNames");
for (int j = 0; j < hdNames.length; j++) {
Cell nCell = nRow.createCell(j); //创建单元格
nCell.setCellValue(hdNames[j]);
}
//写入每一行数据---一条记录就是一行数据
@SuppressWarnings("unchecked")
List<T> data = (List<T>) map.get("data");
String[] hds = (String[]) map.get("hds");
for (int j = 0; j < data.size(); j++) {
for (int k = 0; k < hds.length; k++) {
Object o = getFieldValue(data.get(j), hds[k]); //得到列的值
data2Excel(sheet, o, j + 1, k); //将值写入Excel
}
}
}
return wb;
}
/**
* 参数说明
* listlist数据集合
* hdNames表头需要显示的名称
* hds表头对应的对象属性名称 hdNames一一对应
* xlsName导出Excel的预定义名称
* request: HttpServletRequest
* responseHttpServletResponse
* 通过数据构建Excel
*
* @throws Exception
* @author LiangXianYou
* @date 2015-3-13 上午11:00:30
*/
public static <T> Workbook exportExcel(List<T> list, String[] hdNames, String[] hds) throws Exception {
Workbook wb = new XSSFWorkbook(); //创建工作薄
Sheet sheet = wb.createSheet(); //创建工作表
sheet.autoSizeColumn((short) 0); //自适应宽度
//写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
for (int i = 0; i < hdNames.length; i++) {
Cell nCell = nRow.createCell(i); //创建单元格
nCell.setCellValue(hdNames[i]);
}
//写入每一行数据---一条记录就是一行数据
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < hds.length; j++) {
Object o = getFieldValue(list.get(i), hds[j]); //得到列的值
data2Excel(sheet, o, i + 1, j); //将值写入Excel
}
}
return wb;
}
/** /**
* 通过泛型实例对象得到某一字段值 * 通过泛型实例对象得到某一字段值
*
* @author LiangXianYou * @author LiangXianYou
* @date 2015-3-13 上午10:53:32 * @date 2015-3-13 上午10:53:32
*/ */
private static <T> Object getFieldValue(T t, String fieldName) throws Exception{ private static <T> Object getFieldValue(T t, String fieldName) throws Exception {
if(t == null){ Object v;
return null;
} if (t == null) {
v = null;
} else if (t instanceof Map) {
v = ((Map) t).get(fieldName); //得到列的值
} else {
Field field = t.getClass().getDeclaredField(fieldName); Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);//暴力反射 field.setAccessible(true); //暴力反射
Object o = field.get(t);//得到字段数据的值 v = field.get(t); //得到字段数据的值
return o; }
return v;
} }
/** /**
* 将数据写到Excel中 * 将数据写到Excel中
*
* @author LiangXianYou * @author LiangXianYou
* @date 2015-3-13 上午10:37:55 * @date 2015-3-13 上午10:37:55
*/ */
private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c){ private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c) {
//通过获得sheet中的某一列有得到没有创建 //通过获得sheet中的某一列有得到没有创建
Row nRow = sheet.getRow(r); Row nRow = sheet.getRow(r);
if(nRow == null){ if (nRow == null) {
nRow = sheet.createRow(r); nRow = sheet.createRow(r);
} }
Cell nCell = nRow.createCell(c); Cell nCell = nRow.createCell(c);
//根据不同类型进行转化如有其它类型没有考虑周全的使用发现的时候添加 //根据不同类型进行转化如有其它类型没有考虑周全的使用发现的时候添加
if (o instanceof Integer) nCell.setCellValue((Integer)o); if (o instanceof Integer) nCell.setCellValue((Integer) o);
else if (o instanceof Double) nCell.setCellValue((Double)o); else if (o instanceof Double) nCell.setCellValue((Double) o);
else if (o instanceof Float) nCell.setCellValue((Float)o); else if (o instanceof Float) nCell.setCellValue((Float) o);
else if (o instanceof String) nCell.setCellValue((String)o); else if (o instanceof String) nCell.setCellValue((String) o);
else if (o instanceof Date) nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o)); else if (o instanceof Date) nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));
else if (o instanceof Calendar) nCell.setCellValue((Calendar)o); else if (o instanceof Calendar) nCell.setCellValue((Calendar) o);
else if (o instanceof Boolean) nCell.setCellValue((Boolean)o); else if (o instanceof Boolean) nCell.setCellValue((Boolean) o);
else if (o == null) nCell.setCellValue(""); else if (o == null) nCell.setCellValue("");
else nCell.setCellValue(o + ""); else nCell.setCellValue(o + "");
} }
@ -195,6 +166,7 @@ public class ExcelKit {
List<Map> list = readExcel(file, fields, 1, null); List<Map> list = readExcel(file, fields, 1, null);
return list.size() > 0 ? list.get(0) : new HashMap(); return list.size() > 0 ? list.get(0) : new HashMap();
} }
//read excel head by sheetName //read excel head by sheetName
public static Map readExcelHead(File file, String[] fields, String sheetName) throws IOException { public static Map readExcelHead(File file, String[] fields, String sheetName) throws IOException {
List<Map> list = readExcel(file, fields, 1, sheetName); List<Map> list = readExcel(file, fields, 1, sheetName);
@ -233,20 +205,20 @@ public class ExcelKit {
//read excel sheet[0] //read excel sheet[0]
private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException { private static List<Map> readExcel(File file, String[] fields, int lastRowNum, String sheetName) throws IOException {
Workbook wk; Workbook wk = getWorkbook(file);
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName); Sheet sheet = sheetName == null ? wk.getSheetAt(0) : wk.getSheet(sheetName);
if (sheet == null) throw new OfficeXmlFileException("sheet["+sheetName+"] can't find"); if (sheet == null) throw new OfficeXmlFileException("sheet[" + sheetName + "] can't findList");
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()) {
lastRowNum = sheet.getLastRowNum();
}
return readExcel(sheet, fields, lastRowNum); return readExcel(sheet, fields, lastRowNum);
} }
/** /**
* read excel all sheet, * read excel all sheet,
* can read excel 2007+ and 2007- * can read excel 2007+ and 2007-
*
* @param file * @param file
* @param fields * @param fields
* @param lastRowNum * @param lastRowNum
@ -254,12 +226,7 @@ public class ExcelKit {
* @throws IOException * @throws IOException
*/ */
private static Map<String, List<Map>> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException { private static Map<String, List<Map>> readExcelAll(File file, String[] fields, int lastRowNum) throws IOException {
Workbook wk; Workbook wk = getWorkbook(file);
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
Map<String, List<Map>> data = new LinkedHashMap<>(); Map<String, List<Map>> data = new LinkedHashMap<>();
//reading all sheet //reading all sheet
@ -268,7 +235,7 @@ public class ExcelKit {
List<Map> maps = readExcel(sheet, fields, lastRowNum); List<Map> maps = readExcel(sheet, fields, lastRowNum);
//move head //move head
if (lastRowNum < 0 && maps.size() > 0){ if (lastRowNum < 0 && maps.size() > 0) {
maps.remove(0); maps.remove(0);
} }
data.put(sheet.getSheetName(), maps); data.put(sheet.getSheetName(), maps);
@ -278,40 +245,42 @@ public class ExcelKit {
/** /**
* read excel * read excel
*
* @author Lxyer 2016/8/1 10:32. * @author Lxyer 2016/8/1 10:32.
*/ */
private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException { private static List<Map> readExcel(Sheet sheet, String[] fields, int lastRowNum) throws OfficeXmlFileException {
if (fields == null || fields.length == 0){ if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()) {
lastRowNum = sheet.getLastRowNum();
}
if (fields == null || fields.length == 0) {
return readExcel(sheet, lastRowNum); return readExcel(sheet, lastRowNum);
} }
List<Map> list = new ArrayList<>(); List<Map> list = new ArrayList<>();
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
lastRowNum = sheet.getLastRowNum();
}
int t = 0; int t = 0;
r:for (int i=0; i<=lastRowNum; i++){ r:
for (int i = 0; i <= lastRowNum; i++) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
if (row == null) continue ; if (row == null) continue;
short cellNum = row.getLastCellNum(); short cellNum = row.getLastCellNum();
//空跳过/连续三行为空结束 //空跳过/连续三行为空结束
if (isEmptyRow(row, fields.length)) { if (isEmptyRow(row, fields.length)) {
if (t++ > 3) break; if (t++ > 3) break;
continue ; continue;
} }
Map map = new HashMap(); Map map = new HashMap();
for (int j=0; j<cellNum && j<fields.length; j++){ for (int j = 0; j < cellNum && j < fields.length; j++) {
Cell cell = row.getCell(j); Cell cell = row.getCell(j);
if (cell == null){ if (cell == null) {
map.put(fields[j], ""); map.put(fields[j], "");
continue; continue;
} }
if (cell.getCellTypeEnum() == CellType.NUMERIC){ if (cell.getCellTypeEnum() == CellType.NUMERIC) {
map.put(fields[j], (long)cell.getNumericCellValue()+""); map.put(fields[j], (long) cell.getNumericCellValue() + "");
}else { } else {
map.put(fields[j], cell.getStringCellValue()); map.put(fields[j], cell.getStringCellValue());
} }
} }
@ -321,39 +290,39 @@ public class ExcelKit {
} }
/** /**
*
* @author Lxyer 2016/9/21 00:38. * @author Lxyer 2016/9/21 00:38.
*/ */
private static List<Map> readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException { private static List<Map> readExcel(Sheet sheet, int lastRowNum) throws OfficeXmlFileException {
List<Map> list = new ArrayList<>(); List<Map> list = new ArrayList<>();
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){ if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()) {
lastRowNum = sheet.getLastRowNum(); lastRowNum = sheet.getLastRowNum();
} }
int t = 0; int t = 0;
r:for (int i=0; i<=lastRowNum; i++){ r:
for (int i = 0; i <= lastRowNum; i++) {
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
if (row == null) continue ; if (row == null) continue;
short cellNum = row.getLastCellNum(); short cellNum = row.getLastCellNum();
//空跳过/连续三行为空结束 //空跳过/连续三行为空结束
if (isEmptyRow(row, 3)) { if (isEmptyRow(row, 3)) {
if (t++ > 3) break; if (t++ > 3) break;
continue ; continue;
} }
Map map = new HashMap(); Map map = new HashMap();
for (int j=0; j < cellNum; j++){ for (int j = 0; j < cellNum; j++) {
String field = (char)((j/26 ==0? ' ' : 'a')+j/26) + "" +(char)('a'+j%26); String field = (char) ((j / 26 == 0 ? ' ' : 'a') + j / 26) + "" + (char) ('a' + j % 26);
field = field.replace(" ",""); field = field.replace(" ", "");
Cell cell = row.getCell(j); Cell cell = row.getCell(j);
if (cell == null){ if (cell == null) {
map.put(field, ""); map.put(field, "");
continue; continue;
} }
if (cell.getCellTypeEnum() == CellType.NUMERIC){ if (cell.getCellTypeEnum() == CellType.NUMERIC) {
map.put(field, (long)cell.getNumericCellValue()+""); map.put(field, (long) cell.getNumericCellValue() + "");
}else { } else {
map.put(field, cell.getStringCellValue()); map.put(field, cell.getStringCellValue());
} }
} }
@ -363,13 +332,13 @@ public class ExcelKit {
} }
//空跳过/连续三行为空结束 //空跳过/连续三行为空结束
private static boolean isEmptyRow(Row row, int len){ private static boolean isEmptyRow(Row row, int len) {
for (int i = 0; i< row.getLastCellNum() && i < len; i++) { for (int i = 0; i < row.getLastCellNum() && i < len; i++) {
Cell cell = row.getCell(i);// Cell cell = row.getCell(i);//
if (cell != null){ if (cell != null) {
if (cell.getCellTypeEnum() != CellType.NUMERIC && cell.getStringCellValue() != null && !cell.getStringCellValue().isEmpty()){ if (cell.getCellTypeEnum() != CellType.NUMERIC && cell.getStringCellValue() != null && !cell.getStringCellValue().isEmpty()) {
return false; return false;
}else if (cell.getCellTypeEnum() == CellType.NUMERIC && cell.getNumericCellValue() != 0){ } else if (cell.getCellTypeEnum() == CellType.NUMERIC && cell.getNumericCellValue() != 0) {
return false; return false;
} }
} }
@ -379,13 +348,7 @@ public class ExcelKit {
//get all sheet names //get all sheet names
public static List<String> getSheetNames(File file) throws IOException { public static List<String> getSheetNames(File file) throws IOException {
Workbook wk; Workbook wk = getWorkbook(file);
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
}catch (OfficeXmlFileException e){
wk = new XSSFWorkbook(new FileInputStream(file));
}
List<String> sheetNames = new ArrayList<>(); List<String> sheetNames = new ArrayList<>();
for (int i = 0; i < wk.getNumberOfSheets(); i++) { for (int i = 0; i < wk.getNumberOfSheets(); i++) {
sheetNames.add(wk.getSheetAt(i).getSheetName()); sheetNames.add(wk.getSheetAt(i).getSheetName());
@ -393,4 +356,15 @@ public class ExcelKit {
return sheetNames; return sheetNames;
} }
private static Workbook getWorkbook(File file) throws IOException {
Workbook wk;
try {
wk = new HSSFWorkbook(new FileInputStream(file));//if excel version 2007+ will throws OfficeXmlFileException
} catch (OfficeXmlFileException e) {
wk = new XSSFWorkbook(new FileInputStream(file));
}
return wk;
}
} }

View File

@ -14,7 +14,7 @@ import java.util.Map;
public class ExcelKitTest { public class ExcelKitTest {
//read excel by fields //read excel by fields
@Test //@Test
public void readTest() throws IOException { public void readTest() throws IOException {
/*String sql1 = "select a, b, c from Sheet1"; /*String sql1 = "select a, b, c from Sheet1";
String sql2 = "select a, b, c from Sheet1 where a=1 order by b"; String sql2 = "select a, b, c from Sheet1 where a=1 order by b";
@ -33,7 +33,7 @@ public class ExcelKitTest {
} }
//read excel no fields //read excel no fields
@Test //@Test
public void readTest2() throws IOException { public void readTest2() throws IOException {
List<Map> list = ExcelKit.readExcel(new File("res/test.xlsx")); List<Map> list = ExcelKit.readExcel(new File("res/test.xlsx"));

View File

@ -1,13 +1,10 @@
package com.lxyer.excel; package com.lxyer.excel;
import org.junit.Test; import java.io.BufferedOutputStream;
import org.redkale.net.http.HttpResponse; import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest; import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -25,7 +22,7 @@ import java.util.zip.ZipOutputStream;
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");
@ -52,7 +49,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();
@ -101,6 +98,7 @@ public class ExportKitTest {
* @return * @return
* @throws IOException * @throws IOException
*/ */
/*
public <T> boolean exportExcel(Map head, List<T> rows, HttpServletRequest request, HttpServletResponse response, String xlsName, BiFunction fun) throws IOException { public <T> boolean exportExcel(Map head, List<T> rows, HttpServletRequest request, HttpServletResponse response, String xlsName, BiFunction fun) throws IOException {
if(request.getHeader("user-agent") != null && request.getHeader("user-agent").indexOf("MSIE") != -1) { if(request.getHeader("user-agent") != null && request.getHeader("user-agent").indexOf("MSIE") != -1) {
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls"; xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
@ -120,6 +118,7 @@ public class ExportKitTest {
os.write(ExcelExportKit.exportExcel(head, rows, fun).toString().getBytes()); os.write(ExcelExportKit.exportExcel(head, rows, fun).toString().getBytes());
return true; return true;
} }
*/
/** /**
* 4导出压缩的excel示例 * 4导出压缩的excel示例
@ -156,6 +155,7 @@ public class ExportKitTest {
* @return * @return
* @throws IOException * @throws IOException
*/ */
/*
public static <T,U,R> void exportExcel(Map head, List<T> rows, HttpResponse response, String xlsName, BiFunction<T,U,R> fun) throws IOException { public static <T,U,R> void exportExcel(Map head, List<T> rows, HttpResponse response, String xlsName, BiFunction<T,U,R> fun) throws IOException {
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls"; xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
@ -165,4 +165,5 @@ public class ExportKitTest {
StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun); StringBuilder buf = ExcelExportKit.exportExcel(head, rows, fun);
response.finish(buf.toString().getBytes()); response.finish(buf.toString().getBytes());
} }
*/
} }