亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java SpringMVC框架開發(fā)之數(shù)據(jù)導出Excel文件格式實例詳解

 更新時間:2020年02月20日 16:01:21   作者:七弦桐  
這篇文章主要介紹了Java基礎(chǔ)開發(fā)之數(shù)據(jù)導出Excel文件格式實例詳解,需要的朋友可以參考下

在平時的開發(fā)中,我們會經(jīng)常遇到這樣一個需求,要在頁面通過一個『導出』按鈕把查詢出的數(shù)據(jù)導出到 Excel 表格中。本文即為實現(xiàn)上述需求的一個小實例。

環(huán)境配置

  • jar包
  • poi.jar
  • jdk 1.6
  • tomcat 7.0
  • eclipse 4.4.0

本 Demo 是在 SpringMVC框架中實現(xiàn)。

頁面

export.jsp 很簡單,就只有一個超鏈接。

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>數(shù)據(jù)導出Excel測試界面</title>
</head>
<body>
  <a href="${pageContext.request.contextPath}/export.action" rel="external nofollow" >導出</a>
</body>
</html>

JavaBean 類

public class Person {
  private String name;
  private String age;
  private String addr;
  private String sex;
  // set/get方法和構(gòu)造器省略。。。

工具類

package com.export.action;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.export.pojo.Person;
/**
 * 數(shù)據(jù)導出到Excel工具類
 * 
 * @author zhang_cq
 * 
 */
public class ExportUtil {
  /**
   * 設(shè)置導出Excel的表名
   * 
   * @return
   */
  public String getSheetName() {
    return "測試導出數(shù)據(jù)";
  }
  /**
   * 設(shè)置導出Excel的列名
   * 
   * @return
   */
  public String getSheetTitleName() {
    return "序號,姓名,年齡,居住地,性別";
  }
  /**
   * 創(chuàng)建 sheet 的第一行,標題行
   * 
   * @param sheet
   * @param strTitle
   */
  private void createSheetTitle(HSSFSheet sheet, String strTitle) {
    HSSFRow row = sheet.createRow(0); // 創(chuàng)建該表格(sheet)的第一行
    sheet.setDefaultColumnWidth(4);
    HSSFCell cell = null;
    String[] strArray = strTitle.split(",");
    for (int i = 0; i < strArray.length; i++) {
      cell = row.createCell(i); // 創(chuàng)建該行的第一列
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(strArray[i]));
    }
  }
  @SuppressWarnings("resource")
  public InputStream getExcelStream(List<Person> personList) throws IOException {
    // 創(chuàng)建一個 Excel 文件
    HSSFWorkbook wb = new HSSFWorkbook();
    // 創(chuàng)建一個表格 Sheet
    HSSFSheet sheet = wb.createSheet(this.getSheetName());
    // 創(chuàng)建 sheet 的第一行,標題行
    // 行號從0開始計算
    this.createSheetTitle(sheet, this.getSheetTitleName());
    // 設(shè)置 sheet 的主體內(nèi)容
    this.createSheetBody(personList, sheet);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    wb.write(output);
    byte[] ba = output.toByteArray();
    InputStream is = new ByteArrayInputStream(ba);
    return is;
  }
  private void createSheetBody(List<Person> personList, HSSFSheet sheet) {
    if (personList == null || personList.size() < 1) {
      return;
    }
    // 表格(sheet) 的第二行, 第一行是標題, Excel中行號, 列號 是由 0 開始的
    int rowNum = 1;
    HSSFCell cell = null;
    HSSFRow row = null;
    for (Iterator<Person> it = personList.iterator(); it.hasNext(); rowNum++) {
      Person person = (Person) it.next();
      if (person == null)
        person = new Person();
      row = sheet.createRow(rowNum);
      int i = 0;
      cell = row.createCell(i++);
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(rowNum + ""));
      cell = row.createCell(i++); // name
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(person.getName()));
      cell = row.createCell(i++); // age
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(person.getAge()));
      cell = row.createCell(i++); // addr
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(person.getAddr()));
      cell = row.createCell(i++); // sex
      cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(new HSSFRichTextString(person.getSex()));
    }
  }
}

Action類

package com.export.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.export.pojo.Person;
/**
 * @author zhang_cq
 * @version V1.0
 */
