Browse Source

解决导出列数比较多时不够用的情况

wkeyuan 4 years ago
parent
commit
8e8f18e0ce

+ 11 - 5
pom.xml

@@ -108,11 +108,17 @@
 		<version>3.0-alpha-2</version>
 	</dependency>
 		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
-	<dependency>
-		<groupId>org.apache.poi</groupId>
-		<artifactId>poi</artifactId>
-		<version>5.0.0</version>
-	</dependency>
+		<dependency>
+			<groupId>org.apache.poi</groupId>
+			<artifactId>poi-ooxml</artifactId>
+			<version>5.0.0</version>
+		</dependency>
+		<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
+		<dependency>
+			<groupId>org.apache.poi</groupId>
+			<artifactId>poi-scratchpad</artifactId>
+			<version>5.0.0</version>
+		</dependency>
 
 		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
 	<dependency>

+ 176 - 0
src/main/java/com/key/common/utils/excel/XLSXExportUtil.java

@@ -0,0 +1,176 @@
+package com.key.common.utils.excel;
+
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.*;
+
+import java.io.*;
+import java.util.Calendar;
+
+/**
+ * 生成导出Excel文件对象
+ *
+ */
+public class XLSXExportUtil {
+
+	// 设置cell编码解决中文高位字节截断
+	// 定制日期格式
+	private static String DATE_FORMAT = " m/d/yy "; // "m/d/yy h:mm"
+	// 定制浮点数格式
+	private static String NUMBER_FORMAT = " #,##0.00 ";
+	private String xlsFileName;
+	private String path;
+	private XSSFWorkbook workbook;
+	private XSSFSheet sheet;
+	private XSSFRow row;
+	private XSSFCellStyle cellStyle;
+	private XSSFDataFormat dataFormat ;
+	/**
+	 * 初始化Excel
+	 *
+	 * @param fileName
+	 *            导出文件名
+	 */
+	public XLSXExportUtil(String fileName,String path) {
+		this.xlsFileName = fileName;
+		this.path=path;
+		this.workbook = new XSSFWorkbook();
+		this.sheet = workbook.createSheet();
+		this.cellStyle = workbook.createCellStyle(); // 建立新的cell样式;
+		this.dataFormat = workbook.createDataFormat();
+		//		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
+	}
+
+
+	/** */
+	/**
+	 * 导出Excel文件
+	 *
+	 */
+	public void exportXLS() throws Exception {
+		try {
+			File file=new File(path);
+			if(!file.exists()) {
+				file.mkdirs();
+			}
+			FileOutputStream fOut = new FileOutputStream(path+File.separator+xlsFileName);
+			workbook.write(fOut);
+			fOut.flush();
+			fOut.close();
+		} catch (FileNotFoundException e) {
+			throw new Exception(" 生成导出Excel文件出错! ", e);
+		} catch (IOException e) {
+			throw new Exception(" 写入Excel文件出错! ", e);
+		}
+
+	}
+
+	/** */
+	/**
+	 * 增加一行
+	 *
+	 * @param index
+	 *            行号
+	 */
+	public void createRow(int index) {
+		this.row = this.sheet.createRow(index);
+	}
+
+
+	public void setCellBlue(int index, String value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellType(CellType.STRING);
+//		CellStyle style = workbook.createCellStyle();
+//		Font font = workbook.createFont();
+//		font.setColor(IndexedColors.BLUE.getIndex());
+//		style.setFont(font);
+
+		cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIME.getIndex());
+		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+
+		cell.setCellStyle(cellStyle);
+		cell.setCellValue(value);
+	}
+
+	public void setCellYellow(int index, String value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellType(CellType.STRING);
+
+//		Font font = workbook.createFont();
+//		font.setColor(IndexedColors.BLUE.getIndex());
+//		style.setFont(font);
+
+		cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.YELLOW.getIndex());
+		cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+
+		cell.setCellStyle(cellStyle);
+		cell.setCellValue(value);
+	}
+
+	/** */
+	/**
+	 * 设置单元格
+	 *
+	 * @param index
+	 *            列号
+	 * @param value
+	 *            单元格填充值
+	 */
+	@SuppressWarnings("deprecation")
+	public void setCell(int index, String value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellType(CellType.STRING);
+		cell.setCellValue(value);
+	}
+
+	/** */
+	/**
+	 * 设置单元格
+	 *
+	 * @param index
+	 *            列号
+	 * @param value
+	 *            单元格填充值
+	 */
+	@SuppressWarnings("deprecation")
+	public void setCell(int index, Calendar value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellValue(value.getTime());
+		cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
+		cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
+	}
+
+	/** */
+	/**
+	 * 设置单元格
+	 *
+	 * @param index
+	 *            列号
+	 * @param value
+	 *            单元格填充值
+	 */
+	public void setCell(int index, int value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellType(CellType.NUMERIC);
+		cell.setCellValue(value);
+	}
+
+	/** */
+	/**
+	 * 设置单元格
+	 *
+	 * @param index
+	 *            列号
+	 * @param value
+	 *            单元格填充值
+	 */
+	public void setCell(int index, double value) {
+		XSSFCell cell = this.row.createCell((short) index);
+		cell.setCellType(CellType.NUMERIC);
+		cell.setCellValue(value);
+		cellStyle.setDataFormat(dataFormat.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
+		cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
+	}
+
+}

