2016-07-04 15 views
1

私はuser defined data formatsと思われるExcelを読んでいます。 例えば、データの形式は:"yyyy"E、ちょうどE.POI-XSSF:ユーザー定義のデータ形式

今、このフォーマットは、内蔵、利用可能なフォーマットの一部ではない文字が続くyyyy形式で日付を表示します。

したがって、これをのDataFormatとして設定しようとすると、動作しません。

最初にブックを読み、このブックからDataFormatを作成します。

XSSFWorkbook wb = new XSSFWorkbook(excelToRead); 
XSSFDataFormat df = wb.createDataFormat(); 

dfオブジェクトは現在(200まで印刷して確認)にもuser defined data formatsを持っています。 今、私はこのような新しいスタイルで、同じブックに新しいセルを作成しよう:

XSSFCell cellTemp = row.createCell(0); 
XSSFCellStyle styleTemp = wb.createCellStyle(); 
styleTemp.setDataFormat(df.getFormat(cell.getCellStyle().getDataFormatString())); 
cellTemp.setCellStyle(styleTemp); 
cellTemp.setCellValue(cell.getStringCellValue()); 
System.out.print(formatter.formatCellValue(cellTemp)+" "); 

しかし、フォーマッタは私に正しい文字列値を与えるものではありません、代わりにそれは私の基礎となる整数値を与えます(フォーマットが認識されない場合は、デフォルトであると思われます)。

XSSFでユーザー定義の形式を使用する正しい方法は何ですか?

UPDATE:あなたが達成しようとしているまさに私のために本当に明確ではないyyyy"E"またはyyyy"A"

答えて

0

、私はyyyyのような他のユーザ定義されたフォーマットを使用することだと思わ0.0%ではなく。しかし、DataFormatterが正しくフォーマットされていない場合は、 CellFormatterから派生したクラスを使用することもできます。

例:実際に働いていた

import java.io.*; 

import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.ss.format.*; 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 

import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import java.awt.Desktop; 
import java.util.Date; 

class CustomDateFormatTest { 

public static void main(String[] args) { 
    try { 

    Workbook wb = new XSSFWorkbook(); 
    Sheet sheet = wb.createSheet("Sheet1"); 
    Row row = sheet.createRow(0); 
    Cell cell = row.createCell(0); 

    cell.setCellValue(new Date()); 

    DataFormat format = wb.createDataFormat(); 
    CellStyle cellstyle = wb.createCellStyle(); 
    cellstyle.setDataFormat(format.getFormat("yyyy\"E\"")); 

    cell.setCellStyle(cellstyle); 

    OutputStream out = new FileOutputStream("CustomDateFormatTest.xlsx"); 
    wb.write(out); 
    wb.close(); 

    System.out.println("Done"); 
    File outputfile = new File("CustomDateFormatTest.xlsx"); 
    Desktop.getDesktop().open(outputfile); 

    InputStream inp = new FileInputStream("CustomDateFormatTest.xlsx"); 
    wb = WorkbookFactory.create(inp); 

    sheet = wb.getSheetAt(0); 
    row = sheet.getRow(0); 
    cell = row.getCell(0); 

    //This will not format properly 
    DataFormatter dataformatter = new DataFormatter(); 
    System.out.println(dataformatter.formatCellValue(cell)); 

    //This will format properly 
    String formatstring = cell.getCellStyle().getDataFormatString(); 
    System.out.println(formatstring); 
    CellDateFormatter celldateformatter = new CellDateFormatter(formatstring); 
    //For using CellDateFormatter we need to know that the cell value is a Date. 
    System.out.println(celldateformatter.format(cell.getDateCellValue())); 


    } catch (InvalidFormatException ifex) { 
    } catch (FileNotFoundException fnfex) { 
    } catch (IOException ioex) { 
    } 
} 
} 
+0

!ありがとうございました...他のユーザーフォーマットがなぜ機能していたのか分かりませんが、これは(yyyy "E")1つではありませんでしたか? – gaurav5430

関連する問題