为什么用poi导出excel,设置的单元格格式一个都没显示出来,而且
row.createCell(0).setCellValue("考试科目:");也没在excel里显示出”考试科目“//导出学生安排数据publicvoidexportE...
row.createCell(0).setCellValue("考试科目:"); 也没在excel里显示出”考试科目“
//导出学生安排数据
public void exportExcel(OutputStream os) throws Exception{
Workbook workbook=null;
if("xls".equals(format)){
workbook=new HSSFWorkbook();
}
if("xlsx".equals(format)){
workbook=new XSSFWorkbook();
}
List<Classroom> classrooms=new ArrayList<Classroom>();
arrangeService aS=new arrangeServiceImpl();
classrooms=aS.findAllClassroom();//每个考场一个sheet
for(int i=0;i<classrooms.size();i++){
List<Arrange> list=new ArrayList<Arrange>();
list=aS.findByClassroomId(classrooms.get(i).getId());
if(list.size()!=0){
Sheet sheet=workbook.createSheet(list.get(0).getCourseName()+"_"+list.get(0).getClassroomId());
CellStyle cellStyle1 = workbook.createCellStyle(); //建立新的cell样式
Font font1 = workbook.createFont();
font1.setFontName("宋体");
font1.setFontHeightInPoints((short) 16);// 设置字体大小
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle1.setFont(font1);
cellStyle1.setAlignment(CellStyle.ALIGN_CENTER);
Row row=sheet.createRow(0);
row.createCell(0).setCellStyle(cellStyle1);
row.createCell(0).setCellValue("考试科目:");
row.createCell(1).setCellStyle(cellStyle1);
row.createCell(1).setCellValue(list.get(0).getCourseName());
Row row3=sheet.createRow(1);
row.createCell(0).setCellStyle(cellStyle1);
row3.createCell(0).setCellValue("序号");
row3.createCell(1).setCellStyle(cellStyle1);
row3.createCell(1).setCellValue("学号");
row3.createCell(2).setCellStyle(cellStyle1);
row3.createCell(2).setCellValue("学生");
row3.createCell(3).setCellStyle(cellStyle1);
row3.createCell(3).setCellValue("备注");
for(int j=2;j<list.size()+2;j++){
Arrange a=list.get(j-2);
row=sheet.createRow(j);
row.createCell(0).setCellValue(j-1);
row.createCell(1).setCellValue(a.getStuId());
row.createCell(2).setCellValue(a.getStudentName());
row.createCell(3).setCellValue("");
}
}
}
try{
workbook.write(os);
}catch(IOException e){
e.printStackTrace();
}
} 展开
//导出学生安排数据
public void exportExcel(OutputStream os) throws Exception{
Workbook workbook=null;
if("xls".equals(format)){
workbook=new HSSFWorkbook();
}
if("xlsx".equals(format)){
workbook=new XSSFWorkbook();
}
List<Classroom> classrooms=new ArrayList<Classroom>();
arrangeService aS=new arrangeServiceImpl();
classrooms=aS.findAllClassroom();//每个考场一个sheet
for(int i=0;i<classrooms.size();i++){
List<Arrange> list=new ArrayList<Arrange>();
list=aS.findByClassroomId(classrooms.get(i).getId());
if(list.size()!=0){
Sheet sheet=workbook.createSheet(list.get(0).getCourseName()+"_"+list.get(0).getClassroomId());
CellStyle cellStyle1 = workbook.createCellStyle(); //建立新的cell样式
Font font1 = workbook.createFont();
font1.setFontName("宋体");
font1.setFontHeightInPoints((short) 16);// 设置字体大小
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
cellStyle1.setFont(font1);
cellStyle1.setAlignment(CellStyle.ALIGN_CENTER);
Row row=sheet.createRow(0);
row.createCell(0).setCellStyle(cellStyle1);
row.createCell(0).setCellValue("考试科目:");
row.createCell(1).setCellStyle(cellStyle1);
row.createCell(1).setCellValue(list.get(0).getCourseName());
Row row3=sheet.createRow(1);
row.createCell(0).setCellStyle(cellStyle1);
row3.createCell(0).setCellValue("序号");
row3.createCell(1).setCellStyle(cellStyle1);
row3.createCell(1).setCellValue("学号");
row3.createCell(2).setCellStyle(cellStyle1);
row3.createCell(2).setCellValue("学生");
row3.createCell(3).setCellStyle(cellStyle1);
row3.createCell(3).setCellValue("备注");
for(int j=2;j<list.size()+2;j++){
Arrange a=list.get(j-2);
row=sheet.createRow(j);
row.createCell(0).setCellValue(j-1);
row.createCell(1).setCellValue(a.getStuId());
row.createCell(2).setCellValue(a.getStudentName());
row.createCell(3).setCellValue("");
}
}
}
try{
workbook.write(os);
}catch(IOException e){
e.printStackTrace();
}
} 展开
1个回答
追问
我改了也一样啊,数据能显示出来,设置的单元格格式没显示出来啊
追答
以下面8句话分析,在你创建完样式之后,你又在原来的单元格上创建一次新的,新创建的有内容的单元格对象将原来的替换了,创建完后后面要用到此单元格时用getCell()方法取出对象再设置,其它所有地方都参照此修改。
-------------------错误代码----------------------
Row row3=sheet.createRow(1);
row3.createCell(0).setCellStyle(cellStyle1);
row3.createCell(0).setCellValue("序号");
row3.createCell(1).setCellStyle(cellStyle1);
row3.createCell(1).setCellValue("学号");
row3.createCell(2).setCellStyle(cellStyle1);
row3.createCell(2).setCellValue("学生");
row3.createCell(3).setCellStyle(cellStyle1);
row3.createCell(3).setCellValue("备注");
--------------------正确代码-------------------------
Row row3=sheet.createRow(1);
row3.createCell(0).setCellStyle(cellStyle1);
row3.getCell(0).setCellValue("序号");
row3.createCell(1).setCellStyle(cellStyle1);
row3.getCell(1).setCellValue("学号");
row3.createCell(2).setCellStyle(cellStyle1);
row3.getCell(2).setCellValue("学生");
row3.createCell(3).setCellStyle(cellStyle1);
row3.getCell(3).setCellValue("备注");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询