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();
}
}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM