jxl对excel删除行

时间:2023-03-09 03:15:56
jxl对excel删除行

简单记录下:

package com.pingan;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import jxl.Cell;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook; public class KanApp { /**
* @param args
*/
public static void main(String[] args) {
int rows = 0;
int cols = 0;
//单元格内容
String strCon = "";
//excel路径 注意只支持xls后缀的
String filePath =
"D:\\Users\\kanlianhui689\\Desktop\\WorkSpace\\onecard.xls";
//正则
Pattern pattern =
Pattern.compile("(\\w|\\-)+(\\w|\\.|\\-){0,}@[a-z0-9A-Z]+(\\.[a-z0-9A-Z]+){0,}\\.[A-Za-z]+");
Matcher matcher = null; boolean isEmail = false ; try {
Workbook book = Workbook.getWorkbook( new File( filePath ));
WritableWorkbook wbook = Workbook.createWorkbook(new File(filePath), book);//
// 获得第一个工作表对象
WritableSheet sheet = (WritableSheet) wbook.getSheet( 0 );
rows = sheet.getRows();
Cell cell1 = null;
for (int i = rows - 1; i >=0 ; i--){
cell1 = sheet.getCell( cols , i );
strCon = cell1.getContents(); matcher = pattern.matcher(strCon.trim());
isEmail = matcher.matches();
//System.out.println("try :["+strCon + "]\tres :"+isEmail);
if (isEmail){
System.out.println("delete :"+strCon);
sheet.removeRow(i);
}
}
wbook.write();
wbook.close();
book.close();
System.out.println("done!");
} catch (Exception e) {
System.out.println(e);
}
}
}