1. 程式人生 > >Java-POI操作excel遇到文字字元問題處理

Java-POI操作excel遇到文字字元問題處理

1、問題:用poi讀寫excel,據工單編號匹配兩張sheet的記錄,提取cell文字格內容,發現相同字元無法匹配,用byte才發現,有部分文字帶了亂碼(ascii碼是-62和-96,不知道是什麼東西);

2、解決:增加對字元文字的亂碼處理,過濾掉亂碼,同時用byte陣列來匹配,程式碼如下:

package dx;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class order {
	public static void main(String[] args) {
		try {
			//獲取excel檔案
			String path="D:"+System.getProperty("file.separator")+"tmp"+System.getProperty("file.separator")+"order.xls";
			POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(path));
			//得到Excel工作簿物件    
			HSSFWorkbook wb = new HSSFWorkbook(fs);  
			//得到Excel工作表物件    
			HSSFSheet sheet1 = wb.getSheetAt(0);
			HSSFSheet sheet2 = wb.getSheetAt(1);
			
			//用第二張表上的工單編號去匹配第一張表上的工單編號,匹配在第一張第二列輸入yes
			int rowCount2=sheet2.getLastRowNum();		
			for(int i=1;i<=rowCount2;i++){
				HSSFRow row2 = sheet2.getRow(i);
				HSSFCell orderNo2 = row2.getCell(0);
				String strNo2=orderNo2.getStringCellValue();
				
				strNo2=MessyCodeFilter(strNo2);//清除亂碼
				byte[] byt2=strNo2.getBytes("UTF-8");
					
				//匹配第一張表的工單
				int rowCount1=sheet1.getLastRowNum();
				for(int j=1;j<=rowCount1;j++){
					HSSFRow row1 = sheet1.getRow(j);
					HSSFCell orderNo1 = row1.getCell(0);
					String strNo1=orderNo1.getStringCellValue();
					
					strNo1=MessyCodeFilter(strNo1);//清除亂碼
					byte[] byt1=strNo1.getBytes("UTF-8");
					/*for(int ii=0;ii<byt1.length;ii++){
						System.out.print(ii+":"+byt1[ii]+"\r\n");
					}*/
						
					//if(strNo2==strNo1){//工單編號一致判斷
					if(byt2.length==byt1.length){
						boolean bif=true;
					    for(int bi=0;bi<byt2.length;bi++){
					    	if(byt2[bi]!=byt1[bi]){
					    		bif=false;
					    		break;
					    	}
					    }
					    if(bif){
							HSSFCell yesno1=row1.getCell(1);	
							yesno1.setCellValue("yes");		
							HSSFCell yesno2 = row2.getCell(1);
							yesno2.setCellValue("yes");
							FileOutputStream out=new FileOutputStream(path);
							out.flush();
							wb.write(out);
							out.close();
							break;
					    }
					}
				}
				
			}	
			wb.close();			
			fs.close();
			 
		} catch (FileNotFoundException e) {e.printStackTrace();} 
		catch (IOException e) {e.printStackTrace();} 
	}
	
	//非ascii編碼範圍過濾
	private static String AsciiCodeFilter(String strCont) {  
			try{
				byte[] byteCon=strCont.getBytes("UTF-8");
				for(int i=0;i<byteCon.length;i++){
					if (byteCon[i]<0 || byteCon[i]>127){
						byteCon[i]=32;
					}	
				}
				return new String(byteCon).trim();
			} catch (Exception e) {  
	            e.printStackTrace();  
	        }  
			return strCont;
	}    
	
	//非有效數字、字母、字元過濾
	private static String MessyCodeFilter(String strCont) {  
        try {  
            Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
            Matcher m = p.matcher(strCont);  
            String after = m.replaceAll("");  
            String temp = after.replaceAll("\\p{P}", "");  
            char[] ch = temp.trim().toCharArray();  

            int length = (ch != null) ? ch.length : 0;  
            for (int i = 0; i < length; i++) {  
                char c = ch[i];  
                if (!Character.isLetterOrDigit(c)) {  
                    String str = "" + ch[i];  
                    //if (!str.matches("[\u4e00-\u9fa5]+")) {  
                    if (!str.matches("[0-9a-zA-Z\\u4e00-\\u9fa5]+")){  
                       //ch[i]='\0';//該為置空,也可以用位移
                   	   ch[i]=' ';//用空格
                    }  
                }  
            }  
            return  new String(ch).replace(" ", "").trim();
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return strCont.trim();
	}
}