csv转换为excel

将CSV文件转成Excel文件和获得Excel字段值的工具类
import java.io.*;

import org.apache.poi.hssf.usermodel.*;

/**
* This class provides utility for operating excel file
* @version 1.0
*/
public class ExcelUtil
{

/**
* transform csv file into excel file
*
* @param csv name of a csv file that will be transformed
* @param excel name of a excel file that will store the transformed file
*
* @throws IOException
*/
public final static void CsvToExcel(String csv, String excel) throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");

BufferedReader r = null;

try
{
r = new BufferedReader(new FileReader(csv));

int i = 0;

while (true)
{
String ln = r.readLine();

if (ln == null)
break;

HSSFRow row = sheet.createRow((short) i++);
int j = 0;

for (CSVTokenizer it = new CSVTokenizer(ln); it.hasMoreTokens();)
{
String val = it.nextToken();

HSSFCell cell = row.createCell((short) j++);
cell.setCellValue(val);
}
}
}
finally
{
if (r != null)
r.close();
}

FileOutputStream fileOut = null;

try
{
fileOut = new FileOutputStream(excel);
wb.write(fileOut);
}
finally
{
if (fileOut != null)
fileOut.close();
}
}



public final static Object getCellValue(HSSFCell cell)
{
if (cell != null)
{
int t = cell.getCellType();

if (t == HSSFCell.CELL_TYPE_NUMERIC)
{
int f = cell.getCellStyle().getDataFormat();
//if (f >= 0x0A && f <= 0x16) // Datetime format
if (((f >= 14 && f <= 17) || (f >= 164 && f <= 168) || (f >= 172 && f <= 177) || f == 22))
return (java.util.Date) cell.getDateCellValue();

double d = cell.getNumericCellValue();
if (d == Math.floor(d))
return new Long((long) d);

return new Double(d);
}
else if (t == HSSFCell.CELL_TYPE_STRING)
return cell.getStringCellValue();
}

return null;
}



/**
*
*
* @param args
*
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
CsvToExcel("x.csv", "x.xls");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值