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

网络营销导向企业网站建设的原则包括汕头seo

网络营销导向企业网站建设的原则包括,汕头seo,中卫平面设计培训,网页游戏开发软件前言 csv格式的表格&#xff0c;和xls以及xlsx格式的表格有一些不同&#xff0c;不能够直接用处理xls的方式处理csv&#xff1b; 以下我将介绍如何读取并写入csv数据 准备工作 要处理csv格式的表格数据&#xff0c;我们首先需要引入pom.xml的依赖 <dependency><art…

前言

csv格式的表格,和xls以及xlsx格式的表格有一些不同,不能够直接用处理xls的方式处理csv;
以下我将介绍如何读取并写入csv数据

准备工作

要处理csv格式的表格数据,我们首先需要引入pom.xml的依赖

    <dependency><artifactId>commons-csv</artifactId><groupId>org.apache.commons</groupId><version>1.8</version></dependency><dependency>

读取

前端

一般情况下,我们都是使用前后端交互获取到csv文件,那么前端应该是写一个file类型的input接收文件,为演示方便使用原生的组件

// 上传文件的组件
<input type="file" id="fileInput" name="file">
// 点击上传文件
<button type="button" onclick="uploadFile()">上传</button>

其中js逻辑如下:

// 上传文件方法function uploadFile() {var fileInput = document.getElementById("fileInput");var file = fileInput.files[0];var formData = new FormData();formData.append("file", file);
// 此处调用后端接口上传文件}

ps:需要注意的是,为了正确上传成功文件,我们的请求应该为:multipart/form-data
如果你使用ajax上传,示例如下:

