2013-09-25 5 views
0

데스크톱 응용 프로그램에서 작업 중이며 Excel 파일 가져 오기에 약간의 문제가 있습니다.Excel 파일 가져 오기 - 데이터 형식

모든 것이 좋지만 Excel 시트에서 데이터를 읽을 때 모든 숫자와 알파벳을 읽지 않습니다. 예를 들어, 열의 첫 번째 셀이 숫자이면 해당 열에서 Alphabets을 읽지 않습니다. 해당 열의 유형을 텍스트로 수동 변경하면 모든 것이 좋습니다.

다음은 Excel 시트 데이터를 가져 오기위한 샘플 코드입니다.

아이디어가 있으십니까?

public static DataSet exceldata(string filelocation) 
{ 
    DataSet ds = new DataSet(); 

    OleDbCommand excelCommand = new OleDbCommand(); 
    OleDbDataAdapter excelDataAdapter = new OleDbDataAdapter(); 

    string excelConnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 4.0;HDR=YES;IMEX=1;Importmixedtypes=text;typeguessrows=0;\"", filelocation); 

    OleDbConnection excelConn = new OleDbConnection(excelConnStr); 

    excelConn.Open(); 

    DataTable dtPatterns = new DataTable(); 
    excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn); 

    excelDataAdapter.SelectCommand = excelCommand; 

    excelDataAdapter.Fill(dtPatterns); 

    ds.Tables.Add(dtPatterns); 

    return ds; 

} 

답변

2
System.out.println(cell.toString());//here is the problem 

toString() 메서드는 다음과 같습니다 객체의 문자열 표현을 반환합니다. 일반적으로, toString 메소드는,이 객체를 「텍스트로 표현한다」캐릭터 라인을 돌려줍니다.

사용 cell.getStringCellValue()

대신

cell.toString()

그리고 propor 사용이 필요합니다. 당신이

getNumericCellValue을 (사용) 및

if(cell!=null) 
      { 
       int type = cell.getCellType(); 
       if (type == HSSFCell.CELL_TYPE_STRING) 
        System.out.println(cell.getRichStringCellValue().toString()); 
       else if (type == HSSFCell.CELL_TYPE_NUMERIC) 

        String[] splits = String.valueOf(cell.getNumericCellValue()).split("."); 

       System.out.println(splits[0]); 
       else if (type == HSSFCell.CELL_TYPE_BOOLEAN) 
        System.out.println(cell.getBooleanCellValue()); 
       else if (type == HSSFCell.CELL_TYPE_BLANK) 
        System.out.println(cell.getColumnIndex() + "] = BLANK CELL"); 
      } 
이 조건을 넣어야 할 숫자 값에 대한