+ * ClassName: ExportExcel + *
+ *+ * Description: 导出excel(2007+) + *
+ *+ * Author: Administrator + *
+ *+ * Date: 2016年3月23日 + *
+ */ +public class ExportExcel { + + /** + * 日志 + */ + private static Logger log = LoggerFactory.getLogger(ExportExcel.class); + + /** + * 字体 + */ + private static final String FONT_NAME = "Arial"; + + /** + *+ * Field DATA: 样式名称 + *
+ */ + private static final String STYLE_DATA = "data"; + + /** + * 工作薄对象 + */ + private SXSSFWorkbook wb; + + /** + * 工作表对象 + */ + private SXSSFSheet sheet; + + /** + * 样式列表 + */ + private Map+ * Description: 添加一个单元格 + *
+ * + * @param row 添加的行 + * @param column 添加列号 + * @param val 添加值 + * @param align 对齐方式(1:靠左;2:居中;3:靠右) + * @return 单元格 + */ + public Cell addCell(Row row, int column, Object val, int align) { + final int num3 = 3; + Cell cell = row.createCell(column); + CellStyle style = this.styles.get(STYLE_DATA + (align >= 1 && align <= num3 ? align : "")); + if (val == null) { + cell.setCellValue(""); + } else if (val instanceof String) { + cell.setCellValue((String) val); + } else if (val instanceof Integer) { + cell.setCellValue((Integer) val); + } else if (val instanceof Long) { + cell.setCellValue((Long) val); + } else if (val instanceof Double) { + cell.setCellValue((Double) val); + } else if (val instanceof Float) { + cell.setCellValue((Float) val); + } else if (val instanceof Date) { + DataFormat format = this.wb.createDataFormat(); + style.setDataFormat(format.getFormat("yyyy-MM-dd")); + cell.setCellValue((Date) val); + } else { + cell.setCellValue(val.toString()); + } + cell.setCellStyle(style); + return cell; + } + + /** + *+ * Description: 合并单元格,行列位置都是从0开始 + *
+ * + * @param firstRow 起始行 + * @param lastRow 结束行 + * @param firstCol 其实列 + * @param lastCol 结束列 + */ + public void mergeCell(int firstRow, int lastRow, int firstCol, int lastCol) { + this.sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); + } + + /** + * 输出到客户端 + * + * @param response 响应对象 + * @param fileName 文件名 + * @throws IOException 异常 + */ + public void write(HttpServletResponse response, String fileName) throws IOException { + OutputStream os = null; + try { + response.reset(); + response.setContentType("application/octet-stream; charset=utf-8"); + response.setHeader("Content-Disposition", "attachment; filename=" + urlEncode(fileName)); + os = response.getOutputStream(); + this.wb.write(os); + } finally { + if (os != null) { + os.flush(); + os.close(); + } + } + } + + /** + * 输出到文件 + * + * @param name 输出文件名 + * @throws IOException 异常 + */ + public void writeFile(String name) throws IOException { + FileOutputStream os = null; + try { //NOSONAR + os = new FileOutputStream(name); + this.wb.write(os); + } finally { + if (os != null) { + os.flush(); + os.close(); + } + } + } + + /** + * 清理临时文件 + * + * @throws IOException 异常 + */ + public void dispose() throws IOException { + this.wb.dispose(); + this.wb.close(); + } + + /** + * URL 编码, Encode默认为UTF-8. + * + * @param part 附件名称 + * @return 编码后的附件名称 + * @throws UnsupportedEncodingException 异常 + */ + private String urlEncode(String part) throws UnsupportedEncodingException { + return URLEncoder.encode(part, "UTF-8"); + } + +} diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ExportExcelHeaderMateData.java b/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ExportExcelHeaderMateData.java new file mode 100644 index 0000000..79c097f --- /dev/null +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ExportExcelHeaderMateData.java @@ -0,0 +1,34 @@ +package org.itkk.udf.core.utils.excel; + +import lombok.Data; + +/** + * Excel导出表头定义 + * + * @author Administrator + */ +@Data +public class ExportExcelHeaderMateData { + + /** + * 表头名称 + */ + private String headerName; + + /** + * 表头列宽(算法:最大字数) + */ + private int headerWidth; + + /** + * 构造函数 + * + * @param headerName 表头名称 + * @param headerWidth 表头列宽(算法:最大字数) + */ + public ExportExcelHeaderMateData(String headerName, int headerWidth) { + this.headerName = headerName; + this.headerWidth = headerWidth; + } + +} diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ImportExcel.java b/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ImportExcel.java new file mode 100644 index 0000000..21612be --- /dev/null +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/utils/excel/ImportExcel.java @@ -0,0 +1,209 @@ +/** + * ImportExcel.java + * Created at 2016-03-01 + * Created by Administrator + * Copyright (C) 2016 LLSFW, All rights reserved. + */ + +package org.itkk.udf.core.utils.excel; + +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +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.xssf.usermodel.XSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * 导入excel(2003+) + * + * @author Administrator + */ +public class ImportExcel { + + /** + * 日志 + */ + private static final Logger LOG = LoggerFactory.getLogger(ImportExcel.class); + + /** + * 工作薄对象 + */ + private Workbook wb; + + /** + * 工作表对象 + */ + private Sheet sheet; + + /** + * 标题行号 + */ + private int headerNum; + + /** + * 构造函数 + * + * @param fileName 文件名称(完整路径) + * @param headerNum 标题行号,数据行号=标题行号+1 + * @throws IOException 异常 + */ + public ImportExcel(String fileName, int headerNum) throws IOException { + this(new File(fileName), headerNum); + } + + /** + * 构造函数 + * + * @param file 文件对象 + * @param headerNum 标题行号,数据行号=标题行号+1 + * @throws IOException 异常 + */ + public ImportExcel(File file, int headerNum) throws IOException { + this(file, headerNum, 0); + } + + /** + * 构造函数 + * + * @param fileName 文件名称(完整路径) + * @param headerNum 标题行号,数据行号=标题行号+1 + * @param sheetIndex 工作表编号 + * @throws IOException 异常 + */ + public ImportExcel(String fileName, int headerNum, int sheetIndex) throws IOException { + this(new File(fileName), headerNum, sheetIndex); + } + + /** + * 构造函数 + * + * @param file 导入文件对象 + * @param headerNum 标题行号,数据行号=标题行号+1 + * @param sheetIndex 工作表编号 + * @throws IOException 异常 + */ + public ImportExcel(File file, int headerNum, int sheetIndex) throws IOException { + this(file.getName(), new FileInputStream(file), headerNum, sheetIndex); + } + + /** + * 构造函数 + * + * @param multipartFile 上传文件对象 + * @param headerNum 标题行号,数据行号=标题行号+1 + * @param sheetIndex 工作表编号 + * @throws IOException 异常 + */ + public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex) throws IOException { + this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex); + } + + /** + * 构造函数 + * + * @param fileName 文件名称(完整路径) + * @param is 输入流 + * @param headerNum 标题行号,数据行号=标题行号+1 + * @param sheetIndex 工作表编号 + * @throws IOException 异常 + */ + public ImportExcel(String fileName, InputStream is, int headerNum, int sheetIndex) throws IOException { + if (StringUtils.isBlank(fileName)) { + throw new IOException("导入文档为空!"); + } else if (fileName.toLowerCase().endsWith("xls")) { + this.wb = new HSSFWorkbook(is); + } else if (fileName.toLowerCase().endsWith("xlsx")) { + this.wb = new XSSFWorkbook(is); + } else { + throw new IOException("文档格式不正确!"); + } + if (this.wb.getNumberOfSheets() < sheetIndex) { + throw new IOException("文档中没有工作表!"); + } + this.sheet = this.wb.getSheetAt(sheetIndex); + this.headerNum = headerNum; + LOG.debug("Initialize success."); + } + + /** + * 获取行对象 + * + * @param rownum 行号 + * @return 行 + */ + public Row getRow(int rownum) { + return this.sheet.getRow(rownum); + } + + /** + * 获取数据行号 + * + * @return 行号 + */ + public int getDataRowNum() { + return this.headerNum + 1; + } + + /** + * 获取最后一个数据行号 + * + * @return 行号 + */ + public int getLastDataRowNum() { + return this.sheet.getLastRowNum() + this.headerNum; + } + + /** + * 获取最后一个列号 + * + * @return 列号 + */ + public int getLastCellNum() { + return this.getRow(this.headerNum).getLastCellNum(); + } + + /** + * 获取单元格值 + * + * @param row 获取的行 + * @param column 获取单元格列号 + * @return 单元格值 + */ + public Object getCellValue(Row row, int column) { + Object val = ""; + Cell cell = row.getCell(column); + if (cell != null) { + switch (cell.getCellTypeEnum()) { + case NUMERIC: + val = cell.getNumericCellValue(); + break; + case STRING: + val = cell.getStringCellValue(); + break; + case FORMULA: + val = cell.getCellFormula(); + break; + case BOOLEAN: + val = cell.getBooleanCellValue(); + break; + case ERROR: + val = cell.getErrorCellValue(); + break; + default: + val = null; + break; + } + } + return val; + } +} diff --git a/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java b/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java index 08ad482..2bd7f3e 100644 --- a/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java +++ b/udf-starter-id/src/main/test/java/org/itkk/udf/id/IdWorkerTest.java @@ -1,7 +1,8 @@ -package org.itkk.udf.id; +package java.org.itkk.udf.id; import lombok.extern.slf4j.Slf4j; import org.itkk.udf.core.constant.IdWorkerConstant; +import org.itkk.udf.id.IdWorker; import org.junit.Test; import java.text.SimpleDateFormat;