获取数据list,然后按照list大小,分割成500条数据一个excel.生成多个excel.
------解决思路----------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools
------解决思路----------------------
Templates
* and open the template in the editor.
*/
package com.excel;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
/**
*
* @author lugl
*/
public class Test {
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
for (int i = 0; i < 30; i++) {
strings.add("test" + i);
}
for (int i = 0; i < strings.size(); i++) {
if (i % 3 == 0) {
List<String> list = new ArrayList<>();
int fromIndex = i / 3 * 3;
int toIndex = (i / 3 + 1) * 3;
if (toIndex > strings.size()) {
toIndex = strings.size();
}
list = strings.subList(fromIndex, toIndex);
createEx(list, "sheet" + i);
}
}
}
public static void createEx(List<String> list, String sheetName) {
FileOutputStream os = null;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName);
for (int i = 0; i < list.size(); i++) {
String string = list.get(i);
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(string);
}
try {
os = new FileOutputStream("D:\\" + sheetName + ".xls");
wb.write(os);
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}