POI导入Excel


public class OutPutExcelUtil {
   
public static void outputExcel(List<ResumeInfoEntity> list)throws Exception{
int size=list.size();//拿到集合大小
HSSFWorkbook  web=new HSSFWorkbook();//创建对象
HSSFSheet  sheet=web.createSheet("导出简历Excel");//创建页
HSSFRow  firstrow=sheet.createRow((short)0);//创建行
HSSFCellStyle headStyle=web.createCellStyle();//创建列
headStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//创建列样式
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        headStyle.setWrapText(true);


ResumeInfoEntity resume=ApplicationEntityUtil.getResumeInfoEntity();//实例化一个实体
//以数组的形式 创建表头样式
String[]  names=new String[11];
names[0] ="编号";
names[1] ="名称";
names[2] ="籍贯";
names[3] ="性别";
names[4] ="学历";
names[5] ="专业";
names[6] ="毕业院校";
names[7] ="年龄";
names[8] ="邮箱";
names[9] ="联系方式";
names[10]="教育经历";
//循环创建一个表头
for(int i=0;i<names.length;i++){
HSSFCell firstcell=firstrow.createCell((short) i);//创建第I列
firstcell.setEncoding(HSSFCell.ENCODING_UTF_16);//编码格式为UTF-16
firstcell.setCellValue(names[i]);//往表格里面放入值

}
//循环创建行并放入值
for(int j=0;j<list.size();j++){
  HSSFRow row = sheet.createRow(j+ 1);
  HSSFCellStyle cellstyle=web.createCellStyle();
  cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//每当创建一行的时候就赋值一次 List集里面放的本来就是Resume对象
  resume=list.get(j);
  for(int count=0;count<names.length;count++){
  HSSFCell onecell=row.createCell((short)0);
  onecell.setEncoding(HSSFCell.ENCODING_UTF_16);
  onecell.setCellValue(resume.getResumeId());//编号
 
  HSSFCell twocell=row.createCell((short)1);
  twocell.setEncoding(HSSFCell.ENCODING_UTF_16);
  twocell.setCellValue(resume.getResumeName());//名称
 
  HSSFCell threecell=row.createCell((short)2);
  threecell.setEncoding(HSSFCell.ENCODING_UTF_16);
  threecell.setCellValue(resume.getResumeBirthplace());//籍贯
 
  HSSFCell  fourcell=row.createCell((short)3);
  fourcell.setEncoding(HSSFCell.ENCODING_UTF_16);
  fourcell.setCellValue(resume.getResumeSex());//性别
                 
  HSSFCell  fivecell=row.createCell((short)4);
  fivecell.setEncoding(HSSFCell.ENCODING_UTF_16);
  fivecell.setCellValue(resume.getResumeEducation());//学历
 
  HSSFCell  sixcell=row.createCell((short)5);//专业
  sixcell.setEncoding(HSSFCell.ENCODING_UTF_16);
  sixcell.setCellValue(resume.getResumeSpecialty());
 
 
  HSSFCell  sevencell=row.createCell((short)6);//毕业院校
  sevencell.setEncoding(HSSFCell.ENCODING_UTF_16);
  sevencell.setCellValue(resume.getGraduateSchool());
 
  HSSFCell  eighrcell=row.createCell((short)7);//年龄
  eighrcell.setEncoding(HSSFCell.ENCODING_UTF_16);
  eighrcell.setCellValue(resume.getResumeAge());
 
  HSSFCell  ninecell=row.createCell((short)8);//邮箱
  ninecell.setEncoding(HSSFCell.ENCODING_UTF_16);
  ninecell.setCellValue(resume.getResumeMailbox());
 
  HSSFCell  tencell=row.createCell((short)9);//联系方式
  tencell.setEncoding(HSSFCell.ENCODING_UTF_16);
  tencell.setCellValue(resume.getResumeTelephone());
 
  }
}
     // 创建文件输出流,准备输出电子表格
        OutputStream out = new FileOutputStream("e:/ResumeInfoTest.xls");
        web.write(out);
        out.close();
        System.out.println("数据库导出成功");

}

你可能感兴趣的:(Excel)