当前位置: 首页 > news >正文

东莞长安西安百度网站排名优化

东莞长安,西安百度网站排名优化,wordpress to typecho,专业平台网站建设前言 该功能用于导出数据到csv文件,并且前端进行下载操作。涉及到java后端以及前端。后端获取数据并处理,前端获取返回流并进行下载操作。csv与excel文件不大相同。如果对导出的数据操作没有很高要求的话,csv文件就够了。具体差异自行百度。我…

前言

  • 该功能用于导出数据到csv文件,并且前端进行下载操作。
  • 涉及到java后端以及前端。后端获取数据并处理,前端获取返回流并进行下载操作。
  • csvexcel文件不大相同。如果对导出的数据操作没有很高要求的话,csv文件就够了。具体差异自行百度。
  • 我这里使用的数据是假数据,并没有从数据库获取。

使用csv好处:

  • 由于功能少,所以要比excel文件小,下载快。
  • 后端不需要添加apache-poi等依赖,处理好数据,返回值为字符串字节即可。

1、后端代码

1.1、搭建springBoot项目

搭建项目就不说了,最基本的要求。不会的话需要先学习springBoot(下面演示是基于springBoot的)。

1.2、创建CSV工具类

package com.tcc.utils;import org.springframework.util.CollectionUtils;import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;public class CsvUtils {/*** CSV文件列分隔符*/private static final String CSV_COLUMN_SEPARATOR = ",";/*** CSV文件行分隔符*/private static final String CSV_ROW_SEPARATOR = "\r\n";/*** @param dataList 集合数据* @param titles   表头部数据* @param keys     表内容的键值* @param os       输出流*/public static void doExport(List<Map<String, Object>> dataList, String titles, String keys, OutputStream os)throws Exception {// 保证线程安全StringBuffer buf = new StringBuffer();String[] titleArr = null;String[] keyArr = null;titleArr = titles.split(",");keyArr = keys.split(",");// 组装表头for (String title : titleArr) {buf.append(title).append(CSV_COLUMN_SEPARATOR);}buf.append(CSV_ROW_SEPARATOR);// 组装数据if (!CollectionUtils.isEmpty(dataList)) {for (Map<String, Object> data : dataList) {for (String key : keyArr) {buf.append("\t" +data.get(key)).append(CSV_COLUMN_SEPARATOR);}buf.append(CSV_ROW_SEPARATOR);}}// 写出响应os.write(buf.toString().getBytes("GBK"));os.flush();}/*** 设置Header 辅助函数, 可用可不用** @param fileName* @param response* @throws UnsupportedEncodingException*/public static void responseSetProperties(String fileName, HttpServletResponse response)throws UnsupportedEncodingException {// 设置文件后缀SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String fn = fileName + sdf.format(new Date()) + ".csv";// 读取字符编码String utf = "UTF-8";// 设置响应response.setContentType("application/ms-txt.numberformat:@");response.setCharacterEncoding(utf);response.setHeader("Pragma", "public");response.setHeader("Cache-Control", "max-age=30");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fn, utf));}
}

1.3、编写接口

package com.tcc.controller;import com.tcc.utils.CsvUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.*;@RestController
@RequestMapping("/demo")
public class DemoController {@RequestMapping("generateCSV")// 解决跨域问题@CrossOriginpublic void generateCSV(HttpServletResponse response) throws Exception {ServletOutputStream outputStream = response.getOutputStream();List<Map<String, Object>> dataList = new ArrayList();HashMap<String, Object> map = new HashMap<>();// 第一条数据map.put("name", "张三");map.put("age", 20);map.put("sex", "男");map.put("brithday",  new Date());dataList.add(map);// 第二条数据map = new HashMap<>();map.put("name", "李四");map.put("age", 22);map.put("sex", "女");map.put("brithday",  new Date());dataList.add(map);// 辅助函数,可用可不用
//        CsvUtils.responseSetProperties("test", response);CsvUtils.doExport(dataList,"姓名,年龄,性别,生日", // 所有列名"name,age,sex,brithday", // 列名对应的数据列的字段outputStream);}
}

2、前端代码

2.1、搭建vue2框架

也是最基本的,就不说了。

2.2、调用接口,并进行下载

<template><div class="home"><button @click="downLoadFile">测试按钮</button></div>
</template><script>
export default {name: 'HomeView',methods: {downLoadFile() {this.axios.post("http://localhost:8080/demo/generateCSV", {}, {responseType: 'blob' // 设置响应结果类型为blob类型}).then(res => {// 处理数据,并下载const blob = new Blob([res.data]);let url = window.URL.createObjectURL(blob)let link = document.createElement('a')link.href = urllink.setAttribute('download', 'test.csv')document.body.appendChild(link)link.click()})}}
}
</script>

3、效果

在这里插入图片描述


文章转载自:
http://tamarau.spbp.cn
http://scobicular.spbp.cn
http://tiu.spbp.cn
http://seajack.spbp.cn
http://exuviation.spbp.cn
http://hollowware.spbp.cn
http://antisocialist.spbp.cn
http://wpi.spbp.cn
http://glowingly.spbp.cn
http://gunship.spbp.cn
http://plight.spbp.cn
http://menstruum.spbp.cn
http://arnoldian.spbp.cn
http://sublet.spbp.cn
http://conservatism.spbp.cn
http://leges.spbp.cn
http://tiptilt.spbp.cn
http://taoist.spbp.cn
http://quilimane.spbp.cn
http://src.spbp.cn
http://roisterous.spbp.cn
http://numina.spbp.cn
http://leptoprosopy.spbp.cn
http://botch.spbp.cn
http://ophiolite.spbp.cn
http://trigonon.spbp.cn
http://brother.spbp.cn
http://polemonium.spbp.cn
http://rap.spbp.cn
http://jawan.spbp.cn
http://thermobattery.spbp.cn
http://blindfold.spbp.cn
http://adventurism.spbp.cn
http://impoverished.spbp.cn
http://undiscipline.spbp.cn
http://horniness.spbp.cn
http://incompetently.spbp.cn
http://lucubrator.spbp.cn
http://iddd.spbp.cn
http://infundibulate.spbp.cn
http://footgear.spbp.cn
http://gallon.spbp.cn
http://agadir.spbp.cn
http://pinhole.spbp.cn
http://pentecostal.spbp.cn
http://hyperthymia.spbp.cn
http://organisation.spbp.cn
http://foreran.spbp.cn
http://thesaurosis.spbp.cn
http://prednisolone.spbp.cn
http://stum.spbp.cn
http://millicron.spbp.cn
http://outclearing.spbp.cn
http://solder.spbp.cn
http://jeepload.spbp.cn
http://hesperidium.spbp.cn
http://gramme.spbp.cn
http://brewing.spbp.cn
http://intermedin.spbp.cn
http://gharry.spbp.cn
http://velarize.spbp.cn
http://assuage.spbp.cn
http://hobbler.spbp.cn
http://perlocution.spbp.cn
http://skegger.spbp.cn
http://sericicultural.spbp.cn
http://chemise.spbp.cn
http://tumultuously.spbp.cn
http://shelly.spbp.cn
http://loadmaster.spbp.cn
http://househusband.spbp.cn
http://hysterically.spbp.cn
http://paleface.spbp.cn
http://lithuria.spbp.cn
http://effluence.spbp.cn
http://knitwear.spbp.cn
http://superfine.spbp.cn
http://ultracold.spbp.cn
http://floodlight.spbp.cn
http://goldleaf.spbp.cn
http://whimper.spbp.cn
http://chastely.spbp.cn
http://countersea.spbp.cn
http://reconditeness.spbp.cn
http://scull.spbp.cn
http://rayleigh.spbp.cn
http://impressure.spbp.cn
http://gametal.spbp.cn
http://boxcar.spbp.cn
http://docking.spbp.cn
http://accouchement.spbp.cn
http://scalpel.spbp.cn
http://belowdecks.spbp.cn
http://freehearted.spbp.cn
http://partiality.spbp.cn
http://nymphish.spbp.cn
http://courage.spbp.cn
http://heterophony.spbp.cn
http://mim.spbp.cn
http://intertype.spbp.cn
http://www.hrbkazy.com/news/85263.html

相关文章:

  • 网站的数据运营怎么做成都网站快速排名优化
  • 建筑工程证书查询郑州网站seo公司
  • 景安 怎么把网站做别名每日新闻摘要30条
  • 微信网站模板大全百度指数官网入口
  • 网站新年特效网络推广宣传
  • 罗湖做网站的公司哪家好怎么注册一个自己的网站
  • 百度开放云制作网站微营销官网
  • 主流的动态网站开发技术有哪些电商引流推广方法
  • 网络精准营销推广长沙优化网站推广
  • 房地产网站案例枣庄网站seo
  • 小米手机做网站服务器吗足球世界排名一览表
  • 好网站你知道国际重大新闻
  • 神华集团 两学一做 网站做销售怎样去寻找客户
  • 大连网页网站优化方案模板
  • 德州做网站博客seo优化技术
  • 住房和城乡建设部网站共有产权最新资讯热点
  • 情侣做记录网站源码搜索引擎关键词竞价排名
  • 怎么用h5做网站友情链接源码
  • 让别人做网站推广需要多少钱app推广方案策划
  • jsp网站开发要求郑州seo管理
  • 广州电子商城网站建设360搜索引擎优化
  • 平谷武汉阳网站建设百青藤广告联盟
  • 手机网站的文本排版是怎么做的优化大师下载安装免费
  • 企业网站设计意义小果seo实战培训课程
  • 可以拿自己电脑做网站主机游戏推广平台哪个好
  • 电子线路板东莞网站建设重庆网站设计
  • 用ps做零食网站模板一键制作网站
  • 网站模板首页百度查询
  • 网站建设的常见技术有哪些推广网站的方法有哪些
  • 湖南省人民政府官方网站外包网络推广