@Controller
public class ExportData {
  /**
   * 起始頁面
   * 
   * @return
   */
  @RequestMapping("/index")
  public String login() {
    return "index";
  }
  /**
   * 導出Excel
   * 
   * @author zhang_cq
   */
  @RequestMapping("/export")
  public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 設(shè)置導出的編碼格式,此處統(tǒng)一為UTF-8
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    // 設(shè)置導出文件的名稱
    response.setHeader("Content-Disposition",
        "attachment;filename=" + new String("數(shù)據(jù)導出Excel測試.xls".getBytes(), "iso-8859-1"));
    // 模擬表格需要導出的數(shù)據(jù)
    Person p1 = new Person("張三", "22", "北京", "男");
    Person p2 = new Person("李四", "23", "濟南", "女");
    Person p3 = new Person("王五", "24", "上海", "男");
    List<Person> personList = new ArrayList<Person>();
    personList.add(p1);
    personList.add(p2);
    personList.add(p3);
    // 實際應(yīng)用中這個地方會判斷獲取的數(shù)據(jù),如果沒有對應(yīng)的數(shù)據(jù)則不導出,如果超過2000條,則只導出2000條
    if (personList.size() == 0) {
      PrintWriter print = response.getWriter();
      print.write("沒有需要導出的數(shù)據(jù)!");
      return null;
    }
    ServletOutputStream out = response.getOutputStream();
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
      ExportUtil dataExportUtil = new ExportUtil();
      bis = new BufferedInputStream(dataExportUtil.getExcelStream(personList));
      bos = new BufferedOutputStream(out);
      byte[] buff = new byte[2048];
      int bytesRead;
      while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
      }
      bos.flush();
    } catch (final IOException e) {
      System.out.println("數(shù)據(jù)導出列表導出異常!");
    } finally {
      if (bis != null) {
        bis.close();
      }
      if (bos != null) {
        bos.close();
      }
    }
    return "export";
  }
}

至此,就是Java將數(shù)據(jù)導出Excel文件格式的方法,更多關(guān)于這方面的文章請查看下面的相關(guān)鏈接

相關(guān)文章

  • Java實現(xiàn)超級實用的日記本

    Java實現(xiàn)超級實用的日記本

    一個用Java語言編寫的,實現(xiàn)日記本的基本編輯功能、各篇日記之間的上下翻頁、查詢?nèi)沼泝?nèi)容的程序。全部代碼分享給大家,有需要的小伙伴參考下。
    2015-05-05
  • 一篇文章帶你了解spring事務(wù)失效的多種場景

    一篇文章帶你了解spring事務(wù)失效的多種場景

    在日常編碼過程中常常涉及到事務(wù),在前兩天看到一篇文章提到了Spring事務(wù),那么在此總結(jié)下在Spring環(huán)境下事務(wù)失效的幾種原因.
    2021-09-09
  • Java?POI導出Excel時合并單元格沒有邊框的問題解決

    Java?POI導出Excel時合并單元格沒有邊框的問題解決

    這篇文章主要給大家介紹了關(guān)于Java?POI導出Excel時合并單元格沒有邊框的問題解決辦法,文中通過代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友可以參考下
    2023-07-07
  • 【IntelliJ IDEA】Maven構(gòu)建自己的第一個Java后臺的方法

    【IntelliJ IDEA】Maven構(gòu)建自己的第一個Java后臺的方法

    本篇文章主要介紹了Maven構(gòu)建自己的第一個Java后臺的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 淺談Action+Service +Dao 功能

    淺談Action+Service +Dao 功能

    下面小編就為大家?guī)硪黄獪\談Action+Service +Dao 功能。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Spring Boot和Docker實現(xiàn)微服務(wù)部署的方法

    Spring Boot和Docker實現(xiàn)微服務(wù)部署的方法

    這篇文章主要介紹了Spring Boot和Docker實現(xiàn)微服務(wù)部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • Apache?Commons?CLI構(gòu)建命令行應(yīng)用利器教程

    Apache?Commons?CLI構(gòu)建命令行應(yīng)用利器教程

    這篇文章主要為大家介紹了構(gòu)建命令行應(yīng)用利器Apache?Commons?CLI的使用教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • SpringBoot多種場景傳參模式

    SpringBoot多種場景傳參模式

    傳參是非常常見的,本文主要介紹了SpringBoot多種場景傳參模式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Maven版本依賴pom文件內(nèi)容及使用剖析

    Maven版本依賴pom文件內(nèi)容及使用剖析

    這篇文章主要為大家介紹了Maven版本依賴pom文件內(nèi)容及使用剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • java使用OpenCV從視頻文件中獲取幀

    java使用OpenCV從視頻文件中獲取幀

    這篇文章主要為大家詳細介紹了java使用OpenCV從視頻文件中獲取幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評論