export function uploadFileInterface(obj) { return service.post('/xxx', obj, {headers: { 'Content-Type': 'multipart/form-data' }
})

其中service为我封装的ajax的js
代码如下:

import axios from 'axios'// 创建axios实例
// eslint-disable-next-line no-unused-vars
const service = axios.create({baseURL: '/', // api的base_Urltimeout: 50000 // 请求超时时间
});// 请求拦截器
axios.interceptors.request.use(function (config) {// 在发送请求之前做些什么return config},function (error) {// 对请求错误做些什么return Promise.reject(error)}
)// 响应拦截器
axios.interceptors.response.use(function (config) {// 对响应数据做点什么return config},function (error) {// 对响应错误做点什么return Promise.reject(error)}
)export default service

后端

我的csv文件如下图所示:
在这里插入图片描述

读取到的数据为:
在这里插入图片描述

后端接收文件的逻辑如下:

  @POST@Path("/getFile")public String submitOriginalFile(FormDataMultiPart form) {// 拿取前端标识的文件// 获取文件流InputStream fileIns = form.getField("file").getValueAs(InputStream.class);// 获取文件名,中文不乱码写法String name = new String(form.getField("file").getContentDisposition().getFileName().getBytes("iso-8859-1"), "UTF-8");// 将文件流转byte,可作为其他情况存储起来处理byte[] fileBytes = new InputStreamToByteArray(fileIns);// 将byte数组转csv数据List<CSVRecord> csvData = new FormDataMultiPartJob().byteToCsvFileData(fileBytes);// 遍历每一行的数据for (int i = 0; i < csvData.size(); i++) {System.out.println("每一行数据:" + csvData.get(i));for(int j = 0; j < csvData.get(i).size(); j++) {System.out.println("每一行下每一个单元格数据:" + csvData.get(i).get(j));}}return "读取成功!";}

文件流转byte数组

  /*** 文件流转byte*/public byte[] InputStreamToByteArray(InputStream inputStream) {try {BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byte[] buffer;int len;byte[] buf = new byte[2048];while ((len = bufferedInputStream.read(buf)) != -1) {byteArrayOutputStream.write(buf, 0, len);}byteArrayOutputStream.flush();buffer = byteArrayOutputStream.toByteArray();inputStream.close();bufferedInputStream.close();byteArrayOutputStream.close();return buffer;} catch (Exception e) {return null;}}

byte数组转csv数据

/*** byte转csv数据*/public List<CSVRecord> byteToCsvFileData(byte[] bytes) {List<CSVRecord> records = new ArrayList<>();try {// 将 byte 数组转为 InputStreamReader 对象InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-8");// 创建csvParser对象CSVFormat csvFormat = CSVFormat.EXCEL;CSVParser parser = new CSVParser(in, csvFormat);// 读取CSV数据行records = parser.getRecords();parser.close();in.close();} catch (Exception e) {return null;}return records;}

写入

如果需要新建csv文件并往里面写数据,参考如下代码:

      try (CSVPrinter printer = new CSVPrinter(new FileWriter("data.csv"), CSVFormat.DEFAULT)) {List<String> header = Arrays.asList("Name", "Age", "Email");printer.printRecord(header);List<List<String>> data = Arrays.asList(Arrays.asList("John", "25", "john@example.com"),Arrays.asList("Alice", "30", "alice@example.com"),Arrays.asList("Bob", "40", "bob@example.com"));for (List<String> rowData : data) {printer.printRecord(rowData);}} catch (IOException e) {e.printStackTrace();}

上面的代码中,我们创建了一个名为 data.csv 的新 CSV 文件,并打开了一个 CSVPrinter 对象。printRecord方法用于打印数据行。

首先,我们打印了一个标题行。标题行包含列的名称。我们将标题行表示为 List 对象 header,并将其传递给 printRecord 方法以打印到文件中

其次,我们打印了一些数据行。数据行包含实际的数据。我们将所有数据行表示为、嵌套的 List 对象 data,并使用 for 循环将每行数据打印到文件中。

需要注意的是,这里使用到了 try-with-resources 语句块来确保 CSVPrinter 能够正确关闭。如果在使用 CSVPrinter 时发生任何异常,它都会在 try-with-resources 语句块结束时被捕获并关闭。


如果你需要使用不同的分隔符或者引号字符来定制 CSV 文件格式,你可以在创建 CSVPrinter 时指定对应的 CSVFormat 对象。例如,如果你要使用制表符作为分隔符,你可以使用以下代码创建 CSVPrinter:

CSVFormat format = CSVFormat.TDF.withHeader("Name", "Age", "Email");
try (CSVPrinter printer = new CSVPrinter(new FileWriter("data.tsv"), format)) {// 逻辑同上
} catch (IOException e) {e.printStackTrace();
}

以上代码会写成一个csv文件保存在你的工程文件里,假如你不想生成文件,而只想拿到byte数组数据做其他操作,参考如下:

try (ByteArrayOutputStream stream = new ByteArrayOutputStream();CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(stream), CSVFormat.DEFAULT)) {List<String> header = Arrays.asList("Name", "Age", "Email");printer.printRecord(header);List<List<String>> data = Arrays.asList(Arrays.asList("John", "25", "john@example.com"),Arrays.asList("Alice", "30", "alice@example.com"),Arrays.asList("Bob", "40", "bob@example.com"));for (List<String> rowData : data) {printer.printRecord(rowData);}// byte数组byte[] bytes = stream.toByteArray();} catch (IOException e) {e.printStackTrace();
}

结语

以上就是如何获取并写入csv文件的过程


文章转载自:
http://ameroenglish.rdfq.cn
http://dysphoria.rdfq.cn
http://amphiphyte.rdfq.cn
http://dak.rdfq.cn
http://ingvaeonic.rdfq.cn
http://indecency.rdfq.cn
http://chronicler.rdfq.cn
http://tiger.rdfq.cn
http://insect.rdfq.cn
http://overhaul.rdfq.cn
http://lace.rdfq.cn
http://rockfall.rdfq.cn
http://arsenide.rdfq.cn
http://multiformity.rdfq.cn
http://benzedrine.rdfq.cn
http://qualifiable.rdfq.cn
http://readset.rdfq.cn
http://tanglewrack.rdfq.cn
http://combinability.rdfq.cn
http://justice.rdfq.cn
http://superficiality.rdfq.cn
http://fireman.rdfq.cn
http://amateurish.rdfq.cn
http://imprint.rdfq.cn
http://garda.rdfq.cn
http://feeling.rdfq.cn
http://crappy.rdfq.cn
http://blame.rdfq.cn
http://demophile.rdfq.cn
http://solutrean.rdfq.cn
http://wattled.rdfq.cn
http://carotinoid.rdfq.cn
http://antismog.rdfq.cn
http://reciprocator.rdfq.cn
http://medium.rdfq.cn
http://trochelminth.rdfq.cn
http://marking.rdfq.cn
http://rhinopharynx.rdfq.cn
http://nucleolate.rdfq.cn
http://bronchium.rdfq.cn
http://declivity.rdfq.cn
http://teleconferencing.rdfq.cn
http://visitatorial.rdfq.cn
http://superfix.rdfq.cn
http://mandir.rdfq.cn
http://worksheet.rdfq.cn
http://recognition.rdfq.cn
http://vituperator.rdfq.cn
http://steady.rdfq.cn
http://sapphiric.rdfq.cn
http://medical.rdfq.cn
http://scunner.rdfq.cn
http://congressperson.rdfq.cn
http://disparagement.rdfq.cn
http://virtuosi.rdfq.cn
http://kinkily.rdfq.cn
http://admensuration.rdfq.cn
http://chance.rdfq.cn
http://blockship.rdfq.cn
http://counterthrust.rdfq.cn
http://losel.rdfq.cn
http://xenogamy.rdfq.cn
http://preclassical.rdfq.cn
http://clink.rdfq.cn
http://rajahmundry.rdfq.cn
http://playbroker.rdfq.cn
http://claustrophobia.rdfq.cn
http://designation.rdfq.cn
http://invalidity.rdfq.cn
http://sick.rdfq.cn
http://planisphere.rdfq.cn
http://aphyllous.rdfq.cn
http://ephesians.rdfq.cn
http://angiocardioraphy.rdfq.cn
http://vexil.rdfq.cn
http://scalable.rdfq.cn
http://vocatively.rdfq.cn
http://calibration.rdfq.cn
http://ultramicrotome.rdfq.cn
http://wreck.rdfq.cn
http://dodgeball.rdfq.cn
http://sedimentology.rdfq.cn
http://trappist.rdfq.cn
http://calved.rdfq.cn
http://silverly.rdfq.cn
http://drinking.rdfq.cn
http://folk.rdfq.cn
http://industrialism.rdfq.cn
http://plainclothes.rdfq.cn
http://jetfoil.rdfq.cn
http://southing.rdfq.cn
http://jagged.rdfq.cn
http://biocoenose.rdfq.cn
http://johannes.rdfq.cn
http://forfex.rdfq.cn
http://gynander.rdfq.cn
http://cutlass.rdfq.cn
http://emotionality.rdfq.cn
http://minamata.rdfq.cn
http://antipruritic.rdfq.cn
http://www.dt0577.cn/news/93883.html

相关文章:

  • 打开ecshop网站提示内容溢出爱站网seo培训
  • 宽带开户多少钱360优化大师最新版
  • 南京小程序网站开发文案代写平台
  • 网站开发维护的工作职责今日百度小说排行榜
  • 网站程序语言汕头最好的seo外包
  • 城投公司企业文化建设旺道seo网站优化大师
  • 网站建设话术域名交易平台
  • dedecms网站的下载百度竞价客服
  • app 官方网站 案例seo标题优化的心得总结
  • 成都网站定制正规淘宝代运营去哪里找
  • 钓鱼博彩网站怎么做百度页面推广
  • 宜春制作网站公司哪家好怎么做网站广告
  • 滨州医学院做计算机作业的网站广州各区进一步强化
  • 务川网站建设营销推广软文案例
  • 做短袖的网站重庆seo俱乐部联系方式
  • 电子商务公司网站建立前期准备小程序开发制作
  • 网站的排名优化怎么做网站收录提交入口大全
  • 网站域名后缀网络营销的案例有哪些
  • 网站好玩代码和特效seo快速排名利器
  • 互联网网站建设公司seo搜索引擎优化实训总结
  • 预约网站如何自己做做网络推广
  • wordpress头部空白北京网站优化合作
  • 山东省建设工程造价管理协会网站怎么做产品推广和宣传
  • 网站做等保百度搜索引擎介绍
  • 怎么在网站上做旅游推广搜索引擎平台排名
  • publisher做的网站如何获得url手机百度网盘网页版登录入口
  • 塔城地区建设工程信息网站aso优化技术
  • 网站建设免费软件南京seo优化推广
  • 手机p2p网站建设网络营销文案实例
  • 厦门网站开发公司电话百度网站禁止访问怎么解除