- 新建MAVEN工程
org.apache.poi
poi
3.16
org.apache.poi
poi-ooxml
3.16
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.poi.EncrypteddocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class App {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("D:\aaa.xlsx");
byte[] buff = new byte[is.available()]; // 将数据读取缓存, 避免频繁访问磁盘
is.read(buff);
is.close();
int threadTotal = 2; // 线程数量
int start = 1; // 循环开始位置, 便于中断后继续
ExecutorService es = Executors.newFixedThreadPool(threadTotal);
for (int i = 0; i < threadTotal; i++) {
es.execute(new MyTask(start, threadTotal, i, buff));
}
}
}
class MyTask implements Runnable {
static String result = null; // 结果
byte[] buf = null; // 数据缓存
int start = 0; // 开始位置
int step = 0; // 总的线程数量
int mark = 0; // 运行标记, 用于分配任务
public MyTask(int start, int threadTotal, int mark, byte[] buf) {
this.start = start + mark;
this.mark = mark;
this.step = threadTotal;
this.buf = buf;
}
public String mm(long i) {
if (i < 52) {
if (i < 26)
return String.valueOf((char) (65 + i));
else
return String.valueOf((char) (97 + i - 26));
} else {
return mm(i / 52 - 1) + mm(i % 52);
}
}
private boolean work(String pwd) {
try {
InputStream is = new ByteArrayInputStream(buf);
WorkbookFactory.create(is, pwd);
} catch (EncrypteddocumentException e) {
return false;
} catch (IOException e) {
} catch (InvalidFormatException e) {
}
MyTask.result = pwd;
return true;
}
@Override
public void run() {
Thread.currentThread().setName("T" + mark);
int cnt = 0;
while (MyTask.result == null) {
if (cnt++ == 100 && this.mark == 0) {
System.out.println(Thread.currentThread().getName() + " running at " + start + ", " + mm(start));
cnt = 1;
}
if (work(mm(start)))
break;
start += step;
}
System.err.println(">>>>>>>>>>>>>>>>" + MyTask.result);
}
}