3

jxlを使用して.xlsファイルを読み取ります。以下は、xlsファイルを解析するためのコードです。正常に動作しますが、問題は、日付フィールドを挿入すると、年の下2桁しか読み取れないことです。たとえば、2088年12月13日と1988年12月13日を入れた場合。どちらも12/13/88と表示されます。この問題の解決策はありますか?Excelセルをフォーマットしたり、現在のコードを変更したりして、何かできますか?

ArrayList listDet=new ArrayList();
    HashMap mapDet=null;
    HashMap primeDetails=new HashMap();

    WorkbookSettings ws = null;
    Workbook workbook = null;
    Sheet s = null;
    Cell rowData[] = null;
    int rowCount = '0';
    int columnCount1 = '0';
    int columnCount = '0';
    DateCell dc = null;
    int totalSheet = 0;

    try {
        ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        ws.setCellValidationDisabled(true);

        workbook = Workbook.getWorkbook(fileInputStream, ws);

        totalSheet = workbook.getNumberOfSheets();
        //Getting Default Sheet i.e. 0
        s = workbook.getSheet(0);

        //Total Total No Of Rows in Sheet, will return you no of rows that are occupied with some data
        System.out.println("Total Rows inside Sheet:" + s.getRows());
        rowCount = s.getRows();

        //Total Total No Of Columns in Sheet
        System.out.println("Total Column inside Sheet:" + s.getColumns());
        columnCount = s.getColumns();


        //Reading Individual Row Content
        for (int i = 0; i <rowCount ; i++) {
            //Get Individual Row
            rowData = s.getRow(i);
                mapDet=new HashMap();
                mapDet.put("col1",rowData[0].getContents());
                mapDet.put("col2",rowData[1].getContents());
                mapDet.put("col3",rowData[2].getContents());
                mapDet.put("col4",rowData[3].getContents());
                mapDet.put("col5",rowData[4].getContents());
                mapDet.put("col6",rowData[5].getContents());
                mapDet.put("col7",rowData[6].getContents());
                mapDet.put("col8",rowData[7].getContents());
                mapDet.put("col9",rowData[8].getContents());

                listDet.add(mapDet);



    }
        workbook.close();           
    } catch (IOException e) {
        e.printStackTrace();
    } catch (BiffException e) {
        e.printStackTrace();
    }
    catch(Exception exp)
    {

        exp.printStackTrace();
    }
4

1 に答える 1

4

javadocs/2_6_10/docs/jxl/Cellによると、メソッド: getContents()は、 「このセルの内容を文字列として返すクイックで汚い関数」です。

getContents()では書式設定を完全に制御できないため、独自の関数を定義して、書式設定を制御することができますCellType.DATE。例えば:

 private String getContents(Cell cell) {
    DateCell dCell=null;
    if(cell.getType() == CellType.DATE)
    {
      dCell = (DateCell)cell;
      // System.out.println("Value of Date Cell is: " + dCell.getDate()); 
      // ==> Value of Date Cell is: Thu Apr 22 02:00:00 CEST 2088
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
      // System.out.println(sdf.format(dCell.getDate()));
      // ==> 2088-04-22   
      return sdf.format(dCell.getDate());
    }
    // possibly manage other types of cell in here if needed for your goals   
    // read more: http://www.quicklyjava.com/reading-excel-file-in-java-datatypes/#ixzz2fYIkHdZP 
    return cell.getContents();
}

次に、独自のプライベート関数を呼び出すようにコードを変更するだけです:

mapDet.put("col1", getContents(rowData[0]));
mapDet.put("col2", getContents(rowData[1]));
mapDet.put("col3", getContents(rowData[2]));
mapDet.put("col4", getContents(rowData[3]));
mapDet.put("col5", getContents(rowData[4]));
mapDet.put("col6", getContents(rowData[5]));
mapDet.put("col7", getContents(rowData[6]));
mapDet.put("col8", getContents(rowData[7]));
mapDet.put("col9", getContents(rowData[8]));

それ以外の

mapDet.put("col1",rowData[0].getContents());
...
mapDet.put("col9",rowData[8].getContents());
于 2013-09-23T20:37:29.770 に答える