Java SpringMVC框架開發(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)鏈接
- 使用java實(shí)現(xiàn)百萬(wàn)級(jí)別數(shù)據(jù)導(dǎo)出excel的三種方式
- Java中用POI實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel
- Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過(guò)程
- java實(shí)現(xiàn)異步導(dǎo)出數(shù)據(jù)
- Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例
- Java使用poi組件導(dǎo)出Excel格式數(shù)據(jù)
- Java使用POI導(dǎo)出大數(shù)據(jù)量Excel的方法
- java?Export大量數(shù)據(jù)導(dǎo)出和打包
相關(guān)文章
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
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工具類過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

