Java-POI操作excel遇到文本字符问题处理
生活随笔
收集整理的這篇文章主要介紹了
Java-POI操作excel遇到文本字符问题处理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、問題:用poi讀寫excel,據(jù)工單編號匹配兩張sheet的記錄,提取cell文本格內(nèi)容,發(fā)現(xiàn)相同字符無法匹配,用byte才發(fā)現(xiàn),有部分文本帶了亂碼(ascii碼是-62和-96,不知道是什么東西);
2、解決:增加對字符文本的亂碼處理,過濾掉亂碼,同時用byte數(shù)組來匹配,代碼如下:
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);//用第二張表上的工單編號去匹配第一張表上的工單編號,匹配在第一張第二列輸入yesint 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;} //非有效數(shù)字、字母、字符過濾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();} }總結(jié)
以上是生活随笔為你收集整理的Java-POI操作excel遇到文本字符问题处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线实时大数据平台Storm并行度试验
- 下一篇: 算法导论之用于不相交集合的数据结构