|
|
@@ -1,10 +1,26 @@
|
|
|
package com.uas.platform.b2c.core.support.view;
|
|
|
|
|
|
+import com.uas.platform.b2c.core.utils.ContextUtils;
|
|
|
+import com.uas.platform.b2c.core.utils.StringUtilB2C;
|
|
|
+import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
+import javassist.runtime.Desc;
|
|
|
+import net.sf.jxls.transformer.XLSTransformer;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
+import org.springframework.core.io.support.LocalizedResourceHelper;
|
|
|
+import org.springframework.web.servlet.support.RequestContextUtils;
|
|
|
import org.springframework.web.servlet.view.document.AbstractXlsxView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -15,15 +31,10 @@ import java.util.Map;
|
|
|
*/
|
|
|
public class JxlsxExcelView extends AbstractXlsxView {
|
|
|
|
|
|
- /**
|
|
|
- * 文件名
|
|
|
- */
|
|
|
- private String fileName;
|
|
|
-
|
|
|
/**
|
|
|
* 导入.xls格式文件
|
|
|
*/
|
|
|
- private static final String EXTENSION = ".xls";
|
|
|
+ private static final String EXTENSION = ".xlsx";
|
|
|
|
|
|
/**
|
|
|
* .xls 表格
|
|
|
@@ -35,22 +46,117 @@ public class JxlsxExcelView extends AbstractXlsxView {
|
|
|
*/
|
|
|
public static final String EXCEL_XLSX = "xlsx";
|
|
|
|
|
|
+ /**
|
|
|
+ * url生成模板的路径
|
|
|
+ */
|
|
|
+ private String url = "C:\\Users\\yuj\\Downloads\\xls\\template\\";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 模板路径
|
|
|
+ */
|
|
|
+ private String templateUrl;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件名
|
|
|
+ */
|
|
|
+ private String fileName;
|
|
|
+
|
|
|
+ public JxlsxExcelView() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public JxlsxExcelView(String fileName, String templateUrl) {
|
|
|
+ this.fileName = fileName;
|
|
|
+ this.templateUrl = templateUrl;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
- protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
|
|
|
-/* if (model == null)
|
|
|
- map = new HashMap<String, Object>();
|
|
|
- map.put("export", new Export());
|
|
|
+ protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
|
|
|
+ if (map == null) {
|
|
|
+ map = new HashMap<>();
|
|
|
+ }
|
|
|
XLSTransformer transformer = new XLSTransformer();
|
|
|
transformer.transformWorkbook(workbook, map);
|
|
|
- response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + EXTENSION);*/
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + EXTENSION);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected Workbook createWorkbook(Map<String, Object> model, HttpServletRequest request) {
|
|
|
- Object filePath = model.get("filePath");
|
|
|
- return super.createWorkbook(model, request);
|
|
|
+// File file = null;
|
|
|
+// try {
|
|
|
+// file = copyFile(request);
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+ LocalizedResourceHelper helper = new LocalizedResourceHelper(ContextUtils.getApplicationContext());
|
|
|
+ Locale userLocale = RequestContextUtils.getLocale(request);
|
|
|
+ Resource inputFile = helper.findLocalizedResource(templateUrl, EXTENSION, userLocale);
|
|
|
+ Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ workbook = getWorkbook(inputFile.getFile());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 复制模板文件
|
|
|
+ */
|
|
|
+ private File copyFile(HttpServletRequest request) throws IOException {
|
|
|
+ if (StringUtilB2C.isEmpty(fileName)) {
|
|
|
+ throw new IllegalOperatorException("文件名称不存在");
|
|
|
+ }
|
|
|
+ if (StringUtilB2C.isEmpty(templateUrl)) {
|
|
|
+ throw new IllegalOperatorException("模板文件不存在");
|
|
|
+ }
|
|
|
+ LocalizedResourceHelper helper = new LocalizedResourceHelper(ContextUtils.getApplicationContext());
|
|
|
+ Locale userLocale = RequestContextUtils.getLocale(request);
|
|
|
+ Resource inputFile = helper.findLocalizedResource(templateUrl, EXTENSION, userLocale);
|
|
|
+ inputFile.isOpen();
|
|
|
+ inputFile.isReadable();
|
|
|
+ inputFile.getFile();
|
|
|
+ if (inputFile == null || !inputFile.exists()) {
|
|
|
+ throw new IllegalOperatorException("模板文件不存在");
|
|
|
+ }
|
|
|
+ File destFile = new File(this.url + fileName + System.currentTimeMillis() + EXTENSION);
|
|
|
+ if (!destFile.exists()) {
|
|
|
+ destFile.createNewFile();
|
|
|
+ }
|
|
|
+ FileInputStream ins = new FileInputStream(new File(inputFile.getURL().getFile()));
|
|
|
+ FileOutputStream out = new FileOutputStream(destFile);
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int n = 0;
|
|
|
+ while ((n = ins.read(b)) != -1) {
|
|
|
+ out.write(b, 0, n);
|
|
|
+ }
|
|
|
+ ins.close();
|
|
|
+ out.close();
|
|
|
+ return destFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ /***
|
|
|
+ * 根据文件获取workbook
|
|
|
+ * @param file 文件
|
|
|
+ * @return 返回值
|
|
|
+ */
|
|
|
+ private Workbook getWorkbook(File file) {
|
|
|
+ if (file == null || !file.exists()) {
|
|
|
+ throw new IllegalOperatorException("模板文件不存在");
|
|
|
+ }
|
|
|
+ Workbook workbook = null;
|
|
|
+ try {
|
|
|
+ workbook = WorkbookFactory.create(file);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (InvalidFormatException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return workbook;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
//
|
|
|
// public JxlsxExcelView(String tplPath, String fileName) {
|
|
|
// super();
|