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;
}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值