导出数据到Excel

来源:互联网 发布:今日金十数据解读 编辑:程序博客网 时间:2024/05/21 08:50

public void dataToExcel(String colName,String colValue,String path){
  try{
   FileOutputStream fos = new FileOutputStream(path);
   WritableWorkbook wbook = Workbook.createWorkbook(fos); //建立excel文件
   WritableSheet wsheet = wbook.createSheet("sheet_1", 0); //工作表名称
   //设置Excel字体
   WritableFont wfont = new WritableFont(WritableFont.ARIAL, 12,
   WritableFont.BOLD, false,
   jxl.format.UnderlineStyle.NO_UNDERLINE,
   jxl.format.Colour.BLUE);
   WritableCellFormat titleFormat = new WritableCellFormat(wfont);
   String[] title = colName.split(",");
   String[] data = colValue.split(",");
   int cols = title.length;
   int num = data.length;
   int rows = num/cols;
   
   //设置Excel表头
   for (int i = 0; i < cols; i++) {
    Label excelTitle = new Label(i, 0, title[i], titleFormat);
    wsheet.addCell(excelTitle);
   }
   //
   for(int j = 0;j < rows ;j++){
    for(int k = 0;k < cols ;k++){
     Label excelData = new Label(k,j+1,data[(cols*j+k)]);
     wsheet.addCell(excelData);
    }
   }
   wbook.write(); //写入文件
   wbook.close();
   fos.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

 

 }

0 0
原创粉丝点击