首页 > 其他分享 >csv生成工具

csv生成工具

时间:2024-11-13 20:19:01浏览次数:3  
标签:String 生成 write bufferedWriter import 工具 csv clinicalConditon

由于经常要拉取数据库记录生成csv,所以封装了两个工具。

可以根据需求对这两个工具进行相应的改写。

一种是接受两个参数,一个是csv生成的路径,这个路径分windows和linux,windows系统要注意文件分隔符为\\而linux文件分隔符为//,比如windows下为

E:\\csv\\而Linux下为//www//zip//,但是注意这个传入的路径是包含文件名称的,当然你也可以逻辑上进行改写,举例传入路径 E:\\csv\\good\\test.csv

这样会在E盘csv文件夹下的good文件夹下生成一个test.csv。

第二个参数是实体类链表,因此这个工具的作用是读取你传入的实体类链表并生成一个csv文件到你传入的路径。

import com.nonagon.clinicaltest.entity.ClinicalConditon;


import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * @ClassName:
 * @author: flitsneak nonagon
 * @date: 2020/12/23
 */
public class CSVReader {
    //传入两个参数,一个是cpath 即生成路径,一个是实体类链表
    public static Boolean GenerateCSV(String cpath, List<ClinicalConditon> list){
        BufferedWriter bufferedWriter=null;
        FileOutputStream fileOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(cpath,true);
            bufferedWriter  = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
            bufferedWriter.write("clinicalconditionid");
            bufferedWriter.write(",");
            bufferedWriter.write("ad0");
            bufferedWriter.write(",");
            bufferedWriter.write("data0");
            bufferedWriter.write(",");
            bufferedWriter.write("ad1");
            bufferedWriter.write(",");
            bufferedWriter.write("data1");
            bufferedWriter.write(",");
            bufferedWriter.write("ad2");
            bufferedWriter.write(",");
            bufferedWriter.write("data2");
            bufferedWriter.write(",");
            bufferedWriter.write("speciesType");
            bufferedWriter.write(",");
            bufferedWriter.write("breedType");
            bufferedWriter.write(",");
            bufferedWriter.write("vitalType");
            bufferedWriter.write(",");
            bufferedWriter.write("gender");
            bufferedWriter.write(",");
            bufferedWriter.write("age");
            bufferedWriter.write(",");
            bufferedWriter.write("weight");
            bufferedWriter.write(",");
            bufferedWriter.write("petImageUrl");
            bufferedWriter.newLine();
            for(int i = 0; i < list.size(); i++ )
            {

                for (int j = 0; j < list.get(i).getClinicalDataList().size(); j++) {
                    bufferedWriter.write(String.valueOf(list.get(i).getClinicalConditonId()));
                    bufferedWriter.write(",");
                    bufferedWriter.write("0");
                    bufferedWriter.write(",");
                    bufferedWriter.write(String.valueOf(list.get(i).getClinicalDataList().get(j).getData0()));
                    bufferedWriter.write(",");
                    bufferedWriter.write("0");
                    bufferedWriter.write(",");
                    bufferedWriter.write(String.valueOf(list.get(i).getClinicalDataList().get(j).getData1()));
                    bufferedWriter.write(",");
                    bufferedWriter.write("0");
                    bufferedWriter.write(",");
                    bufferedWriter.write("25.04");
                    bufferedWriter.write(",");
                    bufferedWriter.newLine();
                }

            }
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

第二个工具是对第一个工具的改写,传入的是单个实体而不是实体类链表。

import com.nonagon.clinicaltest.entity.ClinicalConditon;
import com.nonagon.clinicaltest.entity.ClinicalData;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @author: flitsneak nonagon
 * @date: 2020/12/23
 */
public class CSVReader2 {
    //传入两个参数,一个是cpath 即生成路径,一个是实体类
    public static Boolean GenerateCSV(String cpath, ClinicalConditon clinicalCondition){
        BufferedWriter bufferedWriter=null;
        FileOutputStream fileOutputStream = null;

        try {
            fileOutputStream = new FileOutputStream(cpath,true);
            bufferedWriter  = new BufferedWriter(new OutputStreamWriter(fileOutputStream,"utf-8"));
            bufferedWriter.write("ad0");
            bufferedWriter.write(",");
            bufferedWriter.write("data0");
            bufferedWriter.write(",");
            bufferedWriter.write("ad1");
            bufferedWriter.write(",");
            bufferedWriter.write("data1");
            bufferedWriter.write(",");
            bufferedWriter.write("ad2");
            bufferedWriter.write(",");
            bufferedWriter.write("data2");
            bufferedWriter.write(",");
            bufferedWriter.write("name");
            bufferedWriter.write(",");
            bufferedWriter.write("birthday");
            bufferedWriter.write(",");
            bufferedWriter.write("weight");
            bufferedWriter.write(",");
            bufferedWriter.write("gender");
            bufferedWriter.write(",");
            bufferedWriter.write("isNeutered");
            bufferedWriter.write(",");
            bufferedWriter.write("notes");
            bufferedWriter.write(",");
            bufferedWriter.write("selectMeasuringPart");
            bufferedWriter.newLine();
         bufferedWriter.write("0");
            bufferedWriter.write(",");
            bufferedWriter.write("25");
            bufferedWriter.write(",");
            bufferedWriter.write("0");
            bufferedWriter.write(",");
            bufferedWriter.write("25");
            bufferedWriter.write(",");
            bufferedWriter.write("0");
            bufferedWriter.write(",");
            bufferedWriter.write("25");
            bufferedWriter.write(",");
            bufferedWriter.write("null");
            bufferedWriter.write(",");
            bufferedWriter.write("Invalid date");
            bufferedWriter.write(",");
            bufferedWriter.write(clinicalCondition.getWeight().toString());
            bufferedWriter.write(",");
            bufferedWriter.write(clinicalCondition.getGender());
            bufferedWriter.write(",");
            bufferedWriter.write("false");
            bufferedWriter.write(",");
            bufferedWriter.write("null");
            bufferedWriter.write(",");
            bufferedWriter.write(clinicalCondition.getVitalType());
            bufferedWriter.newLine();
                for (int i = 0; i < clinicalCondition.getClinicalDataList().size(); i++) {
                    bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getAd0()));
                    bufferedWriter.write(",");
                    bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getData0()));
                    bufferedWriter.write(",");
                    bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getAd1()));
                    bufferedWriter.write(",");
                    bufferedWriter.write(String.valueOf(clinicalCondition.getClinicalDataList().get(i).getData1()));
                    bufferedWriter.write(",");
                    bufferedWriter.write("0");
                    bufferedWriter.write(",");
                    bufferedWriter.write("25.04");
                    bufferedWriter.write(",");
                    bufferedWriter.newLine();
                }
            bufferedWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        ClinicalConditon clinicalConditon = new ClinicalConditon();
        clinicalConditon.setClinicalConditonId(134);
        clinicalConditon.setSpeciesType("ll");
        clinicalConditon.setBreedType("k");
        clinicalConditon.setVitalType("c");
        clinicalConditon.setGender("m");
        clinicalConditon.setAge(12);
        clinicalConditon.setWeight(12.3);
        clinicalConditon.setPetImageUrl("eee");
        ClinicalData clinicalData = new ClinicalData();
        clinicalData.setAd0(1);
        clinicalData.setData0(33.6);
        clinicalData.setAd1(2);
        clinicalData.setData1(35.6);
        List<ClinicalData> list = new ArrayList<>();

        list.add(clinicalData);
        clinicalConditon.setClinicalDataList(list);

        String path = "e://csv//test.csv";

        CSVReader2 csvReader2 = new CSVReader2();
        csvReader2.GenerateCSV(path,clinicalConditon);

    }
}

