java导出Excel数据大可以用多个sheet页存数据

package cn.com.sinosoft.boot.webservice;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;


@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class WebServiceAxisClient {

@Test
public void run7() {
try {
HSSFWorkbook wb = new HSSFWorkbook();
//写入的路径到具体xls
File savedFile = new File("E:\\anti_money\\111.xls");
OutputStream output = new FileOutputStream(savedFile);
//查询数据LpfMLmRiskAppBPojo表示实体类,lpfMLmRiskAppBDao表示与数据库查询的dao层
        List<LpfMLmRiskAppBPojo> list = lpfMLmRiskAppBDao.queryAll();
int totle = list.size();// 获取List集合的size
int mus = 5;// :excel表格一个工作表可以存储5条)
int avg = totle / mus;
for (int i = 0; i < avg + 1; i++) {
      //sheet页的名称

HSSFSheet sheet = wb.createSheet("信息" + (i + 1));
HSSFRow row = sheet.createRow(0);
// 第一行标题
String[] head = new String[]{"险种编码", "险种名称"};
int headInt = 0;
for (String title : head) {
row.createCell(headInt++).setCellValue(title);
}
int num = i * mus;
int index = 0;
int rowInt = 1;
for (int m = num; m < list.size(); m++) {
if (index == mus) {// 判断index == mus的时候跳出当前for循环
break;
}
LpfMLmRiskAppBPojo actualReturnDetial = list.get(m);
// 每列对应的字段
row = sheet.createRow(rowInt++); // 创建行
row.createCell(0).setCellValue(actualReturnDetial.getRiskCode());
row.createCell(1).setCellValue(actualReturnDetial.getRiskName());

index++;
}
}
wb.write(output);
output.close();

} catch (Exception e) {
e.printStackTrace();
}
}

}
posted @ 2020-09-23 09:56  平安亮子  阅读(1126)  评论(0编辑  收藏  举报