导出word模板 并放入zip进行压缩
导出word主模块
public void exportDocx() {
FileOutputStream os = null;
FileInputStream fs = null;
String fileName = null;
try {
//填充结果
Map map = new HashMap<>();
map.put("orgname", "aa");
map.put("name", "bb");
map.put("no", "aa");
map.put("faultAnalysis", "aa");
map.put("area", "aa");
map.put("descripted", "aa");
map.put("repairno", "aa");
String path = FilePathEnum.REPAIR_DOC.getPath();
fileName = String.format(FilePathEnum.REPAIR_DOC.getName(), "aaa");
InputStream resourceAsStream = this.getClass().getResourceAsStream(path);
Path res = WrodExport.getInstance().convertWord(resourceAsStream, map, fileName);
fs = new FileInputStream(res.toString());//用字节的形式进行读写
os = new FileOutputStream("F:\\测试\\a.docx");
int len;
byte[] bytes = new byte[1024];
while ((len = fs.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (fs != null) {
fs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
解读word模板
public class WrodExport {
public static WrodExport getInstance() {
return new WrodExport();
}
public Path convertWord(InputStream in, Map params, String fileName) throws IOException {
Assert.notNull(in, "参数不能为空");
Assert.notNull(fileName, "参数不能为空");
Assert.notNull(params, "参数不能为空");
Map newParams = new HashMap();
params.forEach((k, v) -> {
newParams.put("${" + k + "}", v);
});
Path path = Paths.get(System.getProperty("java.io.tmpdir"), fileName);
try {
OutputStream out = Files.newOutputStream(path);
Throwable var7 = null;
try {
XWPFDocument doc = new XWPFDocument(in);
this.getParamTable(doc, params, false);
this.replaceInPara((XWPFDocument)doc, newParams);
this.replaceInTable(doc, newParams);
doc.write(out);
} catch (Throwable var17) {
var7 = var17;
throw var17;
} finally {
if (out != null) {
if (var7 != null) {
try {
out.close();
} catch (Throwable var16) {
var7.addSuppressed(var16);
}
} else {
out.close();
}
}
}
} catch (Exception var19) {
var19.printStackTrace();
}
return path;
}
public Path convertWord(InputStream in, Map params, String fileName, boolean horizontal) throws IOException {
Assert.notNull(in, "参数不能为空");
Assert.notNull(fileName, "参数不能为空");
Assert.notNull(params, "参数不能为空");
Map newParams = new HashMap();
params.forEach((k, v) -> {
newParams.put("${" + k + "}", v);
});
Path path = Paths.get(System.getProperty("java.io.tmpdir"), fileName);
try {
OutputStream out = Files.newOutputStream(path);
Throwable var8 = null;
try {
XWPFDocument doc = new XWPFDocument(in);
this.getParamTable(doc, params, horizontal);
this.replaceInPara((XWPFDocument)doc, newParams);
this.replaceInTable(doc, newParams);
doc.write(out);
} catch (Throwable var18) {
var8 = var18;
throw var18;
} finally {
if (out != null) {
if (var8 != null) {
try {
out.close();
} catch (Throwable var17) {
var8.addSuppressed(var17);
}
} else {
out.close();
}
}
}
} catch (Exception var20) {
var20.printStackTrace();
}
return path;
}
private WrodExport() {
}
public void getParamTable(XWPFDocument doc, Map params, boolean horizontal) throws Exception {
List
模板位置
public enum FilePathEnum {
REPAIR_DOC("/template/equ_repair_exp.docx","维修工单导出%s.docx")
;
private String path;
private String name;
public String getPath() {
return path;
}
public String getName() {
return name;
}
FilePathEnum(String path, String name) {
this.path = path;
this.name = name;
}
}
word模板
压缩代码模板
public void compressFileToZipStream(ZipOutputStream zipOutputStream, ByteArrayOutputStream excelOutputStream, String excelFilename) {
byte[] buf = new byte[1024];
try {
// Compress the files
byte[] content = excelOutputStream.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(content);
BufferedInputStream bis = new BufferedInputStream(is);
// Add ZIP entry to output stream.
zipOutputStream.putNextEntry(new ZipEntry(excelFilename));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = bis.read(buf)) > 0) {
zipOutputStream.write(buf, 0, len);
}
// Complete the entry
//excelOutputStream.close();//关闭excel输出流
zipOutputStream.closeEntry();
bis.close();
is.close();
// Complete the ZIP file
} catch (IOException e) {
e.printStackTrace();
}
}
使用浏览器下载方式进行压缩
ServletOutputStream sos = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(sos);
//获取名字
String zipname = "压缩包名字.zip";
response.reset();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((zipname).getBytes(), "iso-8859-1"));
try {
for (int i = 0; i < 文件循环放入; i++) {
RepairDto repairDto = repairDtoList.get(i);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//拿取字节存储的输出流,将docx的文件解读写入到流中,并返回文件名称
String filename = "a"+i+".docx";
exportDocx(repairDto, baos);
compressFileToZipStream(zipOutputStream, baos, filename);
baos.close();
}
zipOutputStream.flush();
zipOutputStream.close();
sos.close();
------------恢复内容开始------------
导出word主模块
public void exportDocx() {
FileOutputStream os = null;
FileInputStream fs = null;
String fileName = null;
try {
//填充结果
Map map = new HashMap<>();
map.put("orgname", "aa");
map.put("name", "bb");
map.put("no", "aa");
map.put("faultAnalysis", "aa");
map.put("area", "aa");
map.put("descripted", "aa");
map.put("repairno", "aa");
String path = FilePathEnum.REPAIR_DOC.getPath();
fileName = String.format(FilePathEnum.REPAIR_DOC.getName(), "aaa");
InputStream resourceAsStream = this.getClass().getResourceAsStream(path);
Path res = WrodExport.getInstance().convertWord(resourceAsStream, map, fileName);
fs = new FileInputStream(res.toString());//用字节的形式进行读写
os = new FileOutputStream("F:\\测试\\a.docx");
int len;
byte[] bytes = new byte[1024];
while ((len = fs.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (fs != null) {
fs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
解读word模板
public class WrodExport {
public static WrodExport getInstance() {
return new WrodExport();
}
public Path convertWord(InputStream in, Map params, String fileName) throws IOException {
Assert.notNull(in, "参数不能为空");
Assert.notNull(fileName, "参数不能为空");
Assert.notNull(params, "参数不能为空");
Map newParams = new HashMap();
params.forEach((k, v) -> {
newParams.put("${" + k + "}", v);
});
Path path = Paths.get(System.getProperty("java.io.tmpdir"), fileName);
try {
OutputStream out = Files.newOutputStream(path);
Throwable var7 = null;
try {
XWPFDocument doc = new XWPFDocument(in);
this.getParamTable(doc, params, false);
this.replaceInPara((XWPFDocument)doc, newParams);
this.replaceInTable(doc, newParams);
doc.write(out);
} catch (Throwable var17) {
var7 = var17;
throw var17;
} finally {
if (out != null) {
if (var7 != null) {
try {
out.close();
} catch (Throwable var16) {
var7.addSuppressed(var16);
}
} else {
out.close();
}
}
}
} catch (Exception var19) {
var19.printStackTrace();
}
return path;
}
public Path convertWord(InputStream in, Map params, String fileName, boolean horizontal) throws IOException {
Assert.notNull(in, "参数不能为空");
Assert.notNull(fileName, "参数不能为空");
Assert.notNull(params, "参数不能为空");
Map newParams = new HashMap();
params.forEach((k, v) -> {
newParams.put("${" + k + "}", v);
});
Path path = Paths.get(System.getProperty("java.io.tmpdir"), fileName);
try {
OutputStream out = Files.newOutputStream(path);
Throwable var8 = null;
try {
XWPFDocument doc = new XWPFDocument(in);
this.getParamTable(doc, params, horizontal);
this.replaceInPara((XWPFDocument)doc, newParams);
this.replaceInTable(doc, newParams);
doc.write(out);
} catch (Throwable var18) {
var8 = var18;
throw var18;
} finally {
if (out != null) {
if (var8 != null) {
try {
out.close();
} catch (Throwable var17) {
var8.addSuppressed(var17);
}
} else {
out.close();
}
}
}
} catch (Exception var20) {
var20.printStackTrace();
}
return path;
}
private WrodExport() {
}
public void getParamTable(XWPFDocument doc, Map params, boolean horizontal) throws Exception {
List
模板位置
public enum FilePathEnum {
REPAIR_DOC("/template/equ_repair_exp.docx","维修工单导出%s.docx")
;
private String path;
private String name;
public String getPath() {
return path;
}
public String getName() {
return name;
}
FilePathEnum(String path, String name) {
this.path = path;
this.name = name;
}
}
word模板
压缩代码模板
public void compressFileToZipStream(ZipOutputStream zipOutputStream, ByteArrayOutputStream excelOutputStream, String excelFilename) {
byte[] buf = new byte[1024];
try {
// Compress the files
byte[] content = excelOutputStream.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(content);
BufferedInputStream bis = new BufferedInputStream(is);
// Add ZIP entry to output stream.
zipOutputStream.putNextEntry(new ZipEntry(excelFilename));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = bis.read(buf)) > 0) {
zipOutputStream.write(buf, 0, len);
}
// Complete the entry
//excelOutputStream.close();//关闭excel输出流
zipOutputStream.closeEntry();
bis.close();
is.close();
// Complete the ZIP file
} catch (IOException e) {
e.printStackTrace();
}
}
使用浏览器下载方式进行压缩
ServletOutputStream sos = response.getOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(sos);
//获取名字
String zipname = "压缩包名字.zip";
response.reset();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((zipname).getBytes(), "iso-8859-1"));
try {
for (int i = 0; i < 文件循环放入; i++) {
RepairDto repairDto = repairDtoList.get(i);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//拿取字节存储的输出流,将docx的文件解读写入到流中,并返回文件名称
String filename = "a"+i+".docx";
exportDocx(repairDto, baos);
compressFileToZipStream(zipOutputStream, baos, filename);
baos.close();
}
zipOutputStream.flush();
zipOutputStream.close();
sos.close();
------------恢复内容结束------------
