欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java導(dǎo)出包含多個(gè)sheet的Excel代碼示例

 更新時(shí)間:2019年03月11日 11:55:55   作者:linyuxb123  
這篇文章主要介紹了java導(dǎo)出包含多個(gè)sheet的Excel,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文實(shí)例為大家分享了java導(dǎo)出包含多個(gè)sheet的Excel的具體代碼,供大家參考,具體內(nèi)容如下

要導(dǎo)出多個(gè)sheet,關(guān)鍵就是Excel導(dǎo)出的時(shí)間設(shè)定,在執(zhí)行導(dǎo)出文件之前,創(chuàng)建多個(gè)工作表

HSSFSheet sheet = workbook.createSheet(sheettitle); 

這樣每創(chuàng)建一個(gè)工作表,便會(huì)生成一個(gè)新的sheet表,在最后導(dǎo)出Excel的時(shí)候一次性導(dǎo)出。

示例:
Java類:

try { 
  HSSFWorkbook workbook = new HSSFWorkbook(); 
  OutputStream out = response.getOutputStream(); 
  for(int j=0;j<n;j++){ 
    BaseResult<List<T>> teasalList = service.select(teasal); 
    //接下來循環(huán)list放到Excel表中 
    if(teasalList.isSuccess()&&teasalList.getResult().size()>0){ 
      //文件標(biāo)題 
      SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd"); 
      String nowdate = formatter1.format(new Date()); 
      String title = null; 
      title = "excel表格標(biāo)題-" + nowdate + ".xls"; 
      String sheettitle = "sheet表名"; 
      //設(shè)置表格標(biāo)題行 
      String oneheaders = "首行標(biāo)題" ; 
      String dateheaders = nowdate ; 
      String[] headers = new String[] {"列1","列2","列3","列4"}; 
      List<Object[]> dataList = new ArrayList<Object[]>(); 
      Object[] objs = null; 
      for(int i =0; i<3 ; i++){ //循環(huán)每一條數(shù)據(jù) 
        objs = new Object[headers.length]; 
        objs[1] = "張三";  //姓名 
        objs[2] = "3"; //序號(hào) 
        //數(shù)據(jù)添加到excel表格 
        dataList.add(objs); 
      } 
      //使用流將數(shù)據(jù)導(dǎo)出 
      //防止中文亂碼 
      String headStr = "attachment; filename=\"" + new String( title.getBytes("gb2312"), "ISO8859-1" ) + "\""; 
      response.setContentType("octets/stream"); 
      response.setContentType("APPLICATION/OCTET-STREAM"); 
      response.setHeader("Content-Disposition", headStr); 
      ExportExcelDownFee ex ; 
      ex = new ExportExcelDownFee(sheettitle, oneheaders, dateheaders,headers, dataList);//沒有標(biāo)題 
      ex.export(workbook,out); 
    } 
  } 
  workbook.write(out); //循環(huán)生成多個(gè)sheet之后在導(dǎo)出Excel
  out.close(); //關(guān)閉流
} catch (Exception e) { 
  e.printStackTrace(); 
}

 工具類:

public class ExportExcelDownFee { 
 
  //導(dǎo)出表的列名 
  private String[] rowName ; 
  //導(dǎo)出表的小標(biāo)題 
  private String oneheaders; 
  //導(dǎo)出表的日期 
  private String dateheaders; 
  //sheet表表名 
  private String sheettitle; 
 
  private List<Object[]> dataList = new ArrayList<Object[]>(); 
 
  HttpServletResponse response; 
   
  //構(gòu)造方法2,傳入要導(dǎo)出的數(shù)據(jù) 
  public ExportExcelDownFee( String sheettitle, String oneheaders, String dateheaders, String[] rowName,List<Object[]> dataList){ 
    this.dataList = dataList; 
    this.oneheaders = oneheaders; 
    this.dateheaders = dateheaders; 
    this.rowName = rowName; 
    this.sheettitle = sheettitle; 
  } 
   
