poi Excel写数据

public static void writeExcel(List<String[]> list,String[] format,String fileName){
  HSSFWorkbook workbook = new HSSFWorkbook();
  HSSFSheet sheet = workbook.createSheet("Sheet1");
  //获取第一行
  HSSFRow row = sheet.createRow(0);
  //第一行为的标题
  for(int i = 0 ; i < format.length ; i++){
   row.createCell(i).setCellValue(format[i]);
  }
  //将list中的数据添加到Excel中
  for(int j = 0 ; j < list.size() ; j++ ){
   HSSFRow row2 = sheet.createRow(j+1);
   String[] temp = list.get(j);
   for(int k = 0 ; k < temp.length ; k++){
    row2.createCell(k).setCellValue(temp[k]);
   }
  }
  try {
   //保存到指定的文件中
   FileOutputStream outStream = new FileOutputStream(fileName);
   workbook.write(outStream);
   outStream.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 

posted @ 2016-03-03 16:47  birkhoff001  阅读(169)  评论(0编辑  收藏  举报