通过poi将csv导成excel

package com.tw.poi; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; 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; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import au.com.bytecode.opencsv.CSVWriter; /** *

通过poi将csv导成excel,所需的包: * opencsv-1.8.jar * poi-2.5.1-final-20040804.jar * poi-contrib-2.5.1-final-20040804.jar * poi-scratchpad-2.5.1-final-20040804.jar * 下载地址:http://www.apache.org/dyn/closer.cgi/jakarta/poi/ * * 此类待完善 *

* @author tw * */ public class PoiCsvUtils { /** *

通过poi将csv导成excel

* @author tw 2009-07-16 * */ public static void CSVexcel(){ try{ String xlsPath = "e:/workbook.xls"; String csvFilePath = "e:/workbook.csv"; CSVWriter writer = null; File tempFile = null; FileWriter fwriter = null; // 写数据 try{ tempFile = new File(csvFilePath); fwriter = new FileWriter(tempFile); writer = new CSVWriter(fwriter); }catch(IOException ioex){ ioex.printStackTrace(); } /*读取Excel文件时,首先生成一个POIFSFileSystem对象, * 由POIFSFileSystem对象构造一个HSSFWorkbook, * 该HSSFWorkbook对象就代表了Excel文档*/ POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(xlsPath)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = null; HSSFCell cell = null; String cellStr = ""; //循环读取行与列的值,并将值写入CSV文件 for(int i=0;i<=sheet.getLastRowNum();i++){ row = sheet.getRow(i); String[] cellArray = new String[row.getLastCellNum()]; for(int j=0;j

你可能感兴趣的:(java,csv,excel,string,null,exception,class)