  /* 
   * 導(dǎo)出數(shù)據(jù) 
   * */ 
  public void export(HSSFWorkbook workbook,OutputStream out) throws Exception{ 
    try{ 
      HSSFSheet sheet = workbook.createSheet(sheettitle);         // 創(chuàng)建工作表 
 
      HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對(duì)象 
      HSSFCellStyle style = this.getStyle(workbook);         //單元格樣式對(duì)象 
 
      //第一行 
      HSSFRow rowfirstName = sheet.createRow(0); 
      HSSFCell oneCellRowName = rowfirstName.createCell(0);        //創(chuàng)建列頭對(duì)應(yīng)個(gè)數(shù)的單元格 
      oneCellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);       //設(shè)置列頭單元格的數(shù)據(jù)類型 
      HSSFRichTextString onetext = new HSSFRichTextString(oneheaders); 
      oneCellRowName.setCellValue(onetext);                 //設(shè)置列頭單元格的值 
      //合并單元格CellRangeAddress構(gòu)造參數(shù)依次表示起始行,截至行,起始列, 截至列  
      sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));  
      oneCellRowName.setCellStyle(columnTopStyle);            //設(shè)置列頭單元格樣式 
       
      //第二行 
      HSSFRow rowDateName = sheet.createRow(1); 
      HSSFCell DateCellRowName = rowDateName.createCell(3); 
      DateCellRowName.setCellValue(dateheaders); 
      DateCellRowName.setCellStyle(columnTopStyle);  
       
      // 定義所需列數(shù) 
      int columnNum = rowName.length; 
      HSSFRow rowRowName = sheet.createRow(2);        // 在索引2的位置創(chuàng)建行(最頂端的行開始的第二行) 
 
      // 將列頭設(shè)置到sheet的單元格中 
      for(int n=0;n<columnNum;n++){ 
        HSSFCell cellRowName = rowRowName.createCell(n);        //創(chuàng)建列頭對(duì)應(yīng)個(gè)數(shù)的單元格 
        cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);       //設(shè)置列頭單元格的數(shù)據(jù)類型 
        HSSFRichTextString text = new HSSFRichTextString(rowName[n]); 
        cellRowName.setCellValue(text);                 //設(shè)置列頭單元格的值 
        cellRowName.setCellStyle(style);            //設(shè)置列頭單元格樣式 
      } 
 
      //將查詢出的數(shù)據(jù)設(shè)置到sheet對(duì)應(yīng)的單元格中 
      for(int i=0;i<dataList.size();i++){ 
 
        Object[] obj = dataList.get(i);//遍歷每個(gè)對(duì)象 
        HSSFRow row = sheet.createRow(i+3);//創(chuàng)建所需的行數(shù)(從第二行開始寫數(shù)據(jù)) 
 
        for(int j=0; j<obj.length; j++){ 
          HSSFCell cell = null;  //設(shè)置單元格的數(shù)據(jù)類型 
          if(j == 0){ 
            cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC); 
            cell.setCellValue(i+1);  
          }else{ 
            cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING); 
            if(!"".equals(obj[j]) && obj[j] != null){ 
              cell.setCellValue(obj[j].toString());            //設(shè)置單元格的值 
            } 
          } 
          cell.setCellStyle(style);                  //設(shè)置單元格樣式 
        } 
      } 
       
      //讓列寬隨著導(dǎo)出的列長自動(dòng)適應(yīng) 
      for (int colNum = 0; colNum < columnNum; colNum++) { 
        int columnWidth = sheet.getColumnWidth(colNum) / 256; 
        for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) { 
          HSSFRow currentRow; 
          //當(dāng)前行未被使用過 
          if (sheet.getRow(rowNum) == null) { 
            currentRow = sheet.createRow(rowNum); 
          } else { 
            currentRow = sheet.getRow(rowNum); 
          } 
          if (currentRow.getCell(colNum) != null) { 
            HSSFCell currentCell = currentRow.getCell(colNum); 
            if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) { 
              int length = 0; 
              try { 
                length = currentCell.getStringCellValue().getBytes().length; 
              } catch (Exception e) { 
                e.printStackTrace(); 
              } 
              if (columnWidth < length) { 
                columnWidth = length; 
              } 
            } 
          } 
 
        } 
        if(colNum == 0){ 
          sheet.setColumnWidth(colNum, (columnWidth-2) * 256); 
        }else{ 
          sheet.setColumnWidth(colNum, (columnWidth+4) * 256); 
        } 
      } 
 
    }catch(Exception e){ 
      e.printStackTrace(); 
    } 
  } 
 
  /* 
   * 列頭單元格樣式 
   */   
  public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { 
 
     // 設(shè)置字體 
     HSSFFont font = workbook.createFont(); 
     //設(shè)置字體大小 
     font.setFontHeightInPoints((short)11); 
     //字體加粗 
     //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
     //設(shè)置字體名字  
     font.setFontName("宋體"); 
     //設(shè)置樣式;  
     HSSFCellStyle style = workbook.createCellStyle(); 
     //在樣式用應(yīng)用設(shè)置的字體;  
     style.setFont(font); 
     //設(shè)置自動(dòng)換行;  
     style.setWrapText(false); 
     //設(shè)置水平對(duì)齊的樣式為居中對(duì)齊;  
     style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
     //設(shè)置垂直對(duì)齊的樣式為居中對(duì)齊;  
     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
 
     return style; 
 
  } 
 
  /*  
   * 列數(shù)據(jù)信息單元格樣式 
   */  
  public HSSFCellStyle getStyle(HSSFWorkbook workbook) { 
     // 設(shè)置字體 
     HSSFFont font = workbook.createFont(); 
     //設(shè)置字體名字  
     font.setFontName("宋體"); 
     //設(shè)置樣式;  
     HSSFCellStyle style = workbook.createCellStyle(); 
     //設(shè)置底邊框;  
     style.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
     //設(shè)置底邊框顏色;  
     style.setBottomBorderColor(HSSFColor.BLACK.index); 
     //設(shè)置左邊框;   
     style.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
     //設(shè)置左邊框顏色;  
     style.setLeftBorderColor(HSSFColor.BLACK.index); 
     //設(shè)置右邊框;  
     style.setBorderRight(HSSFCellStyle.BORDER_THIN); 
     //設(shè)置右邊框顏色;  
     style.setRightBorderColor(HSSFColor.BLACK.index); 
     //設(shè)置頂邊框;  
     style.setBorderTop(HSSFCellStyle.BORDER_THIN); 
     //設(shè)置頂邊框顏色;  
     style.setTopBorderColor(HSSFColor.BLACK.index); 
     //在樣式用應(yīng)用設(shè)置的字體;  
     style.setFont(font); 
     //設(shè)置自動(dòng)換行;  
     style.setWrapText(false); 
     //設(shè)置水平對(duì)齊的樣式為居中對(duì)齊;  
     style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 
     //設(shè)置垂直對(duì)齊的樣式為居中對(duì)齊;  
     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
 
     return style; 
 
   } 
}

