1. package com.tw.poi;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;  
  9. import org.apache.poi.hssf.usermodel.HSSFRow;  
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;  
  13.  
  14. import au.com.bytecode.opencsv.CSVWriter;  
  15.  
  16.  
  17. /**  
  18.  * <p>通过poi将csv导成excel,所需的包:  
  19.  *  opencsv-1.8.jar  
  20.  *  poi-2.5.1-final-20040804.jar  
  21.  *  poi-contrib-2.5.1-final-20040804.jar  
  22.  *  poi-scratchpad-2.5.1-final-20040804.jar  
  23.  * 下载地址:http://www.apache.org/dyn/closer.cgi/jakarta/poi/  
  24.  *   
  25.  * 此类待完善  
  26.  * </p>  
  27.  * @author tw  
  28.  *  
  29.  */ 
  30. public class PoiCsvUtils {  
  31.       
  32.       
  33.     /**  
  34.      * <p>通过poi将csv导成excel</p>  
  35.      * @author tw 2009-07-16  
  36.      *  
  37.      */ 
  38.     public static void CSVexcel(){  
  39.         try{  
  40.             String xlsPath = "e:/workbook.xls";  
  41.             String csvFilePath = "e:/workbook.csv";  
  42.               
  43.             CSVWriter writer = null;  
  44.             File tempFile = null;  
  45.             FileWriter fwriter = null// 写数据  
  46.             try{  
  47.                 tempFile = new File(csvFilePath);  
  48.                 fwriter = new FileWriter(tempFile);  
  49.                 writer = new CSVWriter(fwriter);  
  50.             }catch(IOException ioex){  
  51.                 ioex.printStackTrace();  
  52.             }  
  53.             /*读取Excel文件时,首先生成一个POIFSFileSystem对象,  
  54.              * 由POIFSFileSystem对象构造一个HSSFWorkbook,  
  55.              * 该HSSFWorkbook对象就代表了Excel文档*/ 
  56.            POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(xlsPath));   
  57.            HSSFWorkbook wb = new HSSFWorkbook(fs);   
  58.            HSSFSheet sheet = wb.getSheetAt(0);   
  59.              
  60.  
  61.            HSSFRow row = null;  
  62.            HSSFCell cell = null;  
  63.            String cellStr = "";  
  64.              
  65.            //循环读取行与列的值,并将值写入CSV文件  
  66.            for(int i=0;i<=sheet.getLastRowNum();i++){  
  67.                row = sheet.getRow(i);  
  68.                String[] cellArray = new String[row.getLastCellNum()];  
  69.                for(int j=0;j<row.getLastCellNum();j++){  
  70.                    cell = row.getCell((short) j);  
  71.                     // 判断储存格的格式  
  72.                     if (cell == null){  
  73.                         cellStr = "";  
  74.                     }else {  
  75.                         switch (cell.getCellType()) {  
  76.                             case HSSFCell.CELL_TYPE_NUMERIC://数字格式  
  77.                                 cellStr = cell.getNumericCellValue() + "";  
  78.                                 // getNumericCellValue()会回传double值,若不希望出现小数点,请自行转型为int  
  79.                                 break;  
  80.                             case HSSFCell.CELL_TYPE_STRING://字符格式  
  81.                                 cellStr = cell.getStringCellValue();  
  82.                                 break;  
  83.                                 // case HSSFCell.CELL_TYPE_FORMULA:  
  84.                                 // System.out.print(cell.getNumericCellValue());  
  85.                                 // //读出公式储存格计算後的值  
  86.                                 // //若要读出公式内容,可用cell.getCellFormula()  
  87.                                 // break;  
  88.                             default://不明的格式  
  89.                                 break;  
  90.                         }  
  91.                     }  
  92.                     System.out.println("-----row-"+i+"-----cell-"+j+"---:"+cellStr);  
  93.                     cellArray[j] = cellStr;  
  94.                }  
  95.                writer.writeNext(cellArray);// 把Excel一行记录写入CSV文件  
  96.            }  
  97.              
  98.             fwriter.flush();  
  99.             fwriter.close();  
  100.             writer.close();  
  101.         }catch(Exception e){  
  102.             e.printStackTrace();  
  103.         }  
  104.     }  
  105. }