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