main函数
package xsl;
import java.util.List;
public class main {
public static void main(String args[]){
String xlspath = "/Users/peter/Documents/JIRA管理系统.xls";
//读xls
xlsread obb = new xlsread();
List excelList = obb.readExcel(xlspath);
//比较后返回
String keystring = "【DCE3.0】";
compare abb = new compare();
List newlist = abb.comparexls(excelList, keystring);
//写xls
String newxlspath = "/Users/peter/Documents/test.xls";
xlswrite cbb = new xlswrite();
cbb.write(newxlspath, newlist);
}
}
读excel
1.返回的是两维list
package xsl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class xlsread {
public List readExcel(String xlspath) {
File file = new File(xlspath);
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
List<List> outerList=new ArrayList<List>();
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
for (int i = 0; i < sheet.getRows(); i++) {
List innerList=new ArrayList();
// sheet.getColumns()返回该页的总列数
for (int j = 0; j < sheet.getColumns(); j++) {
String cellinfo = sheet.getCell(j, i).getContents();
if(cellinfo.isEmpty()){
continue;
}
innerList.add(cellinfo);
}
outerList.add(i, innerList);
}
return outerList;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
对比关键词,并返回带关键词的记录
package xsl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class compare {
public List comparexls(List rawList, String keyword) {
List<List> outerList=new ArrayList<List>();
//获取行
for (int i = 0; i < rawList.size(); i++) {
List innerList=new ArrayList();
List list = (List) rawList.get(i);
//每行的list再判断关键词,有关键词,则将该行赋值给innerlist
for (int j = 0; j < list.size(); j++) {
String list1 = list.get(j).toString();
if(list1.contains(keyword)){
innerList = list;
for (int m = 0; m < list.size(); m++) {
System.out.print(list.get(m));
}
}
}
//没有带关键词的行会为空,需要判读
if(!innerList.isEmpty()){
outerList.add(innerList);
}
System.out.println();
}
return outerList;
}
}
写excel
package xsl;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class xlswrite {
public void write (String xlspath, List newlist){
File file=new File(xlspath);
try{
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet( " 第一页 " , 0 );
for (int i = 0;i<newlist.size();i++){
List innerList=new ArrayList();
List list = (List) newlist.get(i);
if(list!=null){
for (int j = 0; j < list.size(); j++) {
Label label = new Label( j ,i , list.get(j).toString() );
sheet.addCell(label);
}
}
}
workbook.write();
workbook.close();
}catch (Exception e){
System.out.println(e);
}
}
}
网友评论