日期:2014-05-17  浏览次数:20774 次

java读取excel中单元格数据导入数据库存在问号
1.excel单元格的数据尾部都有一个空白
2.javatrim()函数也不管用。
Java code

String [][]str=new String[iRowCount][iColCount];
     
     for (int i=1;i<iRowCount;i++) {
     HSSFRow row=sheet.getRow(i);
     if(sheet.getRow(i)!=null){      
           for(int j=0;j<iColCount;j++)   {
                       HSSFCell cell   =row.getCell(j);
                       if (cell!=null){
                       switch(cell.getCellType())    //判断类型
                                           {  
                                           case HSSFCell.CELL_TYPE_NUMERIC:  //数字                                      
                                           cell_value=((Double)cell.getNumericCellValue()).toString().trim();
                                           //cell_value=cell_value.substring(0,cell_value.length());
                                           System.out.println("xxxxxxxx长度"+cell_value.length());
                                            System.out.println("xxxxxxxx长度"+cell_value);
                                           break;  
                                           case HSSFCell.CELL_TYPE_STRING: //字符串
                                           cell_value=cell.getStringCellValue().toString().trim(); 
                                            //cell_value = new String(cell_value.getBytes(), "utf-8");
                                            //cell_value=encodeURI(cell_value);
                                           break;  
                                           case HSSFCell.CELL_TYPE_FORMULA:  //公式
                                           cell_value= cell.getCellFormula().toString().trim();  
                                          
                                           break;   
                                           case HSSFCell.CELL_TYPE_BLANK: // 空值 
                                           cell_value= " <font   color=red> null </font> ";
                                           break;  
                                           case HSSFCell.CELL_TYPE_ERROR: // 故障     
                                           break;  
                                           default:   //未知类型
                                           cell_value= " <font   color=red> null </font> ";
                                           break; 
                        
                                           }     //end   switch]);
                        out.print(cell_value+ "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                        //str[i][j]=cell_value=cell_value.substring(0,cell_value.length()-1);
                        str[i][j]=cell_value.replace((char)12288,' ').replace(" ","");
                        //str[i][j]=cell_value.replace(" ","").trim();
                        
                        
                        System.out.println("第"+i+"行,第"+j+"列 :"+ str[i][j]);
                        }   //end   if    
               }   //end       for   (short   j   =   0;   j   <   iColCount;   j++)   {
       //  out.print( " <br> ");
         }     //end     if(sheet.getRow(i)==null){
       else 
       { 
       iRowCount++;   
       System.out.println("fileToBeRead " + iRowCount);
       }


除了截取有木有好办法

------解决方案--------------------
str.replace("\\s+", "");//去除全角空格
------解决方案--------------------
查了下ASCII编码表:

160 HTML空格(即:&nbsp;) 解释:Non-breaking space

原来如此,那么只能试试看:
str = str.replaceAll("[u00A0]", "");