//创建一个excel文件
HSSFWorkbook workBook = new HSSFWorkbook();
//excel内容
HSSFSheet mySheet;
mySheet = workBook.createSheet("sheet1");//创建一个工作薄
mySheet.setColumnWidth(0,6000);// //第一个参数代表列id(从0开始),第2个参数代表列宽度值
mySheet.setColumnWidth(1,6000);
mySheet.setColumnWidth(2,6000);
mySheet.setColumnWidth(3,10000);
mySheet.setColumnWidth(4,10000);
//单元格格式
HSSFCellStyle style = workBook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
HSSFFont font = workBook.createFont();//设置字体样式
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16); //字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
style.setFont(font);
//单元格格式
HSSFCellStyle styleTwo = workBook.createCellStyle();
styleTwo.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
styleTwo.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
styleTwo.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
styleTwo.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
styleTwo.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
HSSFFont fontTwo = workBook.createFont();//设置字体样式
fontTwo.setFontName("宋体");
fontTwo.setFontHeightInPoints((short) 16);
fontTwo.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
styleTwo.setFont(fontTwo);
//单元格格式
HSSFCellStyle styleThree = workBook.createCellStyle();
styleThree.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
styleThree.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
styleThree.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
styleThree.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
styleThree.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
styleThree.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//excel 自定义的格式
HSSFFont fontThree = workBook.createFont();//设置字体样式
fontThree.setFontName("宋体");
fontThree.setFontHeightInPoints((short) 16); //字体大小
fontThree.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
styleThree.setFont(font);
HSSFRow myRow = mySheet.createRow(0);//创建 并设置第一行
myRow.setHeight((short) 500);
HSSFCell cell = myRow.createCell((short) 0);
cell.setCellStyle(styleTwo);
cell.setCellValue("用户名");
cell = myRow.createCell((short) 1);
cell.setCellStyle(styleTwo);
cell.setCellValue("用户电话");
cell = myRow.createCell((short) 2);
cell.setCellStyle(styleTwo);
cell.setCellValue("用户类型");
cell = myRow.createCell((short) 3);
cell.setCellStyle(styleTwo);
cell.setCellValue("收益总金额(元)");
cell = myRow.createCell((short)4);
cell.setCellStyle(styleTwo);
cell.setCellValue("钱包余额(元)");
for(int i = 1; i <= list.size(); i++){
myRow = mySheet.createRow(i);
UserIncomeStatistic userIncomeStatistic = list.get(i-1); //数据信息
cell = myRow.createCell((short) 0);
cell.setCellStyle(style);
cell.setCellValue(userIncomeStatistic.getUserName());
cell = myRow.createCell((short) 1);
cell.setCellStyle(style);
cell.setCellValue(userIncomeStatistic.getUserMobile());
cell = myRow.createCell((short) 2);
cell.setCellStyle(style);
String userType = "";
if("Admin".equals(userIncomeStatistic.getUserType())){
userType = "管理员";
} else if("Business".equals(userIncomeStatistic.getUserType())){
userType = "商家";
} else if("Member".equals(userIncomeStatistic.getUserType())){
userType = "会员";
}
cell.setCellValue(userType);
cell = myRow.createCell((short) 3);
cell.setCellStyle(styleThree);
cell.setCellValue(Double.parseDouble(userIncomeStatistic.getIncomeAllSum())); //字符串转为double
cell = myRow.createCell((short) 4);
cell.setCellStyle(styleThree);
cell.setCellValue(Double.parseDouble(userIncomeStatistic.getBalance())); //字符串转为double
}