2013-12-12 180 views
0

我正在嘗試使用XLS2CSV將Excel文件轉換爲csv文本文件。我稍微修改了現有的類,將PrintStream更改爲Writer,這樣我就可以使用FileWriter寫入文件。FileWriter意外停止

無論如何,它寫入到System.out.PrintStream罰款,一切都顯示出來。但是當我打開文件時,最後幾行文件丟失了。我的第一個假設是應用程序在完成寫入之前關閉了連接,但我使用了無限循環來嘗試和測試,但即使將應用程序打開,它也執行相同的操作。

有沒有人有任何想法,爲什麼這將打印PrintStream罰款,但不寫入文件?我將FileWriter更改爲CSVWriter,它似乎寫入較少的數據,所以我認爲它確實與連接關閉有關,但我對輸入和輸出相當陌生,所以不勝感激。

這裏是一個功能例子(你需要修改文件名輸入和輸出)

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.Writer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Iterator; 

import org.apache.poi.hssf.usermodel.HSSFDateUtil; 
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellValue; 
import org.apache.poi.ss.usermodel.FormulaEvaluator; 
import org.apache.poi.ss.usermodel.Row; 

public class XLS2CSV { 
private InputStream in; 
private Writer out; 
private DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy"); 

public XLS2CSV(InputStream in, Writer out) { 
    if (null == in) { 
     throw new IllegalArgumentException("in cannot be null."); 
    } 
    if (null == out) { 
     throw new IllegalArgumentException("out cannot be null."); 
    } 
    this.in = in; 
    this.out = out; 
} 

public void process() throws IOException { 
    process(in); 
} 

private void process(InputStream in) throws IOException { 
    HSSFWorkbook w = new HSSFWorkbook(in); 
    int numSheets = w.getNumberOfSheets(); 
    for (int i = 0; i < numSheets; i++) { 
     HSSFSheet sheet = w.getSheetAt(i); 
     int lastRow = 0; 
     for (Iterator rows = sheet.rowIterator(); rows.hasNext();) { 
      Row row = (Row) rows.next(); 
      int lastCol = 0; 
      for (Iterator cells = row.cellIterator(); cells.hasNext();) { 
       Cell cell = (Cell) cells.next(); 
       String cellValue = ""; 
       switch (cell.getCellType()) { 
       case Cell.CELL_TYPE_FORMULA: 
        FormulaEvaluator fe = new HSSFFormulaEvaluator(w); 
        CellValue v = fe.evaluate(cell); 
        switch (v.getCellType()) { 
        case Cell.CELL_TYPE_BOOLEAN: 
         cellValue = String.valueOf(v.getBooleanValue()); 
         break; 
        case Cell.CELL_TYPE_NUMERIC: 
         cellValue = String.valueOf(v.getNumberValue()); 
         break; 
        case Cell.CELL_TYPE_STRING: 
         cellValue = String.valueOf(v.getStringValue()); 
         break; 
        case Cell.CELL_TYPE_BLANK: 
         break; 
        case Cell.CELL_TYPE_ERROR: 
         break; 
         // CELL_TYPE_FORMULA will never happen 
        case Cell.CELL_TYPE_FORMULA: 
         break; 
        } 
        break; 
       case Cell.CELL_TYPE_NUMERIC: 
        if (HSSFDateUtil.isCellDateFormatted(cell)) { 
         Date date = cell.getDateCellValue(); 
         cellValue = dateFormat.format(date); 
        } else { 
         cellValue = String.valueOf(cell); 
        } 
        break; 
       default: 
        cellValue = String.valueOf(cell); 
       } 
       int cellIndex = cell.getColumnIndex(); 
       while (lastCol < cellIndex) { 
        System.out.print(","); 
        out.append(","); 
        lastCol++; 
       } 
       System.out.print(cellValue); 
       out.append(cellValue); 
      } 
      while (lastRow <= row.getRowNum()) { 
       System.out.println(); 
       out.append('\n'); 
       lastRow++; 
      } 
     } 
    } 
} 

public void setDateFormat(DateFormat dateFormat) { 
    if (null == dateFormat) { 
     throw new IllegalArgumentException("dateFormat cannot be null."); 
    } 
    this.dateFormat = dateFormat; 
} 

public DateFormat getDateFormat() { 
    return dateFormat; 
} 

public static void process(File file, Writer out) throws IOException { 
    FileInputStream fis = null; 
    try { 
     fis = new FileInputStream(file); 
     new XLS2CSV(new BufferedInputStream(fis), out).process(); 
    } finally { 
     if (null != fis) { 
      try { 
       fis.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

public static void main(String[] args) { 

    File xlsFile = new File("C:/Documents and Settings/berlgeof/Desktop/Commit Dates 12-10-2013.xls"); 
    if (!xlsFile.exists()) { 
     System.err.println("Not found or not a file: " + xlsFile.getPath()); 
     return; 
    } 

    Writer writer = null; 
    try { 
     writer = new FileWriter("lib/files/converted/Temp Commit Data.csv"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    XLS2CSV xls2csv = null; 
    try { 
     xls2csv = new XLS2CSV(new FileInputStream(xlsFile), writer); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    try { 
     xls2csv.process(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

//  while(true) {} // Let me close the application 


} 

回答

4

你不關閉的作家,這意味着在緩衝區中仍然存在的數據。您應該在finally塊中關閉它(以及所有其他流等),或者如果您使用的是Java 7,請使用try-with-resources語句。

另請注意,您目前「處理」異常幾乎無視它們並繼續 - 這意味着在一件事失敗後,你幾乎肯定會再次失敗。翻出那些try/catch塊,並讓異常傳播起來。 (聲明你的main方法可以拋出IOException。)

+0

拍攝!我總是忘記沖洗和/或關閉我的流。通常Eclipse會警告我永遠不會關閉,有趣的是它不在這裏。感謝您的解決方案,我將解決我的異常處理問題,我主要關心如何解決其他問題,但現在可以解決錯誤處理問題。再次感謝。 – Geoff