POI倒出报表

导入jar包

1

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.17-beta1</version>
</dependency>

1.自己动手写一个单测

    HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件    

/创建HSSFSheet对象(excel表单)

   

HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet

 //创建HSSFRow对象(第一行)

HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell(0);

  //设置单元格的值

/*  cell.setCellValue("学生成绩表");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,2));

HSSFRow row1=sheet.createRow(1);

row1.createCell(0).setCellValue("学生编号");
row1.createCell(1).setCellValue("学生姓名");
row1.createCell(2).setCellValue("学生年龄");

HSSFRow row3=sheet.createRow(2);
row3.createCell(0).setCellValue("1");
row3.createCell(1).setCellValue("真");
row3.createCell(2).setCellValue("18");*/
//创建一个输入流作为报表倒出一个盘

FileOutputStream outputStream=new FileOutputStream("d:\workbooks.xls");

wk.write(outputStream);

outputStream.flush();

//根据集合创建xsl文件

 //创建HSSFWork 对象
HSSFWorkbook wk=new HSSFWorkbook();
//HSSFSheet对象
HSSFSheet sheet=wk.createSheet("学生表");
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell(0);

cell.setCellValue("学生编号");
cell=row.createCell((short)1);
cell.setCellValue("学生姓名");
cell=row.createCell((short)2);
cell.setCellValue("学生年龄");

List<Student> list=new ArrayList();
Student student=new Student();
student.setId(1);
student.setName("溜溜");
student.setAge(12);

Student student1=new Student();
student1.setId(2);
student1.setName("落空");
student1.setAge(13);

list.add(student);
list.add(student1);


for (short i=0;i<list.size();i++) {
row=sheet.createRow(i+1);
row.createCell(0).setCellValue(list.get(i).getId());
row.createCell(1).setCellValue(list.get(i).getName());
row.createCell(2).setCellValue(list.get(i).getAge());
}





原文地址:https://www.cnblogs.com/hualishu/p/7444661.html