java excel异步导出_java 使用countdownlatch 异步导出excel并merge打包

这篇博客介绍了如何在Java中利用CountDownLatch实现异步导出大量Excel数据,通过分页查询和多线程处理提高效率。数据导出后,进一步将多个Excel文件合并到一个ZIP压缩包中。整个过程涉及JdbcTemplate、线程同步以及文件压缩技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ThreadTest extends AbstractJUnit4SpringContextTests {

public CountDownLatch main = new CountDownLatch(1);

public CountDownLatch sub = null;

public static final int maxRow = 60000;

public static List getMethod = new ArrayList();

public static List fileList = new ArrayList();

@Autowired

private JdbcTemplate jdbcTemplate;

public class Exporter implements Runnable{

private final CountDownLatch main;

private final CountDownLatch sub;

private final int beginIndex;

public Exporter(CountDownLatch main,CountDownLatch sub,int beginIndex){

this.main = main;

this.sub = sub;

this.beginIndex = beginIndex;

}

@Override

public void run() {

try {

main.await();

List target = jdbcTemplate.query(

"select ID,PrizeId,PrizePackageId,CreateTime from xxxxx where PrizePackageId=1121 AND id > 221711322 LIMIT " + beginIndex + "," + maxRow,

new BeanPropertyRowMapper(ExcelTest.Winning.class));

generateExcel(target,beginIndex);

}catch (InterruptedException e){

}finally {

sub.countDown();

}

}

}

@org.junit.Test

public void test() throws InterruptedException {

String sql = "select count(*) from xxxxx where PrizePackageId=1121 AND id > 221711322";

int count = jdbcTemplate.queryForInt(sql);

int totalPage = count%maxRow == 0 ? (count/maxRow):(count/maxRow)+1;

sub = new CountDownLatch(totalPage);

try {

getReflectInfo(ExcelTest.Winning.class,getMethod);

} catch (IntrospectionException e) {

logger.info(e);

}

for(int i =1;i<=totalPage;i++){

int beginIndex = (i - 1) * maxRow;

new Thread(new Exporter(main,sub,beginIndex)).start();

}

main.countDown();

sub.await();

//merge

String zipName = "ZipExport.zip";

try {

int len = 0;

byte[] buffers = new byte[1024];

File file = new File("D:\\"+zipName);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));

try {

//zos = new ZipOutputStream(bos);

for (File f : fileList) {

ZipEntry entry = new ZipEntry(f.getName());

zos.putNextEntry(entry);

InputStream in = new FileInputStream(f);

while ((len = in.read(buffers)) != -1) {

zos.write(buffers, 0, len);

}

zos.setEncoding("gbk");//解决中文

zos.closeEntry();

in.close();

}

zos.flush();

zos.close();

} catch (Exception localException) {

}

}catch(Exception e){}

System.out.println("Done");

}

private static void generateExcel(List target,int beginIndex){

HSSFWorkbook workbook2003 = new HSSFWorkbook();

// 创建工作表对象并命名

HSSFSheet sheet = workbook2003.createSheet("学生信息统计表");

// 遍历集合对象创建行和单元格

for (int i = 0; i < target.size(); i++) {

// 创建行

HSSFRow row = sheet.createRow(i);

Object object = target.get(i);

for (int j=0;j< getMethod.size();j++) {

try {

Object result = getMethod.get(j).invoke(object);

if (result instanceof Date) {

result = DateUtils.format((Date) result, DateUtils.getSecondFormatter());

}

HSSFCell cell = row.createCell(j);

cell.setCellValue(result.toString());

} catch (Exception e) {

}

}

}

// 生成文件

File file = new File("D:\\excelTest_"+beginIndex+ ".xls");

FileOutputStream fos = null;

try {

fos = new FileOutputStream(file);

workbook2003.write(fos);

fileList.add(file);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (fos != null) {

try {

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

private static void getReflectInfo(Class> c,List getMethod) throws IntrospectionException {

Field[] fields = c.getDeclaredFields();

for (int i = 0; i < fields.length; i++) {

if (!fields[i].getName().equalsIgnoreCase("serialVersionUID") && !fields[i].getName().equalsIgnoreCase("$jacocoData")) {

getMethod.add(new PropertyDescriptor(fields[i].getName(), c).getReadMethod());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值