以上所述是小編給大家介紹的java導(dǎo)出包含多個(gè)sheet的Excel代碼示例詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解

    Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解

    這篇文章主要介紹了Java Apollo環(huán)境搭建以及集成SpringBoot案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 關(guān)于@SpringBootApplication詳解

    關(guān)于@SpringBootApplication詳解

    這篇文章主要介紹了關(guān)于@SpringBootApplication的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • SpringBoot整合WxJava開啟消息推送的實(shí)現(xiàn)

    SpringBoot整合WxJava開啟消息推送的實(shí)現(xiàn)

    本文主要介紹了SpringBoot整合WxJava開啟消息推送,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • java編程兩種樹形菜單結(jié)構(gòu)的轉(zhuǎn)換代碼

    java編程兩種樹形菜單結(jié)構(gòu)的轉(zhuǎn)換代碼

    這篇文章主要介紹了java編程兩種樹形菜單結(jié)構(gòu)的轉(zhuǎn)換代碼,首先介紹了兩種樹形菜單結(jié)構(gòu)的代碼,然后展示了轉(zhuǎn)換器實(shí)例代碼,最后分享了相關(guān)實(shí)例及結(jié)果演示,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Java中泛型的使用和優(yōu)點(diǎn)解析

    Java中泛型的使用和優(yōu)點(diǎn)解析

    這篇文章主要介紹了Java中泛型的使用和優(yōu)點(diǎn)解析,泛型使用過程中,操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù),這種參數(shù)類型可以用在類、接口和方法中,分別被稱為泛型類、泛型接口、泛型方法,需要的朋友可以參考下
    2023-09-09
  • Spring攔截器中注入Bean失敗解放方案詳解

    Spring攔截器中注入Bean失敗解放方案詳解

    這兩天遇到SpringBoot攔截器中Bean無法注入問題。下面介紹關(guān)于SpringBoot攔截器中Bean無法注入的問題解決方案,感興趣的朋友一起看看吧
    2022-06-06
  • 基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例

    基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例

    這篇文章主要為大家詳細(xì)介紹了基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Java中equals方法使用及重寫練習(xí)

    Java中equals方法使用及重寫練習(xí)

    equals是在object類中的方法,在object中equals是用來看看兩個(gè)參數(shù)是否引用的是同一個(gè)對(duì)象,下面這篇文章主要給大家介紹了關(guān)于Java中equals方法使用及重寫練習(xí)的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • maven打包zip包含bin下啟動(dòng)腳本的完整代碼

    maven打包zip包含bin下啟動(dòng)腳本的完整代碼

    這篇文章主要介紹了maven打包zip包含bin下啟動(dòng)腳本,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Springboot 全局日期格式化處理的實(shí)現(xiàn)

    Springboot 全局日期格式化處理的實(shí)現(xiàn)

    這篇文章主要介紹了Springboot 全局日期格式化處理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評(píng)論