Commit 685bf41b authored by 王琮's avatar 王琮

修改员工导出模板

parent 5f1fffbf
......@@ -12,10 +12,11 @@ import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.rcisoft.core.exception.CyServiceException;
import org.rcisoft.sys.sysuser.dto.ExcelDTO;
......@@ -152,8 +153,7 @@ public class CyEpExcelUtil {
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
}
catch (Exception e) {
} catch (Exception e) {
throw new CyServiceException(502, "模板格式错误");
}
return list;
......@@ -180,27 +180,43 @@ public class CyEpExcelUtil {
private static void defaultExportSelect(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams, ExcelDTO excelDto) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
workbook.createSheet("hidden");
if (excelDto != null && null != excelDto.getSex() && excelDto.getSex().length > 0) {
CyEpExcelUtil.selectList(workbook, 3, 510, 1, 1, excelDto.getSex());
CyEpExcelUtil.selectList(workbook, 3, 510, 1, 1, excelDto.getSex(), "sex");
}
if (excelDto != null && null != excelDto.getDeptName() && excelDto.getDeptName().length > 0) {
CyEpExcelUtil.selectList(workbook, 3, 510, 4, 4, excelDto.getDeptName());
CyEpExcelUtil.selectList(workbook, 3, 510, 4, 4, excelDto.getDeptName(), "dept");
}
if (excelDto != null && null != excelDto.getYq() && excelDto.getYq().length > 0) {
CyEpExcelUtil.selectList(workbook, 3, 510, 3, 3, excelDto.getYq());
CyEpExcelUtil.selectList(workbook, 3, 510, 3, 3, excelDto.getYq(), "yq");
}
if (excelDto != null && null != excelDto.getZw() && excelDto.getZw().length > 0) {
CyEpExcelUtil.selectList(workbook, 3, 510, 5, 5, excelDto.getZw());
CyEpExcelUtil.selectList(workbook, 3, 510, 5, 5, excelDto.getZw(), "zw");
}
// 班次
if (excelDto != null && null != excelDto.getShift() && excelDto.getShift().length > 0) {
CyEpExcelUtil.selectList(workbook, 3, 510, 6, 6, excelDto.getShift());
CyEpExcelUtil.selectList(workbook, 3, 510, 6, 6, excelDto.getShift(), "shift");
}
if (workbook != null) {
downLoadExcel(fileName, response, workbook);
}
}
public static String excelColIndexToStr(int columnIndex) {
if (columnIndex <= 0) {
return null;
}
String columnStr = "";
columnIndex--;
do {
if (columnStr.length() > 0) {
columnIndex--;
}
columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;
columnIndex = (int) ((columnIndex - columnIndex % 26) / 26);
} while (columnIndex > 0);
return columnStr;
}
/**
* firstRow 開始行號 根据此项目,默认为2(下标0开始)
......@@ -209,18 +225,45 @@ public class CyEpExcelUtil {
* lastCol 区域中最后一个单元格的列号
* strings 下拉内容
*/
public static void selectList(Workbook workbook, int firstRow, int lastRow, int firstCol, int lastCol, String[] strings) {
public static void selectList(Workbook workbook, int firstRow, int lastRow, int firstCol, int lastCol,
String[] strings, String cellName) {
Sheet sheet = workbook.getSheetAt(0);
// 生成下拉列表
Sheet hidden = workbook.getSheet("hidden");
int index = workbook.getSheetIndex("hidden");
int last = hidden.getLastRowNum();
//创建单元格对象
Cell cell = null;
//遍历我们上面的数组,将数据取出来放到新sheet的单元格中
for (int i = 0, length = strings.length; i < length; i++) {
//取出数组中的每个元素
String name = strings[i];
//根据i创建相应的行对象(说明我们将会把每个元素单独放一行)
Row row = null;
if (hidden.getRow(i) == null)
row = hidden.createRow(i);
else
row = hidden.getRow(i);
//创建每一行中的第一个单元格
cell = row.createCell(firstCol);
//然后将数组中的元素赋值给这个单元格
cell.setCellValue(name);
last = hidden.getLastRowNum();
}
// 创建名称,可被其他单元格引用
Name namedCell = workbook.createName();
namedCell.setNameName(cellName);
String letter = excelColIndexToStr(firstCol + 1);
// 设置名称引用的公式
namedCell.setRefersToFormula("hidden!$" + letter + "$1:$" + letter + "$" + strings.length);
// 只对(x,x)单元格有效
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
// 生成下拉框内容
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(strings);
DVConstraint dvConstraint = DVConstraint.createFormulaListConstraint(cellName);
HSSFDataValidation dataValidation = new HSSFDataValidation(cellRangeAddressList, dvConstraint);
workbook.setSheetHidden(index, true);
// HSSFDataValidation dataValidation = getDataValidationList4Col(sheet,firstRow,firstCol,lastRow,lastCol,strings,workbook);
// 对sheet页生效
sheet.addValidationData(dataValidation);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment