private static Workbook workbook=null;
private static Sheet sheet=null;
public static void writeExcel() throws Exception{
OutputStream out=new FileOutputStream("F:"+File.separator+"test_2.xlsx");//文件本地存储地址
workbook=new HSSFWorkbook();
sheet=workbook.createSheet("testWrite");
Row row=null;
Cell cell=null;
for(int i=0;i<6;i++){
row=sheet.createRow(i);
for(int j=0;j<6;j++){
cell=row.createCell(j);
sheet.setColumnWidth(j, 4500);
//cell.setCellValue("writeExcel_"+(i+1)+(j+1));
cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
cell.setCellValue(((i+j)%2==0?true:false));
}
}
workbook.write(out);
if(out!=null)out.flush();
if(out!=null)out.close();
}
-----------------------分割线-------------------------------
下面讲述下方法,具体实现自己研究:
上面是一个写入excel的例子,你在数据库里面的数据你可以封装成一个对象,然后取出来存在一个list集合中,然后遍历这个list,将每个数据存在Excel中,list的大小对应着工作薄的行数+1(其中第一行为标题行,即姓名,工号什么的)。工作薄的列数也就是你这个数据对象中的属性数目。