zoukankan      html  css  js  c++  java
  • CSV文件转EXCEl(java)

    package main;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.List;
    import java.util.Scanner;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;

    public class test{
    static String dir="";
    public static void main(String[] args) {
    File directory = new File("");
    System.out.println("读取当面目录:"+directory.getAbsolutePath());
    System.out.println("使用说明:请将csv文件明明为:'评价数据.csv',生成excel文件为:result.xls");
    dir=directory.getAbsolutePath();
    makeExcel();
    Scanner scanner = new Scanner(System.in);

    String in = scanner.nextLine();

    }
    public void readcsv(){
    try {
    BufferedReader reader = new BufferedReader(new FileReader("D:\Downloads\评价数据.csv"));//换成你的文件名
    reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉
    String line = null;
    while((line=reader.readLine())!=null){
    String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分

    String last = item[1];//这就是你要的数据了
    //int value = Integer.parseInt(last);//如果是数值,可以转化为数值
    System.out.println("haha:"+last);
    String temp="";
    try{
    temp=item[2];
    }catch(Exception ex){

    }
    if(true){
    System.out.println("haha:"+last+" "+temp);
    }


    }


    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    /**
    * 将数据写入到excel中
    */
    public static void makeExcel() {

    //第一步,创建一个workbook对应一个excel文件
    HSSFWorkbook workbook = new HSSFWorkbook();
    //第二部,在workbook中创建一个sheet对应excel中的sheet
    HSSFSheet sheet = workbook.createSheet("病例日期表");
    //第三部,在sheet表中添加表头第0行,老版本的poi对sheet的行列有限制
    HSSFRow row = sheet.createRow(0);
    //第四步,创建单元格,设置表头
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("省份");
    cell = row.createCell(1);
    cell.setCellValue("职场代码柜面代码");
    cell = row.createCell(2);
    cell.setCellValue("设备评分");
    cell = row.createCell(3);
    cell.setCellValue("服务评分");
    cell = row.createCell(4);
    cell.setCellValue("系统评分");
    cell = row.createCell(5);
    cell.setCellValue("网络评分");
    cell = row.createCell(6);
    cell.setCellValue("更多反馈");
    cell = row.createCell(7);
    cell.setCellValue("评价时间");


    try {
    DataInputStream in=new DataInputStream(new FileInputStream("D:\Downloads\评价数据.csv"));
    BufferedReader reader=new BufferedReader(new InputStreamReader(in,"utf-8"));
    //BufferedReader reader = new BufferedReader(new FileReader("D:\Downloads\1.csv"));//换成你的文件名
    reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉
    String line = null;
    for(int i=0;(line=reader.readLine())!=null;i++){

    String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分
    String num = item[1];//这就是你要的数据了
    String equip_score="0";
    String network_score="0";
    String service_score="0";
    String system_score="0";
    String msg="NULL";
    String update_datetime="";
    try{
    equip_score=item[2];
    network_score=item[3];
    service_score=item[4];
    system_score=item[5];
    update_datetime=item[7];
    msg=item[6];
    }catch(Exception ex){

    }

    HSSFRow row1 = sheet.createRow(i + 1);
    row1.createCell(0).setCellValue(item[0]);
    //去除前面9
    if(num.substring(0,1).equals("9")){
    num=num.substring(1);
    }
    String sql="insert into [dbo].[evaluate]([quarter],[num],[equip_score],[network_score],[service_score],[system_score],[msg],[update_datetime])"
    + "VALUES(3,'"+num+"',"+equip_score+","+network_score+","+service_score+","+system_score+",'"+msg+"','"+update_datetime+"')";
    System.out.println(sql);
    AddUser(sql);
    row1.createCell(1).setCellValue(num);
    row1.createCell(2).setCellValue(equip_score);
    row1.createCell(3).setCellValue(network_score);
    row1.createCell(4).setCellValue(service_score);
    row1.createCell(5).setCellValue(system_score);
    row1.createCell(6).setCellValue(msg);
    row1.createCell(7).setCellValue(update_datetime);
    }


    } catch (Exception e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
    }

    //将文件保存到指定的位置
    try {
    FileOutputStream fos = new FileOutputStream(dir+"\result.xls");
    workbook.write(fos);
    System.out.println("写入成功");
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public static int AddUser(String sql) {
    Connection con = null;
    PreparedStatement psta = null;
    ResultSet rs = null;
    int countyid = 0;
    int cityid = 0;
    int grade = 0;
    try {
    con = database.getConnection();
    psta = con.prepareStatement(sql);
    psta.executeUpdate();
    } catch (Exception e) {
    System.out.println("error:插入数据错误:" + e.getMessage());
    return 0;
    } finally {
    try {
    database.close(rs, psta, con);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return 1;
    }
    }

  • 相关阅读:
    互联网秒杀设计
    生产者消费者模式实现
    Ping CAP CTO、Codis作者谈redis分布式解决方案和分布式KV存储
    VIM使用学习笔记 : 按键说明
    Cookie的有效访问路径
    简单的Cookie记录浏览记录案例
    认识Cookie和状态管理
    Java异常
    Java接口基础
    String常用方法
  • 原文地址:https://www.cnblogs.com/changhuiming/p/11024393.html
Copyright © 2011-2022 走看看