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;
}
}
CSV文件转EXCEl(java)的更多相关文章
- javascript导出csv文件(excel)
这里贴出JavaScript导出csv文件(excel)的代码. /** * 导出excel * @param {Object} title 标题列key-val * @param {Object} ...
- java导出csv文件使用Excel打开乱码问题
写一个csv文件,发现使用 notpad++ 打开是没有问题的,但是使用 Excel 打开之后显示乱码 刚开始的代码是这样子的: ByteArrayOutputStream os = new Byte ...
- csv 文件用Excel打开乱码
最近在做一个上传的功能,记录下自己的感受. 刚开始我用的是excel上传:但是发现客户服务器有用64位的,使用的时候程序会报错: 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” ...
- C# .csv文件转为Excel格式;Excel格式转换为.csv
using System; using System.Diagnostics; using System.IO; using System.Reflection; using System.Windo ...
- 如何解决PHP生成UTF-8编码的CSV文件用Excel打开乱码的问题
为了识别 Unicode 文件,Microsoft 建议所有的 Unicode 文件应该以 ZERO WIDTH NOBREAK SPACE字符开头.这作为一个”特征符”或”字节顺序标记(byte-o ...
- Python 读取csv文件到excel
朋友问我如何通过python把csv格式的文件另存为xls文件,自己想了想通过读取csv文件然后再保存到xls文件中即可,也许还有其他简单的方法,但这里也为了练习python语法及其他知识,所以采用了 ...
- PHP 导出 CSV 文件用 Excel 打开出现中文乱码
本篇文章由:http://xinpure.com/php-export-csv-file-opened-by-excel-appear-garbled/ 乱码情况 写了一段导出 CSV 文件的代码,可 ...
- python读取csv文件、excel文件并封装成dict类型的list,直接看代码
# coding=UTF-8import csvimport xlrd class ReaderFile(): """ 读取csv文件 filePath:文件路径 &qu ...
- 解决CSV文件用Excel打开乱码问题
这篇文章适合有一定编码基础的人看,纯手动解决乱码问题请参见: 转码保存后,重新打开即可. 转码操作如下: 编辑器->另存为->ASCII码格式文件/UTF-8含BOM格式->保存. ...
随机推荐
- cogs 48. [NOIP2007] 字符串的展开
48. [NOIP2007] 字符串的展开 ★☆ 输入文件:expand.in 输出文件:expand.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 在初赛普 ...
- 必测的支付漏洞(一)——使用fiddler篡改支付金额
互联网产品中常会遇到支付功能,测试人员测试这部分功能时一定要重视,因为如果这部分出现了较严重的bug,将会给公司带来不小的经济损失!如果你测出了问题领导也一定会高兴的!因此测试优先级很高,但具有一定难 ...
- SaltStack学习笔记之安装zabbix_agentd(jinja和pillar)
一.环境说明 机器 IP 主机名 Master 192.168.0.23 minion.saltstack.com Minion 192.168.0.35 minion-node2.saltstack ...
- Kafka单机Windows环境搭建
Kafka单机Windows环境搭建 1,安装jdk1.8:安装目录不能有中文空格: 2,下载zookeeper,https://mirrors.cnnic.cn/apache/zookeeper/z ...
- C#如何把写好的类编译成dll文件
1 新建一个类库项目 2 直接改写这个Class1.cs文件 3 记得要添加Windows.Forms引用 4 我直接把在别的项目中做好的cs文件搞到这里来,连文件名也改了(FilesDi ...
- NYOJ 55 懒省事的小明(哈弗曼树)
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 小明非常想吃果子,正好果园果子熟了. 在果园里,小明已经将全部的果子打了下来,并且按果子的不 ...
- Hibernate学习笔记(六) — Hibernate的二级缓存
我们知道hibernate的一级缓存是将数据缓存到了session中从而降低与数据库的交互.那么二级缓存呢? 一.应用场合 比方.在12306购票时.须要选择出发地与目的地,假设每点一次都与数据库交互 ...
- [转] CVonline: Image Databases
转自:CVonline by Robert Fisher 图像数据库 Index by Topic Action Databases Biological/Medical Face Databases ...
- nginxserver报403 forbidden错误的解决的方法
改动nginx.config文件内容: location / { #root html; root D:\java; ...
- 二分lower_bound()与upper_bound()的运用
<span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit ...