业务:利用sax将表格解析为xml快速读取表格百万级数据,利用多线程将这批数据分割为以一千为单位的小型的excel,根据表头分割出表名,只取第一张表的所有数据
public static void DataInsertData() throws InterruptedException {
for (int i = 0; i < stringlist.size(); i++) {
TableEntity tableEntity = tabledata.get(stringlist.get(i));
String columnnum = tableEntity.getColumnnum();
String[] split = columnnum.split(",");
ArrayList<String> tablelins = new ArrayList<>();
for (int j = 0; j < tableslist.size(); j++) {
ArrayList<String> arrlist = new ArrayList<>();
for (int k = 0; k < split.length; k++) {
String s = tableslist.get(j).get(Integer.parseInt(split[k]));
arrlist.add(s);
}
tablelins.add(arrlist.toString());
list.add(arrlist);
if(j%1000==0){
ThreadExcelImport threadExcelImport = new ThreadExcelImport(list);
Thread.sleep(10000);
threadExcelImport.start();
list.clear();
}
}
tabledata.get(stringlist.get(i)).setColumnvalue(tablelins);
}
}
多线程执行生成以一千为单位的excel格式
package com.bootdo.gi;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.List;
import java.util.Random;
/**
* Created by tiantian on 2019/7/31.
*/
public class ThreadExcelImport extends Thread {
private static Workbook wb;
private List<List<String>> list;
public ThreadExcelImport(List<List<String>> list) {
this.list = list;
}
public List<List<String>> getList() {
return list;
}
public void setList(List<List<String>> list) {
this.list = list;
}
@Override
public void run() {
wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
for (int i = 0; i <list.size() ; i++) {
Row row = sheet.createRow(i);
List<String> rowList = this.list.get(i);
for (int j = 0; j <rowList.size() ; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(rowList.get(j));
}
}
try {
wb.write(new FileOutputStream(new File("D:\\生成的大量数据\\数据表"+new Random().nextInt(100)+".xlsx")));
} catch (IOException e) {
e.printStackTrace();
}
}
}