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格式->保存. ...
随机推荐
- Combinations(带for循环的DFS)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- Ubuntu清理内存命令(效果不明显)
注意:最好不要在生产环境上使用!!! 1.检查内存使用情况 watch -n 3 free -m watch -n 3 cat /proc/meminfo 2.清理 #释放页缓存 echo 1 > ...
- 联想M7400打印机加粉墨了还是显示没有粉墨
联想M7400打印机加粉墨了还是显示没有粉墨?想必有很多的办公人士发现这个难题吧.其实很简单的! 先说下打印机的原理,打印机粉墨一边用的时候系统也在计数,当我们打印机加好墨后但打印机是不知道已经加好 ...
- iframe显示滚动条
子页面通过iframe加载,出现了竖向滚动条 最后查出原因:文档申明 iframe有滚动条的页面的文档申明 <!DOCTYPE html> 改成如下就行了 <!DOCTYPE HTM ...
- CSS聊天气泡
概述 谷歌效果图如下: ie效果图如下: 完整代码 <!DOCTYPE html> <html> <head> <meta charset="gbk ...
- mybatis resultmap标签type属性什么意思
mybatis resultmap标签type属性什么意思? :就表示被转换的对象啊,被转换成object的类型啊 <resultMap id="BaseResultMap" ...
- 又见古老的Typosquatting攻击:这次入侵Npm窃取开发者身份凭证
有些攻击方式虽然听起来很幼稚,但有时候却也可以生效,比如typosquatting攻击——我们上次看到这种攻击是在去年6月份,这本身也是种很古老的攻击方式. 所谓的typosquatting,主要是通 ...
- Centos6.4安装Zimbra初步教程
环境: 1.centos6.4*64位版本 2.主机最好内存设置在2G以上,要不安装的时候卡死你 3.下载最新的开源的Zimbra安装包,下载zcs-8.0.4_GA_5737.RHEL6_64.20 ...
- 小白学开发(iOS)OC_ 字符串的获取 (2015-08-11)
// // main.m // 字符串的获取 // // Created by admin on 15/8/13. // Copyright (c) 2015年 admin. All righ ...
- 5 shell命令之tr
这是一个奇妙的命令. tr的全拼就是translate,即翻译.有趣的是我们能够制定规则进行翻译.使用方法例如以下: tr [option] set1 [set2] tr从标准输入接受输入.然后将结 ...