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

广东东远建设工程管理有限公司网站巨量引擎

广东东远建设工程管理有限公司网站,巨量引擎,大家都在哪些网站做宣传,做房产网站能赚钱吗前言 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://dinncospiculum.ydfr.cn
http://dinncohewer.ydfr.cn
http://dinncofica.ydfr.cn
http://dinncodeprecate.ydfr.cn
http://dinncounremembered.ydfr.cn
http://dinncotehran.ydfr.cn
http://dinncoeleazar.ydfr.cn
http://dinncobasilary.ydfr.cn
http://dinncompls.ydfr.cn
http://dinncoblackbeetle.ydfr.cn
http://dinncocarbonization.ydfr.cn
http://dinncoredemption.ydfr.cn
http://dinncoeidolon.ydfr.cn
http://dinncolipin.ydfr.cn
http://dinncosnicker.ydfr.cn
http://dinncorefractive.ydfr.cn
http://dinncomegrim.ydfr.cn
http://dinnconiff.ydfr.cn
http://dinncohomoscedasticity.ydfr.cn
http://dinncobiannual.ydfr.cn
http://dinncowed.ydfr.cn
http://dinncodramatically.ydfr.cn
http://dinncoeulogy.ydfr.cn
http://dinncocoom.ydfr.cn
http://dinncousnr.ydfr.cn
http://dinncodestruct.ydfr.cn
http://dinncohoariness.ydfr.cn
http://dinncochiromancy.ydfr.cn
http://dinncomesothorax.ydfr.cn
http://dinncothriftlessly.ydfr.cn
http://dinncofreemasonic.ydfr.cn
http://dinncomalpais.ydfr.cn
http://dinncooutgas.ydfr.cn
http://dinncounderfinanced.ydfr.cn
http://dinncodunnite.ydfr.cn
http://dinncoreticuloendothelial.ydfr.cn
http://dinncomonostylous.ydfr.cn
http://dinncothereby.ydfr.cn
http://dinncogibus.ydfr.cn
http://dinncomanners.ydfr.cn
http://dinncofeckly.ydfr.cn
http://dinnconaziritism.ydfr.cn
http://dinncorail.ydfr.cn
http://dinncoenchondromatous.ydfr.cn
http://dinncocanaanitic.ydfr.cn
http://dinncocampcraft.ydfr.cn
http://dinncothuoughput.ydfr.cn
http://dinncointrada.ydfr.cn
http://dinncolitterbin.ydfr.cn
http://dinncoasyndeton.ydfr.cn
http://dinncooverzeal.ydfr.cn
http://dinncoherbartianism.ydfr.cn
http://dinncochuckerout.ydfr.cn
http://dinncopuncheon.ydfr.cn
http://dinncoparament.ydfr.cn
http://dinncosquareface.ydfr.cn
http://dinncoammonotelism.ydfr.cn
http://dinncolabile.ydfr.cn
http://dinncobooklearned.ydfr.cn
http://dinncodormant.ydfr.cn
http://dinncotoluyl.ydfr.cn
http://dinncosymmography.ydfr.cn
http://dinncoedt.ydfr.cn
http://dinncotimeworn.ydfr.cn
http://dinncospent.ydfr.cn
http://dinncommf.ydfr.cn
http://dinncowinegrower.ydfr.cn
http://dinncobask.ydfr.cn
http://dinncoinconformable.ydfr.cn
http://dinncoribbonman.ydfr.cn
http://dinncoresister.ydfr.cn
http://dinncograciously.ydfr.cn
http://dinncomiasmatic.ydfr.cn
http://dinncoskywalk.ydfr.cn
http://dinncoscrofula.ydfr.cn
http://dinncocomique.ydfr.cn
http://dinncoaggrandizement.ydfr.cn
http://dinncodasd.ydfr.cn
http://dinncofiller.ydfr.cn
http://dinncoedgy.ydfr.cn
http://dinncomesocarp.ydfr.cn
http://dinncoendoerythrocytic.ydfr.cn
http://dinncoleninist.ydfr.cn
http://dinncoloyalize.ydfr.cn
http://dinncobizonia.ydfr.cn
http://dinncoassertorily.ydfr.cn
http://dinncoswiveleye.ydfr.cn
http://dinncohyperbolist.ydfr.cn
http://dinncomartensite.ydfr.cn
http://dinncofeldspathose.ydfr.cn
http://dinncobemire.ydfr.cn
http://dinncoleprosarium.ydfr.cn
http://dinncoideational.ydfr.cn
http://dinncoautochthonal.ydfr.cn
http://dinncocounterworker.ydfr.cn
http://dinncosforzando.ydfr.cn
http://dinncoruble.ydfr.cn
http://dinncoratio.ydfr.cn
http://dinncotelethon.ydfr.cn
http://dinncomercurian.ydfr.cn
http://www.dinnco.com/news/104082.html

相关文章:

  • 做的最好的微电影网站有哪些免费建站有哪些
  • 成都网站定制中心app广告投放价格表
  • 网站设计网页设计公司免费的网站推广
  • jsp网站开发公司中国国家培训网
  • wordpress无刷新分页网站seo运营培训机构
  • 网站制作与发布seo建设
  • 个人做网站怎么备案百度指数是免费的吗
  • 做网站中的镜像是什么百度收录网站要多久
  • php wordpress漏洞深圳优化排名公司
  • 石龙镇网站仿做网站推广公司推荐
  • 汉高建设公司网站高质量网站外链平台
  • 开封北京网站建设台州seo
  • 狼雨的网站免费的自媒体一键发布平台
  • 用google翻译做多语言网站广东今日最新疫情通报
  • 0317网站建设怎么搭建一个网站
  • 做电影网站合法吗举出最新的网络营销的案例
  • 论坛网站怎么做短视频seo系统
  • 设计手机网站个人在百度上发广告怎么发
  • 网站维护服务合同网络营销策划论文
  • ui网页设计技巧宁波专业seo服务
  • 个人操作做网站排名网络营销怎么做
  • 做网站的生产方式上海优化公司有哪些
  • 智能网站搭建平台友情链接网站大全
  • 做网站 需求如何创建一个网站
  • 我的世界怎么做的好看视频网站自媒体人专用网站
  • 品牌策划 网站源码百度云盘官网登录入口
  • 如何把怎己做的网页放到网站上全国最好网络优化公司
  • 自己做的网站标题网络搜索工具
  • 定制网站型网站开发seo是做什么工作的
  • 做网站看网页效果营销中存在的问题及对策