zoukankan      html  css  js  c++  java
  • 导出excel

    1. <span style="font-size:18px;">/** 
    2.  * @param sql  查询语句 
    3.  * @param contentTitle excel表格标题名称 
    4.  * @param filepath   生成excel表格文件的路径 
    5.  * @param filename  excel表格的名称 
    6.  * @return 生成excel表格文件的路径 
    7.  */  
    8. public String ToExcel(String sql,String contentTitle,String filepath,String filename) {  
    9.         WritableWorkbook workbook = null;  
    10.         try{  
    11.               
    12.             /** 
    13.              * 执行sql语句,查询数据库结果 
    14.              */  
    15.             List<Map> list = null;  
    16.             //获取结果中的字段  
    17.             if(list!=null && list.isEmpty()){  
    18.                 //获取导出excel表格中所有的列字段名称  
    19.                 Map<String,String> columnMap = list.get(0);  
    20.                 Set<String> keySet = columnMap.keySet();  
    21.                   
    22.                 /* 
    23.                  * 1,创建excel文件 
    24.                  */  
    25.                 File file = new File(filepath);  
    26.                 if (!file.exists()) {  
    27.                     file.mkdirs();  
    28.                 }  
    29.                 //获取文件的完整路径  
    30.                 filename =file+"/"+filename+ ".xls";  
    31.                 //创建excel文件  
    32.                 File excelfile = new File(filename);  
    33.                 excelfile.createNewFile();  
    34.                 workbook = Workbook.createWorkbook(new FileOutputStream(excelfile));  
    35.                   
    36.                 /* 
    37.                  * 2,写入excel文件标题和工作簿名称 
    38.                  */  
    39.               
    40.                 WritableSheet sheet = workbook.createSheet("sheet名称", 0);  
    41.                 Label nL = null;  
    42.                 jxl.write.WritableFont headerFont = new jxl.write.WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);  
    43.                 jxl.write.WritableCellFormat headerFormat = new jxl.write.WritableCellFormat(headerFont);  
    44.                 jxl.write.WritableFont titleFont = new jxl.write.WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);  
    45.                 jxl.write.WritableCellFormat titleFormat = new jxl.write.WritableCellFormat(titleFont);  
    46.   
    47.                 /* 
    48.                  * 3,写入标题 
    49.                  */  
    50.                 nL = new Label(0, 0, contentTitle, headerFormat);  
    51.                 sheet.addCell(nL);  
    52.                 /* 
    53.                  * 4,写入内容 
    54.                  */  
    55.                 Label label;  
    56.                 int index = 0;  
    57.                 for(String key:keySet){  
    58.                     nL = new Label(index, 1, key, titleFormat);  
    59.                     sheet.addCell(nL);  
    60.                     for (int j = 0; j < list.size(); j++) {  
    61.                         //得到单元格的列所对应的值  
    62.                         String value = (String) list.get(j).get(key);  
    63.                         //把值放入单元格中  
    64.                         Label obj = new Label(index, j + 2, value);  
    65.                         sheet.addCell(obj);  
    66.                     }  
    67.                     index++;  
    68.                 }  
    69.                 workbook.write();  
    70.                 workbook.close();  
    71.             }  
    72.         }catch(Exception e){  
    73.             e.printStackTrace();  
    74.         }  
    75.         return filename;  
    76.     }</span>  
  • 相关阅读:
    [考试反思]0904NOIP模拟测试37:守望
    游戏:最短路,拆点
    [考试反思]0903NOIP模拟测试36:复始
    [考试反思]0902NOIP模拟测试35:摆动
    长寿花:dp
    [考试反思]0901NOIP模拟测试34:游离
    赤壁情:dp
    [考试反思]0829NOIP模拟测试33:仰望
    [考试反思]0828NOIP模拟测试32:沉底
    宅男计划:单峰函数三分
  • 原文地址:https://www.cnblogs.com/challengeof/p/4281857.html
Copyright © 2011-2022 走看看