+ 6 - 8
src/main/java/com/key/dwsurvey/service/impl/SurveyAnswerManagerImpl.java

@@ -5,6 +5,7 @@ import java.text.SimpleDateFormat;
 import java.util.*;
 
 import com.key.common.QuType;
+import com.key.common.utils.excel.XLSXExportUtil;
 import com.key.common.utils.web.Struts2Utils;
 import com.key.dwsurvey.dao.SurveyAnswerDao;
 import com.key.dwsurvey.entity.*;
@@ -253,19 +254,16 @@ public class SurveyAnswerManagerImpl extends
 		String basepath = surveyId + "";
 		String urlPath = "/file/" + basepath + "/";// 下载所用的地址
 		String path = urlPath.replace("/", File.separator);// 文件系统路径
-		// File.separator +
-		// "file" +
-		// File.separator+basepath
-		// + File.separator;
 		savePath = savePath + path;
 		File file = new File(savePath);
 		if (!file.exists())
 			file.mkdirs();
 
 		SurveyDirectory surveyDirectory = directoryManager.getSurvey(surveyId);
-		String fileName = surveyId + "_exportSurvey.xls";
+		String fileName = surveyId + "_exportSurvey.xlsx";
 
-		XLSExportUtil exportUtil = new XLSExportUtil(fileName, savePath);
+//		XLSExportUtil exportUtil = new XLSExportUtil(fileName, savePath);
+		XLSXExportUtil exportUtil = new XLSXExportUtil(fileName, savePath);
 		Criterion cri1 = Restrictions.eq("surveyId",surveyId);
 		Page<SurveyAnswer> page = new Page<SurveyAnswer>();
 		page.setPageSize(5000);
@@ -290,7 +288,7 @@ public class SurveyAnswerManagerImpl extends
 		return urlPath + fileName;
 	}
 
-	private void exportXLSRow(XLSExportUtil exportUtil,String surveyAnswerId, List<Question> questions,SurveyAnswer surveyAnswer) {
+	private void exportXLSRow(XLSXExportUtil exportUtil,String surveyAnswerId, List<Question> questions,SurveyAnswer surveyAnswer) {
 		int cellIndex = 0;
 		for (Question question : questions) {
 
@@ -567,7 +565,7 @@ public class SurveyAnswerManagerImpl extends
 
 	}
 
-	private void exportXLSTitle(XLSExportUtil exportUtil,
+	private void exportXLSTitle(XLSXExportUtil exportUtil,
 								List<Question> questions) {
 		exportUtil.createRow(0);
 		int cellIndex = 0;