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格式->保存. ...
随机推荐
- Mycat环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/dreamcode/article/details/44307377 http://blog.csdn.net/lanonola/art ...
- openstack setup demo Enviroment
Enviroment 本文包含以下部分. Host networking Network Time Protocol (NTP) OpenStack packages SQL database NoS ...
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Sping框架的IOC特性 悲观锁、乐观锁 Spring的AOP特性
Sping框架的IOC特性 IOC(Inversion of Control):控制反转 以下以课程与老师的安排来介绍控制反转. 一个合理的课程编排系统应该围绕培训的内容为核心,而不应该以具体的培训老 ...
- io口的作用
I/O接口的作用 主机与外界交换信息称为输入/输出(I/O).主机与外界的信息交换是通过输入/输出设备进行的.一般的输入/输出设备都是机械的或机电相结合的产物,比方常规的外设有键盘.显示器.打 ...
- easyUI的treegrid添加节点(append)时间过长,设置等待(wait)遮罩效果
如题所述,在treegrid中,一次添加多个节点时,时间很长,但easyUI并无显示等待信息,让人以为是陷入了死循环,或者死机了,用户体验很差. 本来,treegrid(或者datagrid)有所谓的 ...
- commons.fileupload 文件上传
编辑jsp页面获取文件 <html> <head> <base href="<%=basePath%>"> <title> ...
- eclipse android开发,文本编辑xml文件,给控件添加ID后,R.java,不自动的问题。
直接编辑xml文件给控件添加id,不自动更新.原来的id写法:@id/et_tel 然后改写成这样:@+id/et_tel 然后就好了!操`1
- 在linux上处理base64加密和解密
http://snailwarrior.blog.51cto.com/680306/142472/ 2.从标准输入读取文件内容,base64编码并打印到标准输出 [root@localhost tes ...
- 使用Zabbix监控ZooKeeper服务的健康状态
一 应用场景描述 在目前公司的业务中,没有太多使用ZooKeeper作为协同服务的场景.但是我们将使用Codis作为Redis的集群部署方案,Codis依赖ZooKeeper来存储配置信息.所以做好Z ...