若依使用easypoi导出word文档
若依使用easypoi导出word文档
·
1、引入easypoi依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
2、导出word文档工具类
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.Map;
/**
* @description Word相关处理
*/
public class WordUtil {
/**
* EasyPoi 替换数据 导出 word
* @param templatePath word模板地址
* @param tempDir 临时文件存放地址
* @param filename 文件名称
* @param data 替换参数
* @param request
* @param response
*/
public static String easyPoiExport(String templatePath, String tempDir, String filename, Map<String, Object> data, HttpServletRequest request, HttpServletResponse response) {
Assert.notNull(templatePath, "模板路径不能为空");
Assert.notNull(tempDir, "临时文件路径不能为空");
Assert.notNull(filename, "文件名称不能为空");
Assert.isTrue(filename.endsWith(".docx"), "文件名称仅支持docx格式");
if (!tempDir.endsWith("/")) {
tempDir = tempDir + File.separator;
}
File file = new File(tempDir);
if (!file.exists()) {
file.mkdirs();
}
try {
String userAgent = request.getHeader("user-agent").toLowerCase();
if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("utf-8"), "ISO-8859-1");
}
XWPFDocument document = WordExportUtil.exportWord07(templatePath, data);
String tempPath = tempDir + filename;
FileOutputStream out = new FileOutputStream(tempPath);
document.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
deleteTempFile(tempDir, filename);
}
return filename;
}
/**
* 删除临时生成的文件
*/
public static void deleteTempFile(String filePath, String fileName) {
File file = new File(filePath + fileName);
File f = new File(filePath);
file.delete();
f.delete();
}
}
3、Controller编写测试接口
/**
* 根据模板导出word
*/
@Log(title = "根据模板导出word", businessType = BusinessType.IMPORT)
@PreAuthorize("@ss.hasPermi('occupation:basics:exportWord')")
@GetMapping("/exportWord")
public AjaxResult exportWord(HttpServletRequest request, HttpServletResponse response){
Map<String, Object> map = new HashMap<>();
map.put("number", "工单编号");
map.put("code", "零件编码");
map.put("name", "零件名称");
map.put("quantity", "计划数");
map.put("time", "计划工时");
map.put("staff", "生产员工");
map.put("seat", "生产机位");
map.put("totalpreyear", "2660");//单条数据
map.put("totalthisyear", "3400");//单条数据
String str = UUID.randomUUID().toString()+".docx";
//获取yml配置地址
String tempDir = RCloudServerConfig.getProfile() + "/download/";
String name = WordUtil.easyPoiExport("static/word/派遣模板.docx", tempDir, str, map, request, response);
return AjaxResult.success(name);
}
4、模板及模板位置
5、前端按钮事件
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExports" v-hasPermi="['occupation:basics:exportWord']">word</el-button>
</el-col>
6、前端js引入
// 根据模板导出word
export function exportWord(query) {
return request({
url: '/occupation/basics/exportWord',
method: 'get',
params: query
})
}
7、前端按钮方法
//导出按钮操作
handleExports() {
const queryParams = this.queryParams;
this.$confirm('是否确认导出word文档?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(() => {
this.exportLoading = true;
return exportWord(queryParams);
}).then(response => {
this.download(response.msg);
this.exportLoading = false;
}).catch(() => {});
},
8、效果
更多推荐
已为社区贡献1条内容
所有评论(0)