1.pom依赖
<!-- excel import begin -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- excel import end -->
2.CommonUtil工具类(为方便运算)
public class CommonUtil {
public static boolean isEmpty(String s) {
return s == null || s.equals("");
}
public static boolean isEmpty(Object obj) {
if (obj == null) {
return true;
} else if (obj instanceof String) {
String instance = (String) obj;
if (instance.trim().length() <= 0 || "null".equalsIgnoreCase(instance)) {
return true;
}
} else if (obj instanceof Integer) {
Integer instance = (Integer) obj;
if (instance < 0) {
return true;
}
} else if (obj instanceof List<?>) {
List<?> instance = (List<?>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Map<?, ?>) {
Map<?, ?> instance = (Map<?, ?>) obj;
if (instance.size() <= 0) {
return true;
}
} else if (obj instanceof Object[]) {
Object[] instance = (Object[]) obj;
if (instance.length <= 0) {
return true;
}
} else if (obj instanceof Long) {
Long instance = (Long) obj;
if (instance < 0) {
return true;
}
}
return false;
}
public static boolean notEmpty(Object obj) {
return !isEmpty(obj);
}
public static boolean exactEqual(String source, String target) {
if (source == null || target == null) {
return false;
}
if (source.length() != target.length()) {
return false;
}
char[] sc = source.toCharArray();
char[] tc = target.toCharArray();
for (int i = 0; i < sc.length; i++) {
if (sc[i] == tc[i]) {
continue;
} else {
return false;
}
}
return true;
}
public static boolean isNumber(String str) {
if (isEmpty(str)) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]*");
if (str.indexOf(".") > 0) {
if (str.indexOf(".") == str.lastIndexOf(".") && str.split("\\.").length == 2) {
return pattern.matcher(str.replace(".", "")).matches();
} else {
return false;
}
} else {
return pattern.matcher(str).matches();
}
}
public static boolean isInteger(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
public static boolean isDouble(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$");
return pattern.matcher(str).matches();
}
public static boolean isDate(String date) {
Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$");
return p.matcher(date).matches();
}
public static Date parseDate(String string) {
if (notEmpty(string)) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(string);
} catch (ParseException e) {
}
}
return null;
}
public static String join(String separator, List<String> strList) {
StringBuilder sb = new StringBuilder();
for (String string : strList) {
sb.append(separator + string);
}
String saveStr = sb.toString();
return saveStr.length() > 0 ? saveStr.substring(separator.length()) : "";
}
public static boolean isOutLength(String s, int l) {
if (null == s || 0 == s.length()) {
return false;
}
if (s.length() > l) {
return true;
}
return false;
}
public static String extractText(String htmlStr) {
String regExScript = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
String regExStyle = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
String regExHtml = "<[^>]+>";
Pattern pScript = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
Matcher mScript = pScript.matcher(htmlStr);
htmlStr = mScript.replaceAll("");
Pattern pStyle = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
Matcher mStyle = pStyle.matcher(htmlStr);
htmlStr = mStyle.replaceAll("");
Pattern pHtml = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
Matcher mHtml = pHtml.matcher(htmlStr);
htmlStr = mHtml.replaceAll("");
return htmlStr;
}
}
3.ExcelWriteUtil (EXCEL导出工具类)
public class ExcelWriteUtil {
public static XSSFWorkbook dropDownList2007(Workbook wb, int sheetnum, String[] datas, int startRow, int endRow, int startCol, int endCol) throws Exception {
XSSFWorkbook workbook = (XSSFWorkbook) wb;
dropDownList2007(workbook.getSheetAt(sheetnum), datas, startRow, endRow, startCol, endCol);
return workbook;
}
public static XSSFSheet dropDownList2007(XSSFSheet realSheet, String[] datas, int startRow, int endRow, int startCol, int endCol) {
if (datas.length > 0) {
String[] array = valiDatas(datas);
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(realSheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(array);
CellRangeAddressList addressList = null;
XSSFDataValidation validation = null;
addressList = new CellRangeAddressList(startRow, endRow, startCol, endCol);
validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
realSheet.addValidationData(validation);
}
return realSheet;
}
private static String[] valiDatas(String[] datas) {
List<String> list = new ArrayList<String>();
for (String string : datas) {
if (list.contains(string)) {
continue;
}
list.add(string);
}
String temp = "";
for (String string : list) {
temp += "," + string;
}
if (temp.startsWith(",")) {
temp = temp.substring(1);
}
int maxLength = 255;
if (temp.length() > maxLength) {
String end = temp.substring(maxLength);
temp = temp.substring(0, maxLength);
if (!end.startsWith(",")) {
temp = temp.substring(0, temp.lastIndexOf(","));
}
}
String[] result = temp.split(",");
return result;
}
public static XSSFWorkbook createTemplate2007(String sheetName, int titleRow, List<String> titleList) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
if (CommonUtil.isEmpty(sheetName)) {
sheetName = "模板";
}
createSheet(workbook, sheetName, titleRow, titleList);
return workbook;
}
public static XSSFSheet createSheet(XSSFWorkbook workbook, String sheetName, int titleRow, List<String> titleList) {
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFRow row = sheet.createRow(titleRow);
CellStyle cellStyle = createCellStyle(workbook, "宋体", 16, IndexedColors.BLACK.getIndex(), true, false, false, false, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = setCellBackgroundStyle(cellStyle, HSSFColor.WHITE.index, CellStyle.BORDER_THIN, true, true, true, true);
int cellIndex = 0;
for (String title : titleList) {
createCell(row, cellIndex, title, cellStyle);
sheet.autoSizeColumn((short) cellIndex);
sheet.setColumnWidth(cellIndex, sheet.getColumnWidth(cellIndex) * 17 / 10);
cellIndex++;
}
return sheet;
}
public static Row createRow(XSSFWorkbook workbook, int sheetNum, int rowNum) {
return workbook.getSheetAt(sheetNum).createRow(rowNum);
}
public static void createExcelRow(XSSFWorkbook workbook, XSSFSheet sheet, int titleRow, List<String> titleList) {
XSSFRow row = sheet.createRow(titleRow);
int cellIndex = 0;
for (String title : titleList) {
createCell(row, cellIndex, title, null);
cellIndex++;
}
}
public static Cell createCell(Row row, int cellIndex, String cellContent, CellStyle cellStyle) {
Cell cell = row.createCell(cellIndex);
cell.setCellValue(cellContent);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
return cell;
}
public static CellStyle createCellStyle(Workbook wb, String fontName, int fontSize, int colorIndex, boolean isBold, boolean isItalic, boolean isStrike, boolean isUnderline, int underLineStyle, int hAlign, int vAlign) {
Font font = wb.createFont();
font.setFontName(fontName);
font.setFontHeightInPoints((short) fontSize);
if (colorIndex >= 0) {
font.setColor((short) colorIndex);
}
if (isBold) {
font.setBold(true);
}
if (isItalic) {
font.setItalic(true);
}
if (isStrike) {
font.setStrikeout(true);
}
if (isUnderline) {
font.setUnderline((byte) underLineStyle);
}
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setFont(font);
cellStyle.setAlignment((short) hAlign);
cellStyle.setVerticalAlignment((short) vAlign);
return cellStyle;
}
public static CellStyle setCellBackgroundStyle(CellStyle cellStyle, short color, short style, boolean isBottom, boolean isLeft, boolean isTop, boolean isRight) {
cellStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
if (isBottom) {
cellStyle.setBorderBottom(style);
}
if (isBottom) {
cellStyle.setBorderLeft(style);
}
if (isBottom) {
cellStyle.setBorderTop(style);
}
if (isBottom) {
cellStyle.setBorderRight(style);
}
return cellStyle;
}
public static void setColumnFormat(XSSFWorkbook wb, String sheetName, CellStyle css, int columnNum, String format) {
DataFormat dataFormat = wb.createDataFormat();
if (css == null) {
css = wb.createCellStyle();
}
css.setDataFormat(dataFormat.getFormat(format));
XSSFSheet sheet = wb.getSheet(sheetName);
sheet.setDefaultColumnStyle(columnNum, css);
}
public static XSSFWorkbook createTimeTableSheet(String sheetName) {
if (CommonUtil.isEmpty(sheetName)) {
sheetName = "模板";
}
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.createSheet(sheetName);
return workbook;
}
public static void addCellComment(XSSFWorkbook workbook, Sheet sheet, int rowIndex, int cellIndex, String commentStr) {
XSSFDrawing draw = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFComment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, 4, 2, 9, 7));
XSSFRichTextString rtf = new XSSFRichTextString(commentStr);
XSSFFont commentFormatter = workbook.createFont();
commentFormatter.setFontName("宋体");
commentFormatter.setFontHeightInPoints((short) 9);
rtf.applyFont(commentFormatter);
comment.setString(rtf);
sheet.getRow(rowIndex).getCell(cellIndex).setCellComment(comment);
}
public static void createExampleRow(XSSFWorkbook workbook, String sheetName, List<String> example, int rowIndex) {
XSSFSheet sheet = workbook.getSheet(sheetName);
ExcelWriteUtil.createExcelRow(workbook, sheet, rowIndex, example);
XSSFRow row = sheet.getRow(rowIndex);
CellStyle style = ExcelWriteUtil.createCellStyle(workbook, "宋体", 12, IndexedColors.BLACK.getIndex(), false, false, false, false, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
for (int i = 0; i < example.size(); i++) {
row.getCell(i).setCellStyle(style);
}
ExcelWriteUtil.addCellComment(workbook, sheet, rowIndex, 0, "示例行,不要删除.");
}
public static CellStyle setBorderStyle(CellStyle style, short top, short right, short bottom, short left) {
style.setBorderTop(top);
style.setBorderRight(right);
style.setBorderBottom(bottom);
style.setBorderLeft(left);
return style;
}
public static String objToString(Object obj) {
return CommonUtil.isEmpty(obj) ? "" : obj.toString();
}
}
4.ExcelReadUtil (EXCEL读取工具类)
public class ExcelReadUtil {
public static Workbook getWorkbook(MultipartFile file) {
String name = file.getOriginalFilename();
try {
if (isExcel2007(name)) {
return new XSSFWorkbook(OPCPackage.open(file.getInputStream()));
} else {
return new HSSFWorkbook(file.getInputStream());
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InvalidFormatException ife) {
ife.printStackTrace();
}
return null;
}
public static boolean valueTemplate(Workbook workbook, int sheetIndex, int rowIndex, String[] titles) {
List<String> titleList = new ArrayList<>();
Row row = workbook.getSheetAt(sheetIndex).getRow(rowIndex);
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
String cellValue = "";
if (null != cell) {
cellValue = parseExcel(cell).trim();
}
titleList.add(cellValue);
}
if (titleList.size() != titles.length) {
return false;
}
for (int i = 0; i < titles.length; i++) {
if (!titles[i].equals(titleList.get(i))) {
return false;
}
}
return true;
}
public static List<Map<String, String>> readData(Workbook workbook, int sheetIndex, int startRow, int startCol, String[] colKeys) {
List<Map<String, String>> dataList = new ArrayList<>();
int size = 0;
if (colKeys != null) {
size = colKeys.length;
}
Sheet sheet = workbook.getSheetAt(sheetIndex);
for (int i = startRow; i < sheet.getLastRowNum() + 1; i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
Map<String, String> dataMap = new HashMap<>();
int index = 0;
for (int j = startCol; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
String cellValue = null;
if (null != cell) {
cellValue = parseExcel(cell);
} else {
cellValue = "";
}
String key = "col" + j;
if (size > 0 && size > index) {
key = colKeys[index];
}
index++;
dataMap.put(key, cellValue);
}
if (!dataMap.isEmpty()) {
dataList.add(dataMap);
}
}
return dataList;
}
public static List<Map<String, String>> readExcel(MultipartFile target, int startrow, int startcol, int sheetnum) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
String name = target.getOriginalFilename();
if (isExcel2007(name)) {
varList = readExcel2007(target, startrow, startcol, sheetnum, null);
} else {
varList = readExcel2003(target, startrow, startcol, sheetnum, null);
}
return varList;
}
public static List<Map<String, String>> readExcel(MultipartFile target, int startrow, int startcol, int sheetnum, List<String> cloumNameList) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
String name = target.getOriginalFilename();
if (isExcel2007(name)) {
varList = readExcel2007(target, startrow, startcol, sheetnum, cloumNameList);
} else {
varList = readExcel2003(target, startrow, startcol, sheetnum, cloumNameList);
}
return varList;
}
@SuppressWarnings("resource")
private static List<Map<String, String>> readExcel2003(MultipartFile target, int startrow, int startcol, int sheetnum, List<String> cloumNameList) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
HSSFWorkbook wb = null;
try {
int size = 0;
if (cloumNameList != null && !cloumNameList.isEmpty()) {
size = cloumNameList.size();
}
wb = new HSSFWorkbook(target.getInputStream());
HSSFSheet sheet = wb.getSheetAt(sheetnum);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = startrow; i < rowNum; i++) {
Map<String, String> varpd = new HashMap<String, String>(6);
HSSFRow row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
int index = 0;
for (int j = startcol; j < cellNum; j++) {
HSSFCell cell = row.getCell(j);
String cellValue = null;
if (null != cell) {
cellValue = parseExcel(cell).trim();
} else {
cellValue = "";
}
String var = "var" + j;
if (size > 0 && size > index) {
var = cloumNameList.get(index);
}
index++;
varpd.put(var, cellValue);
}
boolean isEmptyRow = true;
for (Map.Entry<String, String> entry : varpd.entrySet()) {
if (!CommonUtil.isEmpty(entry.getValue().trim())) {
isEmptyRow = false;
break;
}
}
if (!isEmptyRow) {
varList.add(varpd);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
wb = null;
}
return varList;
}
@SuppressWarnings("resource")
private static List<Map<String, String>> readExcel2007(MultipartFile target, int startrow, int startcol, int sheetnum, List<String> cloumNameList) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
XSSFWorkbook wb = null;
try {
int size = 0;
if (cloumNameList != null && !cloumNameList.isEmpty()) {
size = cloumNameList.size();
}
OPCPackage p = OPCPackage.open(target.getInputStream());
wb = new XSSFWorkbook(p);
XSSFSheet sheet = wb.getSheetAt(sheetnum);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = startrow; i < rowNum; i++) {
if (i > 100000) {
break;
}
Map<String, String> varpd = new HashMap<String, String>(6);
XSSFRow row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
int index = 0;
for (int j = startcol; j < cellNum; j++) {
XSSFCell cell = row.getCell(j);
String cellValue = null;
if (null != cell) {
cellValue = parseExcel(cell);
} else {
cellValue = "";
}
String var = "var" + j;
if (size > 0 && size > index) {
var = cloumNameList.get(index);
}
index++;
varpd.put(var, cellValue);
}
boolean isEmptyRow = true;
for (Map.Entry<String, String> entry : varpd.entrySet()) {
if (!CommonUtil.isEmpty(entry.getValue().trim())) {
isEmptyRow = false;
break;
}
}
if (!isEmptyRow) {
varList.add(varpd);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
wb = null;
}
return varList;
}
@SuppressWarnings("resource")
public static List<Map<String, String>> readExcel2003(MultipartFile target, int startrow, int startcol, int sheetnum) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(target.getInputStream());
HSSFSheet sheet = wb.getSheetAt(sheetnum);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = startrow; i < rowNum; i++) {
Map<String, String> varpd = new HashMap<String, String>();
HSSFRow row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for (int j = startcol; j < cellNum; j++) {
HSSFCell cell = row.getCell(j);
String cellValue = null;
if (null != cell) {
cellValue = parseExcel(cell);
} else {
cellValue = "";
}
varpd.put("var" + j, cellValue);
}
varList.add(varpd);
}
} catch (Exception e) {
System.out.println(e);
} finally {
wb = null;
}
return varList;
}
@SuppressWarnings("resource")
public static List<Map<String, String>> readExcel2007(MultipartFile target, int startrow, int startcol, int sheetnum) {
List<Map<String, String>> varList = new ArrayList<Map<String, String>>();
XSSFWorkbook wb = null;
try {
OPCPackage p = OPCPackage.open(target.getInputStream());
wb = new XSSFWorkbook(p);
XSSFSheet sheet = wb.getSheetAt(sheetnum);
int rowNum = sheet.getLastRowNum() + 1;
for (int i = startrow; i < rowNum; i++) {
if (i > 100000) {
break;
}
Map<String, String> varpd = new HashMap<String, String>();
XSSFRow row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for (int j = startcol; j < cellNum; j++) {
XSSFCell cell = row.getCell(j);
String cellValue = null;
if (null != cell) {
cellValue = parseExcel(cell);
} else {
cellValue = "";
}
varpd.put("var" + j, cellValue);
}
varList.add(varpd);
}
} catch (Exception e) {
System.out.println(e);
} finally {
wb = null;
}
return varList;
}
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static String parseExcel(Cell cell) {
String result = new String();
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = null;
if (cell.getCellStyle().getDataFormat() == HSSFDataFormat.getBuiltinFormat("h:mm")) {
sdf = new SimpleDateFormat("HH:mm");
} else {
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
Date date = cell.getDateCellValue();
result = sdf.format(date);
} else if (cell.getCellStyle().getDataFormat() == 58 || cell.getCellStyle().getDataFormat() == 181 || cell.getCellStyle().getDataFormat() == 183) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat();
format.applyPattern("#");
result = format.format(value);
}
break;
case HSSFCell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().toString();
if ("NULL".equals(result)) {
result = null;
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
try {
result = String.valueOf(cell.getNumericCellValue());
} catch (IllegalStateException e) {
result = String.valueOf(cell.getRichStringCellValue());
}
if (CommonUtil.isDouble(result) && result.indexOf(".") + 2 == result.length() && "0".equals(result.substring(result.length() - 1))) {
result = result.substring(0, result.indexOf("."));
}
break;
case HSSFCell.CELL_TYPE_BLANK:
result = "";
default:
result = "";
break;
}
return result.trim();
}
}
-------------------------以下是业务代码示例-------------------------
5.导出示例
@ApiOperation(value = "导出")
@GetMapping("/excel/download")
public void download(HttpServletResponse response) throws Exception {
List<String> titleList = new ArrayList<>();
titleList.add("序号");
titleList.add("资产名称");
titleList.add("资产数量");
titleList.add("计量单位");
titleList.add("单价(元)");
titleList.add("总价(元)");
titleList.add("使用地点");
titleList.add("资产类别");
titleList.add("采购日期");
titleList.add("报废年限(年)");
titleList.add("使用情况");
XSSFWorkbook workbook = ExcelWriteUtil.createTemplate2007("资产列表", 0, titleList);
CellStyle cellStyleCenter = ExcelWriteUtil.createCellStyle(workbook, "宋体", 14, IndexedColors.BLACK.getIndex(), false, false, false, true, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyleCenter = ExcelWriteUtil.setBorderStyle(cellStyleCenter, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN);
String name = "";
String state = "";
Integer typeId = null;
List<AssetsRtn> list = assetDao.selectAssetList(name, state, typeId);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < list.size(); i++) {
Row row = ExcelWriteUtil.createRow(workbook, 0, i + 1);
AssetsRtn data = list.get(i);
ExcelWriteUtil.createCell(row, 0, ExcelWriteUtil.objToString(i + 1), cellStyleCenter);
ExcelWriteUtil.createCell(row, 1, ExcelWriteUtil.objToString(data.getName()), cellStyleCenter);
ExcelWriteUtil.createCell(row, 2, ExcelWriteUtil.objToString(data.getAmount()), cellStyleCenter);
ExcelWriteUtil.createCell(row, 3, ExcelWriteUtil.objToString(data.getUnitText()), cellStyleCenter);
ExcelWriteUtil.createCell(row, 4, ExcelWriteUtil.objToString(data.getPrice()), cellStyleCenter);
double price = data.getPrice().doubleValue();
double totalPrice = price * data.getAmount();
DecimalFormat df = new DecimalFormat("#.00");
ExcelWriteUtil.createCell(row, 5, ExcelWriteUtil.objToString(df.format(totalPrice)), cellStyleCenter);
ExcelWriteUtil.createCell(row, 6, ExcelWriteUtil.objToString(data.getSite()), cellStyleCenter);
ExcelWriteUtil.createCell(row, 7, ExcelWriteUtil.objToString(data.getTypeText()), cellStyleCenter);
ExcelWriteUtil.createCell(row, 8, ExcelWriteUtil.objToString(sf.format(data.getCreateTime())), cellStyleCenter);
ExcelWriteUtil.createCell(row, 9, ExcelWriteUtil.objToString(data.getMaxUseYears()), cellStyleCenter);
String stateText = "";
if (data.getState().equals("1")) {
stateText = "使用中";
} else if (data.getState().equals("2")) {
stateText = "闲置";
} else {
stateText = "报废";
}
ExcelWriteUtil.createCell(row, 10, ExcelWriteUtil.objToString(stateText), cellStyleCenter);
}
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < titleList.size(); i++) {
sheet.autoSizeColumn(i);
sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10);
}
writeResponseExcel(response, workbook, "资产列表");
}
protected void writeResponseExcel(HttpServletResponse response, XSSFWorkbook workbook, String excelName) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode(excelName + ".xlsx", "utf-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
workbook.close();
out.flush();
out.close();
}
6.模板下载示例
@ApiOperation(value = "模板下载")
@GetMapping("/excel/download/model")
public void downloadModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<String> titleList = new ArrayList<>();
titleList.add("资产名称");
titleList.add("资产数量");
titleList.add("计量单位");
titleList.add("单价(元)");
titleList.add("使用地点");
titleList.add("资产类别");
titleList.add("采购日期");
titleList.add("报废年限(年)");
titleList.add("使用情况");
XSSFWorkbook workbook = ExcelWriteUtil.createTemplate2007("资产列表模板", 0, titleList);
XSSFSheet sheet = workbook.getSheet("资产列表模板");
List<String> example = new ArrayList<>();
example.add("电脑");
example.add("5");
example.add("台");
example.add("888.00");
example.add("办公室");
example.add("办公用品");
example.add("2017-01-01 00:00:00");
example.add("10");
example.add("使用中");
ExcelWriteUtil.createExampleRow(workbook, "资产列表模板", example, 1);
CellStyle cellStyle = ExcelWriteUtil.createCellStyle(workbook, "宋体", 12, IndexedColors.BLACK.getIndex(), false, false, false, false, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
for (int i = 0; i < titleList.size(); i++) {
sheet.autoSizeColumn(i);
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("资产列表录入.xlsx", "utf-8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
ServletOutputStream out;
try {
out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
7.导入示例
@ApiOperation(value = "导入")
@PostMapping("/excel/upload")
public Object upload(MultipartFile file) {
Workbook workbook = ExcelReadUtil.getWorkbook(file);
if (workbook == null) {
return "请选择excel文件";
}
if (!ExcelReadUtil.valueTemplate(workbook, 0, 0, new String[]{
"资产名称", "资产数量", "计量单位", "单价(元)", "使用地点", "资产类别", "采购日期", "报废年限(年)", "使用情况"})) {
return "请选择正确的excel模板";
}
List<Map<String, String>> list = ExcelReadUtil.readData(workbook, 0, 2, 0, new String[]{
"name", "amount", "unitText", "price", "site", "typeText", "createTime", "maxUseYears", "state"});
List<BaseDictionary> typeList = dictionaryService.queryCategoryOfAssetType(1);
List<BaseDictionary> unitList = dictionaryService.queryCategoryOfAssetType(2);
List<Assets> dataList = new ArrayList<>();
List<String> errList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Assets data = new Assets();
Map<String, String> row = list.get(i);
if (CommonUtil.isEmpty(row.get("name"))) {
errList.add(String.format("第%d行名称为空", i + 1));
} else {
data.setName(row.get("name"));
}
if (CommonUtil.isEmpty(row.get("amount"))) {
errList.add(String.format("第%d行数量为空", i + 1));
} else if (!CommonUtil.isInteger(row.get("amount"))) {
errList.add(String.format("第%d行数量不是整数", i + 1));
} else {
data.setAmount(Integer.valueOf(row.get("amount")));
}
Integer unitId = getDicId(unitList, row.get("unitText"));
if (CommonUtil.isEmpty(row.get("unitText"))) {
errList.add(String.format("第%d行计量单位为空", i + 1));
} else if (unitId == null) {
errList.add(String.format("第%d行计量单位未定义", i + 1));
} else {
data.setUnitId(unitId);
}
if (CommonUtil.notEmpty(row.get("price")) && !CommonUtil.isNumber(row.get("price"))) {
errList.add(String.format("第%d行采购单价不是数字", i + 1));
} else if (CommonUtil.notEmpty(row.get("price"))) {
data.setPrice(new BigDecimal(row.get("price")));
}
if (CommonUtil.isEmpty(row.get("site"))) {
errList.add(String.format("第%d行使用地点为空", i + 1));
} else {
data.setSite(row.get("site"));
}
Integer typeId = getDicId(typeList, row.get("typeText"));
if (CommonUtil.isEmpty(row.get("typeText"))) {
errList.add(String.format("第%d行资产类别为空", i + 1));
} else if (typeId == null) {
errList.add(String.format("第%d行资产类别未定义", i + 1));
} else {
data.setTypeId(typeId);
}
Date date = CommonUtil.parseDate(row.get("createTime"));
if (CommonUtil.notEmpty(row.get("createTime")) && date == null) {
errList.add(String.format("第%d行采购日期格式不正确", i + 1));
} else if (CommonUtil.notEmpty(row.get("createTime"))) {
data.setCreateTime(date);
}
if (CommonUtil.notEmpty(row.get("maxUseYears")) && !CommonUtil.isInteger(row.get("maxUseYears"))) {
errList.add(String.format("第%d行使用年限不是整数", i + 1));
} else if (CommonUtil.notEmpty(row.get("maxUseYears"))) {
data.setMaxUseYears(Integer.valueOf(row.get("maxUseYears")));
}
if (CommonUtil.isEmpty(row.get("state"))) {
errList.add(String.format("第%d行使用状态为空", i + 1));
} else if (row.get("state").indexOf("使用中")>0) {
data.setState("1");
} else if(row.get("state").indexOf("闲置")>0){
data.setState("2");
}else {
data.setState("3");
}
dataList.add(data);
}
if (errList.size() > 0) {
return errList;
}
int count = 0;
for (Assets assets : dataList) {
if (assetDao.saveAsset(assets)>0) {
count++;
}
}
return count;
}
private Integer getDicId(List<BaseDictionary> list, String name) {
for (BaseDictionary dictionary : list) {
if (dictionary.getName().equals(name)) {
return dictionary.getId();
}
}
return null;
}