其实hutool工具包的生成csv的方式也很简单,但是要传入String数组,代码量上并没有精简,所以就这样用吧。

标签:String,生成,write,bufferedWriter,import,工具,csv,clinicalConditon
From: https://blog.csdn.net/nonagontech/article/details/143751455

相关文章

  • Qt | 串口调试工具实现
    点击上方"蓝字"关注我们01、QSerialPort>>>QSerialPort是Qt框架中的一个类,用于串行通信。它提供了一个简单的接口,允许开发者通过串口与外部设备(如传感器、相机、单片机等)进行数据交换。QSerialPort支持多种串口操作,可以方便地设置波特率、数据位、停止位和校验位等通......
  • 最小生成树
    最小生成树模板题:【模板】最小生成树求最小生成树的边权和。Prim这似乎是我最早学的最小生成树算法。也是忘的最早的首先注意到,由\(n\)个节点和$n-1$条边构成的连通图一定是树。那么只需要选\(n-1\)条边使图连通,求最小代价。不难发现只要保证结果不出现环就可能是......
  • 截图工具推荐-Snipaste
    ......
  • 用idea的docker插件生成镜像并推送到服务器
    准备这两个文件......
  • 比ChatGPT更酷的AI工具
    相较于寻找比ChatGPT更酷的AI工具,这听起来似乎是个挑战,因为ChatGPT已经以它强大的综合性能在AI界大名鼎鼎。然而,每个工具都有其独特的优势,特别是在特定的应用场景下,其他AI工具可能会展现出与ChatGPT不同的魅力。接下来,我将向大家介绍三款我认为字在各自领域非常酷的AI工具。......
  • 《MyBatis-Plus 代码生成器基础入门》
    1.概念介绍MyBatis-Plus是一个MyBatis的增强工具,旨在简化开发、提高效率。它在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。MyBatis-Plus提供了代码生成器功能,可以快速地根据数据库表结构生成Entity(实体类)、Mapper(映射器接口)、Service(服务层接口及其实......
  • GIS工具哪家强?五款优质GIS工具箱对比分析
    本文将为大家介绍五款功能各异的GIS工具箱,包括GISBox、QGIS、MapTiler、SagaGIS和WhiteboxGAT。每款工具箱都有其独特的功能和应用场景,能够满足不同类型的GIS任务需求。无论是数据处理、空间分析、影像处理还是可视化需求,这些工具都能为用户提供丰富的解决方案。本文将详细列出......
  • 将labelme生成的label和img合并展示
    img:label:合并达到的效果:实现代码:点击查看代码importcv2importnumpyasnpfromPILimportImagedefadd_legend(image,colors,labels,alpha=1):"""在图像的右上角添加图例,使用固定的尺寸。:paramimage:输入的BGR图像。:paramcolors:颜色......
  • 搭建知识库工具合集2025年哪些工具会爆火?
    随着企业对于知识管理和员工培训需求的日益增长,2025年的知识库工具市场将呈现出更加多样化、智能化的趋势。在众多工具中,哪些将脱颖而出成为企业的首选呢?本文将重点介绍几款备受瞩目的知识库工具。1.HelpLookAI知识库HelpLook是一款专注于为企业提供高效智能的知识管理解......
  • 无问芯穹与清华、上交联合研究团队提出合稀疏注意力方案MoA,加速长文本生成,实现最高8倍
    随着大语言模型在长文本场景下的需求不断涌现,其核心的注意力机制(AttentionMechanism)也获得了非常多的关注。注意力机制会计算一定跨度内输入文本(令牌,Token)之间的交互,从而实现对上下文的理解。随着应用的发展,高效处理更长输入的需求也随之增长[1][2],这带来了计算代价的挑战:注......