2015-11-02 102 views
0

我試圖將包含多個工作表的Excel(.xls)文件轉換爲.csv文件。代碼工作正常,但我注意到某些列的數據類型正在從時間數據類型更改爲雙數據類型。在Java中沒有日期的寫入時間格式(hh:mm:ss)

例如:如果我的輸入是00:45:20,我得到的輸出如0.006168981481481482。每個工作表都有使用時間數據類型的列。

注意:我的輸入沒有日期部分。只有時間組件在那裏。我看過幾篇與此相關的帖子,並且嘗試過。但是java代碼只打印默認日期並排除時間部分。

我覺得在case語句中必須修改某些內容才能填充時間數據類型。我想有一個通用的程序,以便每當有數據類型時,我必須以相同的格式編寫它。該代碼我使用:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Iterator; 

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.Row; 

public class exceltst 
{ 
    static void xls(File inputFile, File outputFile,int sheet_num) 
    { 
     // For storing data into CSV files 
     StringBuffer data = new StringBuffer(); 
     try 
     { 
     FileOutputStream fos = new FileOutputStream(outputFile); 

     // Get the workbook object for XLS file 
     HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile)); 
     // Get first sheet from the workbook 
     HSSFSheet sheet = workbook.getSheetAt(sheet_num); 
     Cell cell; 
     Row row; 

     // Iterate through each rows from first sheet 
     Iterator<Row> rowIterator = sheet.iterator(); 
     while (rowIterator.hasNext()) 
     { 
       row = rowIterator.next(); 
       // For each row, iterate through each columns 
       Iterator<Cell> cellIterator = row.cellIterator(); 
       while (cellIterator.hasNext()) 
       { 
         cell = cellIterator.next(); 

         switch (cell.getCellType()) 
         { 
         case Cell.CELL_TYPE_BOOLEAN: 
           data.append(cell.getBooleanCellValue() + ","); 
           break; 

         case Cell.CELL_TYPE_NUMERIC: 
           data.append(cell.getNumericCellValue() + ","); 
           break; 

         case Cell.CELL_TYPE_STRING: 
           data.append(cell.getStringCellValue() + ","); 
           break; 

         case Cell.CELL_TYPE_BLANK: 
           data.append("" + ","); 
           break; 

         default: 
           data.append(cell + ","); 
         } 


       } 
       data.append('\n'); 
     } 

     fos.write(data.toString().getBytes()); 
     fos.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
       e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
       e.printStackTrace(); 
     } 
     } 

     public static void main(String[] args) 
     { 
       File inputFile = new File("C:\\Call_Center_20150323.xls"); 
       File outputFile1 = new File("C:\\live_person.csv"); 
       xls(inputFile, outputFile1,3); 
     } 
} 

能否請你幫如何填充時間數據類型沒有日期,而不是雙輸出文件(HH:SS:毫米)?

+0

@Andreas:我看到後也是如此。但是,這裏的區別在於,我的輸入文件中沒有日期部分。只有時間部分在那裏。因此,當我應用該代碼時,它將填充默認日期並忽略時間部分。 – Vivek

+1

如果你的單元格有時間格式,那麼應用格式應該以'HH:MM:SS'格式返回一個字符串,當然假設這是所討論的格式。該字符串然後直接進入您的.csv文件。不涉及日期或時間邏輯。 – Andreas

回答

0

嘗試

if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){ 
     if (DateUtil.isCellDateFormatted(cell)) { 
      System.out.println(cell.getDateCellValue()); 
     } else { 
      System.out.println(cell.getNumericCellValue()); 
     } 
} 

有關詳細信息,你可以參考here

0

您應該在第一次創建CellStyle,然後設置這種風格爲您的時間單元。另外對於cvs文件,你不能創建一個CellStyle,你應該使用excel文件來使用單元格樣式。

對於Excel:

CellStyle style = workBook.createCellStyle(); 
style.setDataFormat(workBook.createDataFormat().getFormat("hh:mm:ss")); 
cell.setCellStyle(style); 
cell.setCellValue("16:15:11"); 

CVS的文件,你應該設置你的Cell的值作爲字符串:

data.append("16:15:11" + ","); 
相關問題