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