package com.ws.demo;
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 java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author wangsheng
* @title: PoiTest
* @projectName demo
* @description:
*/
public class PoiTest {
public static void main(String[] args) {
multiThreadWrite();
}
/**
* 使用多线程进行Excel写操做,提升写入效率。
*/
public static void multiThreadWrite() {
/**
* 使用线程池进行线程管理。
*/
ExecutorService es = Executors.newCachedThreadPool();
/**
* 使用计数栅栏
*/
CountDownLatch doneSignal = new CountDownLatch(3);
HSSFWorkbook wb;
try {
wb = new HSSFWorkbook(new FileInputStream("E:\\temp\\poiTest.xls"));
HSSFSheet sheet = wb.getSheetAt(0);
es.submit(new PoiWriter(doneSignal, sheet, 0, 19999));
es.submit(new PoiWriter(doneSignal, sheet, 20000, 39999));
es.submit(new PoiWriter(doneSignal, sheet, 40000, 59999));
/**
* 使用CountDownLatch的await方法,等待全部线程完成sheet操做
*/
doneSignal.await();
es.shutdown();
FileOutputStream os = new FileOutputStream("E:\\temp\\poiTest.xls");
wb.write(os);
os.flush();
os.close();
System.out.println("Excel completed......");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 测试基本的POI写操做
*/
public static void poiBasicWriteTest() {
try {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(
"E:\\temp\\poiTest.xls"));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.createRow(0);
HSSFCell contentCell = row.createCell(0);
contentCell.setCellValue("abc");
FileOutputStream os = new FileOutputStream("E:\\temp\\poiTest.xls");
wb.write(os);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* sheet的row使用treeMap存储的,是非线程安全的,因此在建立row时须要进行同步操做。
* @param sheet
* @param rownum
* @return
*/
private static synchronized HSSFRow getRow(HSSFSheet sheet, int rownum) {
return sheet.createRow(rownum);
}
/**
* 进行sheet写操做的sheet。
* @author alex
*
*/
protected static class PoiWriter implements Runnable {
private final CountDownLatch doneSignal;
private HSSFSheet sheet;
private int start;
private int end;
public PoiWriter(CountDownLatch doneSignal, HSSFSheet sheet, int start,
int end) {
this.doneSignal = doneSignal;
this.sheet = sheet;
this.start = start;
this.end = end;
}
public void run() {
int i = start;
try {
while (i <= end) {
HSSFRow row = getRow(sheet, i);
HSSFCell contentCell = row.getCell(0);
if (contentCell == null) {
contentCell = row.createCell(0);
}
contentCell.setCellValue(i + 1);
++i;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
doneSignal.countDown();
System.out.println("start: " + start + " end: " + end
+ " Count: " + doneSignal.getCount());
}
}
}
}
poi多线程写
最新推荐文章于 2024-05-29 14:22:30 发布