栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

vue导出word实战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue导出word实战

vue导出Word实战

产品经理:系统上显示职员的姓名,年龄,工作照等信息。老板想把他们打印出来,做成简历并且不能是pdf必须是word

项目经理:工作都忙不完,他咋一天天事这么多

开发: 分分钟给他搞定吧,小事!

框架 vue+springboot

  1. vue代码
导出(word)
  1. js代码
import {exportWord} from '@/api/export.js'  
printWord(){
	let exportName = "导出此word.doc";
	exportWord({code:this.code}).then((result) => {
			let blob = new Blob([result], {
				type: "application/vnd.ms-excel"
			});
			if ('download' in document.createElement('a')) {
				const link = document.createElement('a');
				link.style.display = "none";
				link.href = URL.createObjectURL(blob);
				link.setAttribute("download", exportName);
				document.body.appendChild(link);
				link.click();
				document.body.removeChild(link);
			} else {
				navigator.msSaveBlob(blob, exportName);
			}
			this.cancel();
	})
},
  1. export. js
//导出word
export const exportWord = params => {
    return axios.post('export/exportWord', params).then(res => res.data);
};
  1. controller
@Controller
@ResponseBody
@RequestMapping("/export")
public class ExportController extends baseController {

    @Autowired
    ExportApplication exportApplication;
    
    // 用途说明: 导出word
    @PostMapping(value = "/exportWord")
    public void exportWord() {
        try {
           exportApplication.exportWord();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
  1. application
public interface ExportApplication {
     void exportWord();
}
  1. applicationImpl
@Component
@Slf4j
public class ExportApplicationImpl implements ExportApplication {

    @Override
    public void exportWord() {
		//查询你自己的业务数据
        List exportInfo = .....查询业务的方法....;
        Map dataMap = new HashMap();
        //familyList 是职员的家庭信息的集合,对应模板中的表格
        List> familyList = new ArrayList>();
        for (int i = 0; i < exportInfo.size(); i++) {
            Map map = new HashMap();
            //获取的职员姓名信息
            map.put("name", exportInfo.get(i).getName());
            //获取的职员年龄信息
            map.put("age", exportInfo.get(i).getAge());
			//获取的职员照片信息
            map.put("phoneSrc", exportInfo.get(i).getPhoneSrc());
			//添加到大家庭中
            familyList.add(map);
        }
		//将信息放到要导出的Map中
        dataMap.put("expList", familyList);

        @SuppressWarnings("deprecation")
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("utf-8");
        //有两种方式获取你的模板,模板在项目中时用第一个,模板在本地时用第二个。
        //取得的模板
        configuration.setClassForTemplateLoading(this.getClass(), "/template");
        HttpServletResponse response = HttpServlet.getResponse();
        response.setContentType("application/vnd.ms-excel;chartset=utf-8");
        response.setCharacterEncoding("UTF-8");
        Template t = null;
        Writer out = null;
        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode("文件名","UTF-8") +".doc");
            t = configuration.getTemplate("template.ftl", "utf-8"); //文件名,获取模板
            out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
            t.process(dataMap, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException  e1) {

            }
        }

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号