升级poi到最新版3.17

pull/25/head
RuoYi 2018-09-04 16:49:23 +08:00
parent 1cb672f8bd
commit 84068a2e2e
2 changed files with 177 additions and 143 deletions

View File

@ -238,7 +238,7 @@
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>3.9</version> <version>3.17</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -13,6 +13,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@ -21,17 +22,21 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellRangeAddressList;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Excel; import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager; import com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager;
@ -98,6 +103,8 @@ public class ExcelUtil<T>
if (rows > 0) if (rows > 0)
{ {
// 默认序号
int serialNum = 0;
// 有数据时才处理 得到类的所有field. // 有数据时才处理 得到类的所有field.
Field[] allFields = clazz.getDeclaredFields(); Field[] allFields = clazz.getDeclaredFields();
// 定义一个map用于存放列的序号和field. // 定义一个map用于存放列的序号和field.
@ -110,14 +117,14 @@ public class ExcelUtil<T>
{ {
// 设置类的私有字段属性可访问. // 设置类的私有字段属性可访问.
field.setAccessible(true); field.setAccessible(true);
fieldsMap.put(col, field); fieldsMap.put(++serialNum, field);
} }
} }
for (int i = 1; i < rows; i++) for (int i = 1; i < rows; i++)
{ {
// 从第2行开始取数据,默认第一行是表头. // 从第2行开始取数据,默认第一行是表头.
Row row = sheet.getRow(i); Row row = sheet.getRow(i);
int cellNum = sheet.getRow(0).getPhysicalNumberOfCells(); int cellNum = serialNum;
T entity = null; T entity = null;
for (int j = 0; j < cellNum; j++) for (int j = 0; j < cellNum; j++)
{ {
@ -129,7 +136,7 @@ public class ExcelUtil<T>
else else
{ {
// 先设置Cell的类型然后就可以把纯数字作为String类型读进来了 // 先设置Cell的类型然后就可以把纯数字作为String类型读进来了
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING); row.getCell(j).setCellType(CellType.STRING);
cell = row.getCell(j); cell = row.getCell(j);
} }
@ -178,7 +185,7 @@ public class ExcelUtil<T>
} }
else if (java.util.Date.class == fieldType) else if (java.util.Date.class == fieldType)
{ {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) if (cell.getCellTypeEnum() == CellType.NUMERIC)
{ {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell.setCellValue(sdf.format(cell.getNumericCellValue())); cell.setCellValue(sdf.format(cell.getNumericCellValue()));
@ -210,6 +217,11 @@ public class ExcelUtil<T>
* @param sheetName * @param sheetName
*/ */
public AjaxResult exportExcel(List<T> list, String sheetName) public AjaxResult exportExcel(List<T> list, String sheetName)
{
OutputStream out = null;
HSSFWorkbook workbook = null;
try
{ {
// 得到所有定义字段 // 得到所有定义字段
Field[] allFields = clazz.getDeclaredFields(); Field[] allFields = clazz.getDeclaredFields();
@ -224,7 +236,7 @@ public class ExcelUtil<T>
} }
// 产生工作薄对象 // 产生工作薄对象
HSSFWorkbook workbook = new HSSFWorkbook(); workbook = new HSSFWorkbook();
// excel2003中每个sheet中最多有65536行 // excel2003中每个sheet中最多有65536行
int sheetSize = 65536; int sheetSize = 65536;
// 取出一共有多少个sheet. // 取出一共有多少个sheet.
@ -255,30 +267,30 @@ public class ExcelUtil<T>
// 创建列 // 创建列
cell = row.createCell(i); cell = row.createCell(i);
// 设置列中写入内容为String类型 // 设置列中写入内容为String类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
HSSFCellStyle cellStyle = workbook.createCellStyle(); HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
if (attr.name().indexOf("注:") >= 0) if (attr.name().indexOf("注:") >= 0)
{ {
HSSFFont font = workbook.createFont(); HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED); font.setColor(HSSFFont.COLOR_RED);
cellStyle.setFont(font); cellStyle.setFont(font);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); cellStyle.setFillForegroundColor(HSSFColorPredefined.YELLOW.getIndex());
sheet.setColumnWidth(i, 6000); sheet.setColumnWidth(i, 6000);
} }
else else
{ {
HSSFFont font = workbook.createFont(); HSSFFont font = workbook.createFont();
// 粗体显示 // 粗体显示
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setBold(true);
// 选择需要用到的字体格式 // 选择需要用到的字体格式
cellStyle.setFont(font); cellStyle.setFont(font);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); cellStyle.setFillForegroundColor(HSSFColorPredefined.LIGHT_YELLOW.getIndex());
// 设置列宽 // 设置列宽
sheet.setColumnWidth(i, 3766); sheet.setColumnWidth(i, 3766);
} }
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setWrapText(true); cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
@ -303,8 +315,8 @@ public class ExcelUtil<T>
int endNo = Math.min(startNo + sheetSize, list.size()); int endNo = Math.min(startNo + sheetSize, list.size());
// 写入各条记录,每条记录对应excel表中的一行 // 写入各条记录,每条记录对应excel表中的一行
HSSFCellStyle cs = workbook.createCellStyle(); HSSFCellStyle cs = workbook.createCellStyle();
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); cs.setAlignment(HorizontalAlignment.CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cs.setVerticalAlignment(VerticalAlignment.CENTER);
for (int i = startNo; i < endNo; i++) for (int i = startNo; i < endNo; i++)
{ {
row = sheet.createRow(i + 1 - startNo); row = sheet.createRow(i + 1 - startNo);
@ -333,12 +345,12 @@ public class ExcelUtil<T>
} }
// 如果可以转成数字则导出为数字类型 // 如果可以转成数字则导出为数字类型
BigDecimal bc = new BigDecimal(String.valueOf(field.get(vo))); BigDecimal bc = new BigDecimal(String.valueOf(field.get(vo)));
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellType(CellType.NUMERIC);
cell.setCellValue(bc.doubleValue()); cell.setCellValue(bc.doubleValue());
} }
catch (Exception e) catch (Exception e)
{ {
cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellType(CellType.STRING);
if (vo == null) if (vo == null)
{ {
// 如果数据存在就填入,不存在填入空格. // 如果数据存在就填入,不存在填入空格.
@ -360,19 +372,41 @@ public class ExcelUtil<T>
} }
} }
} }
try
{
String filename = encodingFilename(sheetName); String filename = encodingFilename(sheetName);
OutputStream out = new FileOutputStream(getfile() + filename); out = new FileOutputStream(getfile() + filename);
workbook.write(out); workbook.write(out);
out.close();
return AjaxResult.success(filename); return AjaxResult.success(filename);
} }
catch (Exception e) catch (Exception e)
{ {
log.error("关闭flush失败{}", e.getMessage()); log.error("导出Excel异常{}", e.getMessage());
return AjaxResult.error("导出Excel失败请联系网站管理员"); return AjaxResult.error("导出Excel失败请联系网站管理员");
} }
finally
{
if (workbook != null)
{
try
{
workbook.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
if (out != null)
{
try
{
out.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
} }
/** /**