|
@@ -1,339 +1,340 @@
|
|
|
package cn.com.qmth.examcloud.support.excel;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
+import cn.com.qmth.examcloud.commons.util.UUID;
|
|
|
+import cn.com.qmth.examcloud.support.util.FileDisposeUtil;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.lang.StringEscapeUtils;
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.apache.commons.lang.StringEscapeUtils;
|
|
|
-
|
|
|
-import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
-import cn.com.qmth.examcloud.commons.util.UUID;
|
|
|
-import cn.com.qmth.examcloud.support.util.FileDisposeUtil;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/*
|
|
|
* excel导出工具
|
|
|
*/
|
|
|
public class ExcelExportUtil {
|
|
|
-
|
|
|
|
|
|
- private static final String TEMP_FILE_EXP = "excelExport/";
|
|
|
-
|
|
|
- private static final String DEFALUT_CONTENT_TYPE = "application/vnd.ms-excel";
|
|
|
|
|
|
- private static final String DEFALUT_EXT = ".xlsx";
|
|
|
+ private static final String TEMP_FILE_EXP = "excelExport/";
|
|
|
|
|
|
- private static final String ZIP_SUFFIX = ".zip";
|
|
|
+ private static final String DEFALUT_CONTENT_TYPE = "application/vnd.ms-excel";
|
|
|
|
|
|
+ private static final String DEFALUT_EXT = ".xlsx";
|
|
|
|
|
|
- public static void exportExcel(String fileName, Class<?> dataClass, List<?> dataset,
|
|
|
- HttpServletResponse response) throws Exception {
|
|
|
- File directory = new File(TEMP_FILE_EXP + File.separator + UUID.randomUUID());
|
|
|
- ServletOutputStream outputStream = null;
|
|
|
- InputStream in=null;
|
|
|
- try {
|
|
|
- response.setHeader("Content-Disposition",
|
|
|
- "inline;filename=" + URLEncoder.encode(fileName, "UTF-8") + DEFALUT_EXT);
|
|
|
- response.setContentType(DEFALUT_CONTENT_TYPE);
|
|
|
- outputStream=response.getOutputStream();
|
|
|
- File file=createExcelFile(UUID.randomUUID(), dataClass, dataset, directory);
|
|
|
- in=new FileInputStream(file);
|
|
|
- int len=0;
|
|
|
- byte[] buffer=new byte[1024];
|
|
|
- while((len=in.read(buffer))>0){
|
|
|
- outputStream.write(buffer,0,len);
|
|
|
- }
|
|
|
- }finally {
|
|
|
- if(in!=null)in.close();
|
|
|
- if(outputStream!=null)outputStream.close();
|
|
|
- FileUtils.deleteDirectory(directory);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void exportExcel(Class<?> dataClass, List<?> dataset,OutputStream outputStream) throws Exception {
|
|
|
- File directory = new File(TEMP_FILE_EXP + File.separator + UUID.randomUUID());
|
|
|
- InputStream in=null;
|
|
|
- try {
|
|
|
- File file=createExcelFile(UUID.randomUUID(), dataClass, dataset, directory);
|
|
|
- in=new FileInputStream(file);
|
|
|
- int len=0;
|
|
|
- byte[] buffer=new byte[1024];
|
|
|
- while((len=in.read(buffer))>0){
|
|
|
- outputStream.write(buffer,0,len);
|
|
|
- }
|
|
|
- }finally {
|
|
|
- if(in!=null)in.close();
|
|
|
- if(outputStream!=null)outputStream.close();
|
|
|
- FileUtils.deleteDirectory(directory);
|
|
|
- }
|
|
|
- }
|
|
|
+ private static final String ZIP_SUFFIX = ".zip";
|
|
|
|
|
|
|
|
|
- private static File createExcelFile(String fileName,Class<?> dataClass, List<?> dataset, File directory) throws Exception{
|
|
|
- File excelTargetDir = new File(directory.getAbsolutePath() + "/excel/");
|
|
|
- File exceloriginalZip=new File(directory.getAbsolutePath() + "/excel/exceloriginal.zip");
|
|
|
- File excelfile=null;
|
|
|
- try {
|
|
|
- excelTargetDir.mkdirs();
|
|
|
- exceloriginalZip.createNewFile();
|
|
|
- InputStream is=ExcelExportUtil.class.getClass().getResourceAsStream("/exceloriginal/exceloriginal.zip");
|
|
|
- FileUtils.copyInputStreamToFile(is, exceloriginalZip);
|
|
|
- FileDisposeUtil.unZip(excelTargetDir, exceloriginalZip);
|
|
|
- exceloriginalZip.delete();
|
|
|
- dispose(excelTargetDir, dataClass,dataset);
|
|
|
- File zipfile = new File(
|
|
|
- excelTargetDir.getParentFile().getAbsolutePath() + File.separator + fileName + ZIP_SUFFIX);
|
|
|
- FileDisposeUtil.createZip(excelTargetDir.getAbsolutePath(), zipfile.getAbsolutePath());
|
|
|
- excelfile = new File(
|
|
|
- excelTargetDir.getParentFile().getAbsolutePath() + File.separator + fileName + DEFALUT_EXT);
|
|
|
- zipfile.renameTo(excelfile);
|
|
|
- }finally {
|
|
|
-// try {
|
|
|
-// FileUtils.deleteDirectory(excelTargetDir);
|
|
|
-// } catch (IOException e) {
|
|
|
-// }
|
|
|
- }
|
|
|
- return excelfile;
|
|
|
+ public static void exportExcel(String fileName, Class<?> dataClass, List<?> dataset,
|
|
|
+ HttpServletResponse response) throws Exception {
|
|
|
+ File directory = new File(TEMP_FILE_EXP + File.separator + UUID.randomUUID());
|
|
|
+ ServletOutputStream outputStream = null;
|
|
|
+ InputStream in = null;
|
|
|
+ try {
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
+ "inline;filename=" + URLEncoder.encode(fileName, "UTF-8") + DEFALUT_EXT);
|
|
|
+ response.setContentType(DEFALUT_CONTENT_TYPE);
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ File file = createExcelFile(UUID.randomUUID(), dataClass, dataset, directory);
|
|
|
+ in = new FileInputStream(file);
|
|
|
+ int len = 0;
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ while ((len = in.read(buffer)) > 0) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (in != null) in.close();
|
|
|
+ if (outputStream != null) outputStream.close();
|
|
|
+ FileUtils.deleteDirectory(directory);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ public static void exportExcel(Class<?> dataClass, List<?> dataset, OutputStream outputStream) throws Exception {
|
|
|
+ File directory = new File(TEMP_FILE_EXP + File.separator + UUID.randomUUID());
|
|
|
+ InputStream in = null;
|
|
|
+ try {
|
|
|
+ File file = createExcelFile(UUID.randomUUID(), dataClass, dataset, directory);
|
|
|
+ in = new FileInputStream(file);
|
|
|
+ int len = 0;
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ while ((len = in.read(buffer)) > 0) {
|
|
|
+ outputStream.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ if (in != null) in.close();
|
|
|
+ if (outputStream != null) outputStream.close();
|
|
|
+ FileUtils.deleteDirectory(directory);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- private static void dispose(File excelTargetDir, Class<?> dataClass, List<?> dataset)
|
|
|
- throws IOException, NoSuchMethodException, SecurityException,
|
|
|
- IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
- //excel列名A B C AA AB
|
|
|
- List<String> colName=new ArrayList<String>();
|
|
|
- //单元格值
|
|
|
- Map<String,Integer> vmap=new LinkedHashMap<String, Integer>();
|
|
|
- int vmapSize=0;
|
|
|
- ExcelSheet sheet=new ExcelSheet();
|
|
|
- int index = 1;//第一行开始
|
|
|
- //处理头
|
|
|
- List<ColumnSetting> columnSettings = getColumnSettings(dataClass);
|
|
|
- ExcelRow headrow=new ExcelRow();
|
|
|
- headrow.setR(String.valueOf(index));
|
|
|
- for(int i=0;i<columnSettings.size();i++) {
|
|
|
- colName.add(getExcelColumnName(i));
|
|
|
- ExcelCell cell=new ExcelCell();
|
|
|
- cell.setR(colName.get(i)+index);
|
|
|
- cell.setS("1");
|
|
|
- Integer cellV=vmap.get(columnSettings.get(i).getHeader());
|
|
|
- if(cellV==null) {
|
|
|
- vmapSize++;
|
|
|
- cellV=vmapSize;
|
|
|
- vmap.put(columnSettings.get(i).getHeader(), cellV);
|
|
|
- }
|
|
|
- cell.setV(cellV.toString());
|
|
|
- headrow.addCell(cell);
|
|
|
- }
|
|
|
- colName.add(getExcelColumnName(colName.size()));
|
|
|
- sheet.addRow(headrow);
|
|
|
- sheet.setDimension(colName.get(0)+1+":"+colName.get(colName.size()-1)+(dataset.size()+1));
|
|
|
- File sheetFile=writeSheetHead(excelTargetDir, sheet, columnSettings);
|
|
|
- //处理数据
|
|
|
- for(int k=0;k<dataset.size();k++) {
|
|
|
- index++;
|
|
|
- Object obj=dataset.get(k);
|
|
|
- ExcelRow row=new ExcelRow();
|
|
|
+
|
|
|
+ private static File createExcelFile(String fileName, Class<?> dataClass, List<?> dataset, File directory) throws Exception {
|
|
|
+ File excelTargetDir = new File(directory.getAbsolutePath() + "/excel/");
|
|
|
+ File exceloriginalZip = new File(directory.getAbsolutePath() + "/excel/exceloriginal.zip");
|
|
|
+ File excelfile = null;
|
|
|
+ try {
|
|
|
+ excelTargetDir.mkdirs();
|
|
|
+ exceloriginalZip.createNewFile();
|
|
|
+ InputStream is = ExcelExportUtil.class.getClass().getResourceAsStream("/exceloriginal/exceloriginal.zip");
|
|
|
+ FileUtils.copyInputStreamToFile(is, exceloriginalZip);
|
|
|
+ FileDisposeUtil.unZip(excelTargetDir, exceloriginalZip);
|
|
|
+ exceloriginalZip.delete();
|
|
|
+ dispose(excelTargetDir, dataClass, dataset);
|
|
|
+ File zipfile = new File(
|
|
|
+ excelTargetDir.getParentFile().getAbsolutePath() + File.separator + fileName + ZIP_SUFFIX);
|
|
|
+ FileDisposeUtil.createZip(excelTargetDir.getAbsolutePath(), zipfile.getAbsolutePath());
|
|
|
+ excelfile = new File(
|
|
|
+ excelTargetDir.getParentFile().getAbsolutePath() + File.separator + fileName + DEFALUT_EXT);
|
|
|
+ zipfile.renameTo(excelfile);
|
|
|
+ } finally {
|
|
|
+ // try {
|
|
|
+ // FileUtils.deleteDirectory(excelTargetDir);
|
|
|
+ // } catch (IOException e) {
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ return excelfile;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void dispose(File excelTargetDir, Class<?> dataClass, List<?> dataset)
|
|
|
+ throws IOException, NoSuchMethodException, SecurityException,
|
|
|
+ IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
|
|
+ //excel列名A B C AA AB
|
|
|
+ List<String> colName = new ArrayList<String>();
|
|
|
+ //单元格值
|
|
|
+ Map<String, Integer> vmap = new LinkedHashMap<String, Integer>();
|
|
|
+ int vmapSize = 0;
|
|
|
+ ExcelSheet sheet = new ExcelSheet();
|
|
|
+ int index = 1;//第一行开始
|
|
|
+ //处理头
|
|
|
+ List<ColumnSetting> columnSettings = getColumnSettings(dataClass);
|
|
|
+ ExcelRow headrow = new ExcelRow();
|
|
|
+ headrow.setR(String.valueOf(index));
|
|
|
+ for (int i = 0; i < columnSettings.size(); i++) {
|
|
|
+ colName.add(getExcelColumnName(i));
|
|
|
+ ExcelCell cell = new ExcelCell();
|
|
|
+ cell.setR(colName.get(i) + index);
|
|
|
+ cell.setS("1");
|
|
|
+ Integer cellV = vmap.get(columnSettings.get(i).getHeader());
|
|
|
+ if (cellV == null) {
|
|
|
+ vmapSize++;
|
|
|
+ cellV = vmapSize;
|
|
|
+ vmap.put(columnSettings.get(i).getHeader(), cellV);
|
|
|
+ }
|
|
|
+ cell.setV(cellV.toString());
|
|
|
+ headrow.addCell(cell);
|
|
|
+ }
|
|
|
+ colName.add(getExcelColumnName(colName.size()));
|
|
|
+ sheet.addRow(headrow);
|
|
|
+ sheet.setDimension(colName.get(0) + 1 + ":" + colName.get(colName.size() - 1) + (dataset.size() + 1));
|
|
|
+ File sheetFile = writeSheetHead(excelTargetDir, sheet, columnSettings);
|
|
|
+ //处理数据
|
|
|
+ for (int k = 0; k < dataset.size(); k++) {
|
|
|
+ index++;
|
|
|
+ Object obj = dataset.get(k);
|
|
|
+ ExcelRow row = new ExcelRow();
|
|
|
row.setR(String.valueOf(index));
|
|
|
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
|
|
|
for (short i = 0; i < columnSettings.size(); i++) {
|
|
|
String methodName = columnSettings.get(i).getGetMethodName();
|
|
|
Method method = dataClass.getMethod(methodName, new Class[]{});
|
|
|
- Object value = method.invoke(obj, new Object[] {});
|
|
|
- ExcelCell cell=new ExcelCell();
|
|
|
- cell.setR(colName.get(i)+index);
|
|
|
+ Object value = method.invoke(obj, new Object[]{});
|
|
|
+ ExcelCell cell = new ExcelCell();
|
|
|
+ cell.setR(colName.get(i) + index);
|
|
|
cell.setS("0");
|
|
|
- if(value!=null) {
|
|
|
- String encodeValue=StringEscapeUtils.escapeXml(value.toString());
|
|
|
- Integer cellV=vmap.get(encodeValue);
|
|
|
- if(cellV==null) {
|
|
|
- vmapSize++;
|
|
|
- cellV=vmapSize;
|
|
|
- vmap.put(encodeValue, cellV);
|
|
|
- }
|
|
|
- cell.setV(cellV.toString());
|
|
|
- }else {
|
|
|
- cell.setV(String.valueOf(0));
|
|
|
+ if (value != null) {
|
|
|
+ String encodeValue = StringEscapeUtils.escapeXml(value.toString());
|
|
|
+ Integer cellV = vmap.get(encodeValue);
|
|
|
+ if (cellV == null) {
|
|
|
+ vmapSize++;
|
|
|
+ cellV = vmapSize;
|
|
|
+ vmap.put(encodeValue, cellV);
|
|
|
+ }
|
|
|
+ cell.setV(cellV.toString());
|
|
|
+ } else {
|
|
|
+ cell.setV(String.valueOf(0));
|
|
|
}
|
|
|
row.addCell(cell);
|
|
|
}
|
|
|
sheet.addRow(row);
|
|
|
//早点回收内存
|
|
|
dataset.set(k, null);
|
|
|
- if(sheet.getRowCount()>=10000) {
|
|
|
- writeSheetBody(sheetFile, sheet);
|
|
|
- sheet.clearRows();
|
|
|
+ if (sheet.getRowCount() >= 10000) {
|
|
|
+ writeSheetBody(sheetFile, sheet);
|
|
|
+ sheet.clearRows();
|
|
|
}
|
|
|
}
|
|
|
- if(sheet.getRowCount()>0) {
|
|
|
- writeSheetBody(sheetFile, sheet);
|
|
|
- sheet.clearRows();
|
|
|
+ if (sheet.getRowCount() > 0) {
|
|
|
+ writeSheetBody(sheetFile, sheet);
|
|
|
+ sheet.clearRows();
|
|
|
}
|
|
|
- writeSheetTail(sheetFile);
|
|
|
-
|
|
|
- writeSharedStrings(excelTargetDir, vmap,index*(colName.size()-1));
|
|
|
- vmap=null;
|
|
|
- }
|
|
|
+ writeSheetTail(sheetFile);
|
|
|
+
|
|
|
+ writeSharedStrings(excelTargetDir, vmap, index * (colName.size() - 1));
|
|
|
+ vmap = null;
|
|
|
+ }
|
|
|
|
|
|
- private static void writeSharedStrings(File excelTargetDir, Map<String,Integer> vmap,Integer count) throws IOException {
|
|
|
- File file = new File(excelTargetDir.getAbsolutePath() + "/xl/sharedStrings.xml");
|
|
|
- FileOutputStream outputStream = null;
|
|
|
+ private static void writeSharedStrings(File excelTargetDir, Map<String, Integer> vmap, Integer count) throws IOException {
|
|
|
+ File file = new File(excelTargetDir.getAbsolutePath() + "/xl/sharedStrings.xml");
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
try {
|
|
|
file.createNewFile();//创建文件
|
|
|
- outputStream = new FileOutputStream(file,true);
|
|
|
+ outputStream = new FileOutputStream(file, true);
|
|
|
String data1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
outputStream.write(data1.getBytes("utf-8"));
|
|
|
- String data2 = "<sst count=\""+count+"\" uniqueCount=\""+(vmap.size()+1)+"\"\n";
|
|
|
+ String data2 = "<sst count=\"" + count + "\" uniqueCount=\"" + (vmap.size() + 1) + "\"\n";
|
|
|
outputStream.write(data2.getBytes("utf-8"));
|
|
|
- String data3="xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><si><t/></si>\n";
|
|
|
+ String data3 = "xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"><si><t/></si>\n";
|
|
|
outputStream.write(data3.getBytes("utf-8"));
|
|
|
- for(String k:vmap.keySet()) {
|
|
|
- String data4="<si><t>"+k+"</t></si>\n";
|
|
|
- outputStream.write(data4.getBytes("utf-8"));
|
|
|
+ for (String k : vmap.keySet()) {
|
|
|
+ String data4 = "<si><t>" + k + "</t></si>\n";
|
|
|
+ outputStream.write(data4.getBytes("utf-8"));
|
|
|
}
|
|
|
- String data5="</sst>";
|
|
|
- outputStream.write(data5.getBytes("utf-8"));
|
|
|
- outputStream.flush();
|
|
|
+ String data5 = "</sst>";
|
|
|
+ outputStream.write(data5.getBytes("utf-8"));
|
|
|
+ outputStream.flush();
|
|
|
} catch (Exception e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
} finally {
|
|
|
- outputStream.close();
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- private static File writeSheetHead(File excelTargetDir, ExcelSheet sheet,List<ColumnSetting> columnSettings) throws IOException {
|
|
|
- File file = new File(excelTargetDir.getAbsolutePath() + "/xl/worksheets/sheet1.xml");
|
|
|
- FileOutputStream outputStream = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static File writeSheetHead(File excelTargetDir, ExcelSheet sheet, List<ColumnSetting> columnSettings) throws IOException {
|
|
|
+ File file = new File(excelTargetDir.getAbsolutePath() + "/xl/worksheets/sheet1.xml");
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
try {
|
|
|
- outputStream = new FileOutputStream(file,true);
|
|
|
+ outputStream = new FileOutputStream(file, true);
|
|
|
String data1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
|
|
outputStream.write(data1.getBytes("utf-8"));
|
|
|
String data2 = "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">\n";
|
|
|
outputStream.write(data2.getBytes("utf-8"));
|
|
|
- String data3="<dimension ref=\""+sheet.getDimension()+"\"/>\n";
|
|
|
+ String data3 = "<dimension ref=\"" + sheet.getDimension() + "\"/>\n";
|
|
|
outputStream.write(data3.getBytes("utf-8"));
|
|
|
- String data4="<sheetViews><sheetView workbookViewId=\"0\" tabSelected=\"true\"/></sheetViews><sheetFormatPr defaultRowHeight=\"15.0\" baseColWidth=\"15\"/><cols>\n";
|
|
|
+ String data4 = "<sheetViews><sheetView workbookViewId=\"0\" tabSelected=\"true\"/></sheetViews><sheetFormatPr defaultRowHeight=\"15.0\" baseColWidth=\"15\"/><cols>\n";
|
|
|
outputStream.write(data4.getBytes("utf-8"));
|
|
|
- int index=0;
|
|
|
- for(ColumnSetting col:columnSettings) {
|
|
|
- index++;
|
|
|
- if(col.getWidth()!=0) {
|
|
|
- String data10 = "<col min=\""+index+"\" max=\""+index+"\" width=\""+col.getWidth()+"\" customWidth=\"true\"/>\n";
|
|
|
+ int index = 0;
|
|
|
+ for (ColumnSetting col : columnSettings) {
|
|
|
+ index++;
|
|
|
+ if (col.getWidth() != 0) {
|
|
|
+ String data10 = "<col min=\"" + index + "\" max=\"" + index + "\" width=\"" + col.getWidth() + "\" customWidth=\"true\"/>\n";
|
|
|
+ outputStream.write(data10.getBytes("utf-8"));
|
|
|
+ } else {
|
|
|
+ String data10 = "<col min=\"" + index + "\" max=\"" + index + "\" width=\"" + 15 + "\" customWidth=\"true\"/>\n";
|
|
|
outputStream.write(data10.getBytes("utf-8"));
|
|
|
- }else {
|
|
|
- String data10 = "<col min=\""+index+"\" max=\""+index+"\" width=\""+15+"\" customWidth=\"true\"/>\n";
|
|
|
- outputStream.write(data10.getBytes("utf-8"));
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
String data11 = "</cols><sheetData>\n";
|
|
|
outputStream.write(data11.getBytes("utf-8"));
|
|
|
- outputStream.flush();
|
|
|
- return file;
|
|
|
+ outputStream.flush();
|
|
|
+ return file;
|
|
|
} catch (Exception e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
} finally {
|
|
|
- outputStream.close();
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- private static void writeSheetBody(File file, ExcelSheet sheet) throws IOException {
|
|
|
- FileOutputStream outputStream = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void writeSheetBody(File file, ExcelSheet sheet) throws IOException {
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
try {
|
|
|
- outputStream = new FileOutputStream(file,true);
|
|
|
- for(ExcelRow row:sheet.getRows()) {
|
|
|
- String data5="<row r=\""+row.getR()+"\">\n";
|
|
|
- outputStream.write(data5.getBytes("utf-8"));
|
|
|
- for(ExcelCell cell:row.getCells()) {
|
|
|
- String data6="<c r=\""+cell.getR()+"\" s=\""+cell.getS()+"\" t=\"s\"><v>"+cell.getV()+"</v></c>\n";
|
|
|
- outputStream.write(data6.getBytes("utf-8"));
|
|
|
- }
|
|
|
- String data7="</row>\n";
|
|
|
- outputStream.write(data7.getBytes("utf-8"));
|
|
|
- row=null;
|
|
|
+ outputStream = new FileOutputStream(file, true);
|
|
|
+ for (ExcelRow row : sheet.getRows()) {
|
|
|
+ String data5 = "<row r=\"" + row.getR() + "\">\n";
|
|
|
+ outputStream.write(data5.getBytes("utf-8"));
|
|
|
+ for (ExcelCell cell : row.getCells()) {
|
|
|
+ String data6 = "<c r=\"" + cell.getR() + "\" s=\"" + cell.getS() + "\" t=\"s\"><v>" + cell.getV() + "</v></c>\n";
|
|
|
+ outputStream.write(data6.getBytes("utf-8"));
|
|
|
+ }
|
|
|
+ String data7 = "</row>\n";
|
|
|
+ outputStream.write(data7.getBytes("utf-8"));
|
|
|
+ row = null;
|
|
|
}
|
|
|
- outputStream.flush();
|
|
|
+ outputStream.flush();
|
|
|
} catch (Exception e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
} finally {
|
|
|
- outputStream.close();
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- private static void writeSheetTail(File file) throws IOException {
|
|
|
- FileOutputStream outputStream = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void writeSheetTail(File file) throws IOException {
|
|
|
+ FileOutputStream outputStream = null;
|
|
|
try {
|
|
|
- outputStream = new FileOutputStream(file,true);
|
|
|
- String data7="</sheetData><pageMargins bottom=\"0.75\" footer=\"0.3\" header=\"0.3\" left=\"0.7\" right=\"0.7\" top=\"0.75\"/></worksheet>";
|
|
|
- outputStream.write(data7.getBytes("utf-8"));
|
|
|
- outputStream.flush();
|
|
|
+ outputStream = new FileOutputStream(file, true);
|
|
|
+ String data7 = "</sheetData><pageMargins bottom=\"0.75\" footer=\"0.3\" header=\"0.3\" left=\"0.7\" right=\"0.7\" top=\"0.75\"/></worksheet>";
|
|
|
+ outputStream.write(data7.getBytes("utf-8"));
|
|
|
+ outputStream.flush();
|
|
|
} catch (Exception e) {
|
|
|
- throw new ExamCloudRuntimeException(e);
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
} finally {
|
|
|
- outputStream.close();
|
|
|
+ if (outputStream != null) {
|
|
|
+ outputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 提取ExcelProperty注解类的字段信息
|
|
|
+ *
|
|
|
* @param dataClass 需要解析 写入excel的数据类型
|
|
|
* @return
|
|
|
*/
|
|
|
- private static List<ColumnSetting> getColumnSettings(Class<?> dataClass){
|
|
|
+ private static List<ColumnSetting> getColumnSettings(Class<?> dataClass) {
|
|
|
List<ColumnSetting> columnSettings = new ArrayList<>();
|
|
|
//先在方法上找ExcelProperty注解
|
|
|
Method[] methods = dataClass.getDeclaredMethods();
|
|
|
- for(Method method : methods){
|
|
|
+ for (Method method : methods) {
|
|
|
ExcelProperty exportProperty = method.getAnnotation(ExcelProperty.class);
|
|
|
- if(exportProperty != null && exportProperty.name().trim().length() > 0){
|
|
|
- ColumnSetting columnSetting = new ColumnSetting(StringEscapeUtils.escapeXml(exportProperty.name()),method.getName(),
|
|
|
- exportProperty.width(),exportProperty.index());
|
|
|
+ if (exportProperty != null && exportProperty.name().trim().length() > 0) {
|
|
|
+ ColumnSetting columnSetting = new ColumnSetting(StringEscapeUtils.escapeXml(exportProperty.name()), method.getName(),
|
|
|
+ exportProperty.width(), exportProperty.index());
|
|
|
columnSettings.add(columnSetting);
|
|
|
}
|
|
|
}
|
|
|
//如果方法上找不到注解,再到属性上找
|
|
|
- if(columnSettings.size() == 0){
|
|
|
- Field[] fields = dataClass.getDeclaredFields();
|
|
|
- for(Field field:fields){
|
|
|
- ExcelProperty exportProperty = field.getAnnotation(ExcelProperty.class);
|
|
|
- if(exportProperty != null && exportProperty.name().trim().length() > 0){
|
|
|
- ColumnSetting columnSetting = new ColumnSetting(StringEscapeUtils.escapeXml(exportProperty.name()),"get"+toUpperCaseFirstOne(field.getName()),
|
|
|
- exportProperty.width(),exportProperty.index());
|
|
|
+ if (columnSettings.size() == 0) {
|
|
|
+ Field[] fields = dataClass.getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ ExcelProperty exportProperty = field.getAnnotation(ExcelProperty.class);
|
|
|
+ if (exportProperty != null && exportProperty.name().trim().length() > 0) {
|
|
|
+ ColumnSetting columnSetting = new ColumnSetting(StringEscapeUtils.escapeXml(exportProperty.name()), "get" + toUpperCaseFirstOne(field.getName()),
|
|
|
+ exportProperty.width(), exportProperty.index());
|
|
|
columnSettings.add(columnSetting);
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
Collections.sort(columnSettings);
|
|
|
return columnSettings;
|
|
|
}
|
|
|
-
|
|
|
- private static String toUpperCaseFirstOne(String s){
|
|
|
- if(Character.isUpperCase(s.charAt(0)))
|
|
|
- return s;
|
|
|
- else
|
|
|
- return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ private static String toUpperCaseFirstOne(String s) {
|
|
|
+ if (Character.isUpperCase(s.charAt(0)))
|
|
|
+ return s;
|
|
|
+ else
|
|
|
+ return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
|
|
|
+ }
|
|
|
+
|
|
|
private static String getExcelColumnName(int celNum) {
|
|
|
- int num = celNum+1;//celNum是从0算起
|
|
|
- String tem = "";
|
|
|
- while(num > 0) {
|
|
|
- int lo = (num - 1) % 26;//取余,A到Z是26进制,
|
|
|
- tem = (char)(lo + 'A') + tem;
|
|
|
- num = (num - 1) / 26;//取模
|
|
|
- }
|
|
|
- return tem;
|
|
|
- }
|
|
|
+ int num = celNum + 1;//celNum是从0算起
|
|
|
+ String tem = "";
|
|
|
+ while (num > 0) {
|
|
|
+ int lo = (num - 1) % 26;//取余,A到Z是26进制,
|
|
|
+ tem = (char) (lo + 'A') + tem;
|
|
|
+ num = (num - 1) / 26;//取模
|
|
|
+ }
|
|
|
+ return tem;
|
|
|
+ }
|
|
|
+
|
|
|
}
|