Vue实现导出xml

1.最终效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-m6pHR3Li-1634720630926)(C:\Users\HSLM\AppData\Roaming\Typora\typora-user-images\image-20211020170226995.png)]

2.环境要求

安装x2js模块

在项目终端运行以下命令

cnpm install x2js --save

在main.js配置文件中引入模块,并且全局挂载

// 引入模块
import x2js from 'x2js'

const app = createApp(App)
// 全局挂载 x2js
app.config.globalProperties.$x2js = new x2js()

3.将json文件导出为xml

//点击按钮触发该按钮 
exportXml(){
    //需要导出的json文件
      let groupdata = {NoticeData:this.testdata}
    //groupdata必须只有一个跟节点noticeData不然导出xml时会报错
      let obj = {
        Info:groupdata,
      };
      // 调用$x2js 将我们的json数据转换成xml数据格式
      let xml = this.$x2js.js2xml(obj);
      // 下面就是我们想要的xml文件的数据格式了
      // 这里会生成一个url
      let url = window.URL.createObjectURL(
          new Blob([xml], {type: "text/xml"})
      );
      // 然后就可以创建a标签 最后下载下来了
      let link = document.createElement("a");
      // 不显示链接
      link.style.display = "none";
      link.href = url;
      // 设置链接属性
      link.setAttribute("download", "自定义文件名字");
      //点击链接
      document.body.appendChild(link);
      link.click();
    },

4.全部代码

<template>   
	<el-button type="primary" @click="exportExcel">导出Excel</el-button>
</template>


<script lang="ts">
import { defineComponent, ref  } from 'vue'
import { ElMessage } from 'element-plus'
export default defineComponent({
     data() {
         return {
            testdata: [
            {
              "noticeId":345646,
              "satName":"ZOHREH-2",
              "country":"IRN",
              "freqMin":456.0,
              "freqMax":456.0,
              "beamInfoList":
                  [
                    {
                      "beamName":"RS49",
                      "freqMin":3456.0,
                      "freqMax":654.0,
                      "groupInfoList":
                          [
                            {
                              "groupId":567.34,
                              "freqMin":768.0,
                              "freqMax":678.0,
                              "pwrMax":1.0
                            },
                            {
                              "groupId":10600362,
                              "freqMin":11450.0,
                              "freqMax":11700.0,
                              "pwrMax":2.0
                            },
                            {
                              "groupId":10600363,
                              "freqMin":14000.0,
                              "freqMax":14500.0,
                              "pwrMax":3.0
                            }
                          ]
                    },
                 ]
        	}]
         }
     },
    methods: {
      let groupdata = {NoticeData:this.testdata}
    //groupdata必须只有一个跟节点noticeData不然导出xml时会报错
      let obj = {
        Info:groupdata,
      };
      // 调用$x2js 将我们的json数据转换成xml数据格式
      let xml = this.$x2js.js2xml(obj);
      // 下面就是我们想要的xml文件的数据格式了
      // 这里会生成一个url
      let url = window.URL.createObjectURL(
          new Blob([xml], {type: "text/xml;charset=utf-8"})
      );

      // 然后就可以创建a标签 最后下载下来了
      let link = document.createElement("a");
      // 不显示链接
      link.style.display = "none";
      link.href = url;
      // 设置链接属性
      link.setAttribute("download", "频段查询");
      //点击链接
      document.body.appendChild(link);
      link.click();
      // 删除连接
      document.body.removeChild(link);
    }
    
})
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
实现Spring Boot和Vue导出Word文档,可以使用poi和docx4j这两个工具。 首先是后端Spring Boot的实现: 1. 添加poi和docx4j依赖到pom.xml文件中: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>11.3.3</version> </dependency> ``` 2. 创建Word导出接口: ```java @RestController @RequestMapping("/export") public class ExportController { @GetMapping("/word") public void exportWord(HttpServletResponse response) throws Exception { // 创建一个空白的Word文档 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); // 添加段落 wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello, World!"); // 设置响应头 response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); response.setHeader("Content-Disposition", "attachment; filename=test.docx"); // 输出Word文档 wordMLPackage.save(response.getOutputStream()); } } ``` 3. 启动Spring Boot应用,访问http://localhost:8080/export/word即可下载导出的Word文档。 然后是前端Vue实现: 1. 安装axios和file-saver依赖: ```bash npm install axios file-saver --save ``` 2. 创建导出Word的方法: ```js exportWord() { axios({ method: 'get', url: '/export/word', responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const fileName = 'test.docx'; saveAs(blob, fileName); }); } ``` 3. 在Vue组件中添加一个按钮,并绑定导出Word的方法: ```html <template> <div> <button @click="exportWord">导出Word</button> </div> </template> <script> import axios from 'axios'; import { saveAs } from 'file-saver'; export default { name: 'Export', methods: { exportWord() { axios({ method: 'get', url: '/export/word', responseType: 'blob' }).then(response => { const blob = new Blob([response.data]); const fileName = 'test.docx'; saveAs(blob, fileName); }); } } }; </script> ``` 4. 运行Vue应用,点击按钮即可下载导出的Word文档。 以上就是Spring Boot和Vue导出Word文档的实现步骤,希望能对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值