Browse Source

升级POI包

keyuan 4 years ago
parent
commit
b25d55f005

+ 1 - 1
pom.xml

@@ -99,7 +99,7 @@
 	<dependency>
 		<groupId>org.apache.poi</groupId>
 		<artifactId>poi</artifactId>
-		<version>3.7</version>
+		<version>4.1.0</version>
 	</dependency>
 
 		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

+ 10 - 6
src/main/java/com/key/common/utils/excel/POIWriteToExcel.java

@@ -12,10 +12,11 @@ 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.ss.usermodel.CellType;
 
 /**
  * 此类采用 poi包 实现了向 .xls 文件写入的功能, 目前为止 FastExcel 还没有提供向 .xls 文件写入的功能
- * 
+ *
  * @author hellojim
  * @company cxtech
  */
@@ -129,7 +130,8 @@ public class POIWriteToExcel {
 		// 把字体颜色设置为红色
 		font.setColor(HSSFFont.COLOR_NORMAL);
 		// 把字体设置为粗体
-		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+//		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+		font.setBold(true);
 		// 创建格式
 		HSSFCellStyle cellStyle = workbook.createCellStyle();
 		// 把创建的字体付加于格式
@@ -152,7 +154,7 @@ public class POIWriteToExcel {
 				}
 
 				// 设置此单元格中存入的是字符串
-				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+//				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 				// 设置编码 这个是用来处理中文问题的
 				// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
 				// 向此单元格中放入值
@@ -181,7 +183,8 @@ public class POIWriteToExcel {
 		// 把字体颜色设置为红色
 		font.setColor(HSSFFont.COLOR_NORMAL);
 		// 把字体设置为粗体
-		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+//		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+		font.setBold(true);
 		// 创建格式
 		HSSFCellStyle cellStyle = workbook1.createCellStyle();
 		// 把创建的字体付加于格式
@@ -204,7 +207,8 @@ public class POIWriteToExcel {
 				}
 
 				// 设置此单元格中存入的是字符串
-				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+//				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+				cell.setCellType(CellType.STRING);
 				// 设置编码 这个是用来处理中文问题的
 				// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
 				// 向此单元格中放入值
@@ -219,4 +223,4 @@ public class POIWriteToExcel {
 		fileOutputStream.close();
 
 	}
-}
+}

+ 14 - 10
src/main/java/com/key/common/utils/excel/ReadExcelUtil.java

@@ -10,6 +10,9 @@ 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.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.ss.usermodel.CellType;
+
+import static org.apache.poi.ss.usermodel.CellType.*;
 
 public class ReadExcelUtil {
 	public static HSSFWorkbook getWorkBook(String filePath){
@@ -41,7 +44,7 @@ public class ReadExcelUtil {
 		String msg = getCellStringValue(sfCell);
 		return msg;
 	}
-	
+
 	public static void reader(String filePath) {
 		try {
 			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
@@ -49,7 +52,7 @@ public class ReadExcelUtil {
 			HSSFSheet sheet = wb.getSheetAt(0);
 			HSSFRow row = sheet.getRow(3);
 			HSSFCell cell = row.getCell((short) 0);
-			int type = cell.getCellType();
+			CellType type = cell.getCellType();
 			String msg = getCellStringValue(cell);
 			System.out.println(type + ":" + msg);
 		} catch (IOException e) {
@@ -59,28 +62,29 @@ public class ReadExcelUtil {
 
 	public static String getCellStringValue(HSSFCell cell) {
 		String cellValue = "";
-		switch (cell.getCellType()) {
-		case HSSFCell.CELL_TYPE_STRING:
+		CellType cellType = cell.getCellType();
+		switch (cellType) {
+			case STRING:
 			cellValue = cell.getStringCellValue();
 			if (cellValue.trim().equals("") || cellValue.trim().length() <= 0) {
 				cellValue = " ";
 			}
 			break;
-		case HSSFCell.CELL_TYPE_NUMERIC:
+			case NUMERIC:
 			// cellValue = String.valueOf(cell.getNumericCellValue());
 			DecimalFormat formatter = new DecimalFormat("######");
 			cellValue = formatter.format(cell.getNumericCellValue());
 			break;
-		case HSSFCell.CELL_TYPE_FORMULA:
-			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+		case FORMULA:
+			cell.setCellType(NUMERIC);
 			cellValue = String.valueOf(cell.getNumericCellValue());
 			break;
-		case HSSFCell.CELL_TYPE_BLANK:
+			case BLANK:
 			cellValue = " ";
 			break;
-		case HSSFCell.CELL_TYPE_BOOLEAN:
+			case BOOLEAN:
 			break;
-		case HSSFCell.CELL_TYPE_ERROR:
+			case ERROR:
 			break;
 		default:
 			break;

+ 14 - 11
src/main/java/com/key/common/utils/excel/XLSExportUtil.java

@@ -11,6 +11,7 @@ import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 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.ss.usermodel.CellType;
 
 /**
  * 生成导出Excel文件对象
@@ -29,7 +30,7 @@ public class XLSExportUtil {
 	private HSSFRow row;
 	/**
 	 * 初始化Excel
-	 * 
+	 *
 	 * @param fileName
 	 *            导出文件名
 	 */
@@ -43,8 +44,7 @@ public class XLSExportUtil {
 	/** */
 	/**
 	 * 导出Excel文件
-	 * 
-	 * @throws XLSException
+	 *
 	 */
 	public void exportXLS() throws Exception {
 		try {
@@ -67,7 +67,7 @@ public class XLSExportUtil {
 	/** */
 	/**
 	 * 增加一行
-	 * 
+	 *
 	 * @param index
 	 *            行号
 	 */
@@ -78,7 +78,7 @@ public class XLSExportUtil {
 	/** */
 	/**
 	 * 设置单元格
-	 * 
+	 *
 	 * @param index
 	 *            列号
 	 * @param value
@@ -87,14 +87,15 @@ public class XLSExportUtil {
 	@SuppressWarnings("deprecation")
 	public void setCell(int index, String value) {
 		HSSFCell cell = this.row.createCell((short) index);
-		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+//		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
+		cell.setCellType(CellType.STRING);
 		cell.setCellValue(value);
 	}
 
 	/** */
 	/**
 	 * 设置单元格
-	 * 
+	 *
 	 * @param index
 	 *            列号
 	 * @param value
@@ -112,7 +113,7 @@ public class XLSExportUtil {
 	/** */
 	/**
 	 * 设置单元格
-	 * 
+	 *
 	 * @param index
 	 *            列号
 	 * @param value
@@ -120,14 +121,15 @@ public class XLSExportUtil {
 	 */
 	public void setCell(int index, int value) {
 		HSSFCell cell = this.row.createCell((short) index);
-		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+//		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+		cell.setCellType(CellType.NUMERIC);
 		cell.setCellValue(value);
 	}
 
 	/** */
 	/**
 	 * 设置单元格
-	 * 
+	 *
 	 * @param index
 	 *            列号
 	 * @param value
@@ -135,7 +137,8 @@ public class XLSExportUtil {
 	 */
 	public void setCell(int index, double value) {
 		HSSFCell cell = this.row.createCell((short) index);
-		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+//		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
+		cell.setCellType(CellType.NUMERIC);
 		cell.setCellValue(value);
 		HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
 		HSSFDataFormat format = workbook.createDataFormat();