最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java SpringMVC框架開發(fā)之?dāng)?shù)據(jù)導(dǎo)出Excel文件格式實(shí)例詳解

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

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

環(huán)境配置

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

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

頁(yè)面

export.jsp 很簡(jiǎn)單,就只有一個(gè)超鏈接。

<%@ 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ù)導(dǎo)出Excel測(cè)試界面</title>
</head>
<body>
  <a href="${pageContext.request.contextPath}/export.action" rel="external nofollow" >導(dǎo)出</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ù)導(dǎo)出到Excel工具類
 * 
 * @author zhang_cq
 * 
 */
public class ExportUtil {
  /**
   * 設(shè)置導(dǎo)出Excel的表名
   * 
   * @return
   */
  public String getSheetName() {
    return "測(cè)試導(dǎo)出數(shù)據(jù)";
  }
  /**
   * 設(shè)置導(dǎo)出Excel的列名
   * 
   * @return
   */
  public String getSheetTitleName() {
    return "序號(hào),姓名,年齡,居住地,性別";
  }
  /**
   * 創(chuàng)建 sheet 的第一行,標(biāo)題行
   * 
   * @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)建一個(gè) Excel 文件
    HSSFWorkbook wb = new HSSFWorkbook();
    // 創(chuàng)建一個(gè)表格 Sheet
    HSSFSheet sheet = wb.createSheet(this.getSheetName());
    // 創(chuàng)建 sheet 的第一行,標(biāo)題行
    // 行號(hào)從0開始計(jì)算
    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) 的第二行, 第一行是標(biāo)題, Excel中行號(hào), 列號(hào) 是由 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 {
  /**
   * 起始頁(yè)面
   * 
   * @return
   */
  @RequestMapping("/index")
  public String login() {
    return "index";
  }
  /**
   * 導(dǎo)出Excel
   * 
   * @author zhang_cq
   */
  @RequestMapping("/export")
  public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 設(shè)置導(dǎo)出的編碼格式,此處統(tǒng)一為UTF-8
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    // 設(shè)置導(dǎo)出文件的名稱
    response.setHeader("Content-Disposition",
        "attachment;filename=" + new String("數(shù)據(jù)導(dǎo)出Excel測(cè)試.xls".getBytes(), "iso-8859-1"));
    // 模擬表格需要導(dǎo)出的數(shù)據(jù)
    Person p1 = new Person("張三", "22", "北京", "男");
    Person p2 = new Person("李四", "23", "濟(jì)南", "女");
    Person p3 = new Person("王五", "24", "上海", "男");
    List<Person> personList = new ArrayList<Person>();
    personList.add(p1);
    personList.add(p2);
    personList.add(p3);
    // 實(shí)際應(yīng)用中這個(gè)地方會(huì)判斷獲取的數(shù)據(jù),如果沒(méi)有對(duì)應(yīng)的數(shù)據(jù)則不導(dǎo)出,如果超過(guò)2000條,則只導(dǎo)出2000條
    if (personList.size() == 0) {
      PrintWriter print = response.getWriter();
      print.write("沒(méi)有需要導(dǎo)出的數(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ù)導(dǎo)出列表導(dǎo)出異常!");
    } finally {
      if (bis != null) {
        bis.close();
      }
      if (bos != null) {
        bos.close();
      }
    }
    return "export";
  }
}

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

相關(guān)文章

  • Java求解二叉樹的最近公共祖先實(shí)例代碼

    Java求解二叉樹的最近公共祖先實(shí)例代碼

    樹是一種非線性的數(shù)據(jù)結(jié)構(gòu),它是由n(n>=0)個(gè)有限結(jié)點(diǎn)組成一個(gè)具有層次關(guān)系的集合,這篇文章主要給大家介紹了關(guān)于Java求解二叉樹的最近公共祖先的相關(guān)資料,需要的朋友可以參考下
    2021-06-06
  • Java利用SPI實(shí)現(xiàn)解耦的示例詳解

    Java利用SPI實(shí)現(xiàn)解耦的示例詳解

    SPI的全稱是服務(wù)提供接口,可以用其來(lái)啟動(dòng)框架的擴(kuò)展和替換組件。本文將利用SPI實(shí)現(xiàn)解耦,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2023-04-04
  • Maven 主模塊和子模塊pom.xml依賴聲明

    Maven 主模塊和子模塊pom.xml依賴聲明

    這篇文章主要介紹了Maven 主模塊和子模塊pom.xml依賴聲明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • maven如何使用profiles多環(huán)境配置

    maven如何使用profiles多環(huán)境配置

    在軟件開發(fā)過(guò)程中,我們經(jīng)常需要在不同的環(huán)境中部署和運(yùn)行我們的應(yīng)用程序,例如開發(fā)環(huán)境、測(cè)試環(huán)境和生產(chǎn)環(huán)境,為了方便管理和配置不同環(huán)境下的參數(shù),我們可以使用Maven的profiles功能,本文給大家介紹maven如何使用profiles多環(huán)境配置,感興趣的的朋友一起看看吧
    2024-02-02
  • Java封裝統(tǒng)一的Result Model案例

    Java封裝統(tǒng)一的Result Model案例

    這篇文章主要介紹了Java封裝統(tǒng)一的Result Model案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Java自帶定時(shí)任務(wù)ScheduledThreadPoolExecutor實(shí)現(xiàn)定時(shí)器和延時(shí)加載功能

    Java自帶定時(shí)任務(wù)ScheduledThreadPoolExecutor實(shí)現(xiàn)定時(shí)器和延時(shí)加載功能

    今天小編就為大家分享一篇關(guān)于Java自帶定時(shí)任務(wù)ScheduledThreadPoolExecutor實(shí)現(xiàn)定時(shí)器和延時(shí)加載功能,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • 最新JVM垃圾回收算法詳解

    最新JVM垃圾回收算法詳解

    ? 垃圾收集器對(duì)堆進(jìn)行回收前,首先要確定堆中的對(duì)象哪些還"存活",哪些已經(jīng)"死去"。有兩種算法,分別是引用計(jì)數(shù)算法(Recference?Counting)和可達(dá)性分析算法(Reachability?Analysis),這篇文章主要介紹了JVM垃圾回收算法,需要的朋友可以參考下
    2022-05-05
  • Springboot+Redis實(shí)現(xiàn)API接口限流的示例代碼

    Springboot+Redis實(shí)現(xiàn)API接口限流的示例代碼

    本文主要介紹了Springboot+Redis實(shí)現(xiàn)API接口限流的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • springboot封裝JsonUtil,CookieUtil工具類代碼實(shí)例

    springboot封裝JsonUtil,CookieUtil工具類代碼實(shí)例

    這篇文章主要介紹了springboot封裝JsonUtil,CookieUtil工具類過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • idea打包java程序(包含依賴的所有jar包)

    idea打包java程序(包含依賴的所有jar包)

    這篇文章主要介紹了idea打包java程序(包含依賴的所有jar包),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06

最新評(píng)論

宜章县| 公安县| 天门市| 三河市| 如东县| 新津县| 饶阳县| 寿宁县| 清丰县| 易门县| 溧水县| 濮阳县| 大冶市| 隆安县| 乡宁县| 铁岭市| 苏尼特左旗| 偃师市| 叙永县| 吐鲁番市| 始兴县| 永胜县| 横山县| 正镶白旗| 阳新县| 桦川县| 津南区| 秦皇岛市| 元朗区| 澄迈县| 涟源市| 扎鲁特旗| 清镇市| 从化市| 石泉县| 凤阳县| 苍梧县| 漳州市| 定结县| 福安市| 会东县|