69 lines
2.5 KiB
Java
Raw Normal View History

2022-02-12 08:48:57 +08:00
package com.xkrs.util;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
import java.util.Map;
/**
* @Author: XinYi Song
* @Date: 2022/1/17 8:56
*/
public class ExportExcel {
/**
* 最简单的填充
*
* @since 2.1.1
*/
/*public static String simpleFill(Map<String, String> map,String templateFileName) {
String s = System.currentTimeMillis() + ".xlsx";
// 方案2 根据Map填充
String fileName = "/usr/local/excel/" + s;
//String fileName = "E:\\shoptest\\" + s;
// 这里 会填充到第一个sheet 然后文件流会自动关闭
ExcelWriterBuilder write = EasyExcel.write(fileName);
ExcelWriterBuilder excelWriterBuilder = write.withTemplate(templateFileName);
ExcelWriter build = excelWriterBuilder.build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
build.fill(map,writeSheet);
build.finish();
//EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map);
return "http://118.24.27.47:2088/excel/"+s;
//return "http://192.168.2.139/"+s;
}*/
public static String exportToProveExcel(Map<String, String> dataMap, String srcFilePath) throws IOException, InvalidFormatException {
String s = System.currentTimeMillis() + ".xlsx";
//String path = "E:/shop/"+s;
String path = "/home/sxy/server/industrial_measurement/excel/" + s;
// 开始转换。利用 transformer 转到Excel
XLSTransformer transformer = new XLSTransformer();
// 参数srcFilePath模板源文件 cMap需要导出的数据 destFile.getAbsolutePath():下载的目标文件
transformer.transformXLS(srcFilePath, dataMap, path);
//return "http://192.168.2.139/"+s;
return "http://118.24.27.47:2088/excel/"+s;
}
/*public static void main(String[] args) throws IOException, InvalidFormatException {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
String templateFileName = TestFileUtil.getPath() + "templates" + File.separator + "test.xlsx";
Map<String, String> map = new HashMap<String, String>();
map.put("name", "张三");
map.put("sex", "");
//simpleFill(map,templateFileName);
HttpServletResponse response = null;
String s = exportToProveExcel(map);
System.out.println(s);
}*/
}