在實(shí)際的項(xiàng)目開(kāi)發(fā)中,很多地方都會(huì)用到一個(gè)功能,那就是數(shù)據(jù)的導(dǎo)出,而在數(shù)據(jù)導(dǎo)出時(shí),大部分都是借用的框架,今天我給大家演示的就是基于SSM框架的數(shù)據(jù)導(dǎo)出功能。
POI介紹:
POI是 Apache 軟件基金會(huì)用Java編寫(xiě)的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI 提供API給Java 程序?qū)icrosoft Office格式檔案讀和寫(xiě)的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫(xiě),意為“簡(jiǎn)潔版的模糊實(shí)現(xiàn)”,我們一般用此工具來(lái)來(lái)操作Excel文件。
代碼實(shí)現(xiàn):
- 首先在基于maven工程的pom.xml中添加兩個(gè)依賴(lài)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
- 然后新建一個(gè)類(lèi)WriteExcel.java,此類(lèi)主要定義導(dǎo)出Excel的格式,以及創(chuàng)建Excel格式的輸出流
package cn.ppdxzz.poi;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Description:通用Excel輸入流
*
* @Date: 2020/2/16 15:17
* @Author: PeiChen
*/
public class WriteExcel {
//導(dǎo)出表的列名
private String[] rowName;
//每行作為一個(gè)Object對(duì)象
private List<Object[]> dataList = new ArrayList<Object[]>();
//構(gòu)造方法,傳入要導(dǎo)出的數(shù)據(jù):第一個(gè)參數(shù)傳入一個(gè)列名數(shù)組,第二個(gè)參數(shù)傳入一個(gè)list集合(Object對(duì)象數(shù)組)
public WriteExcel(String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
}
/*
* 導(dǎo)出數(shù)據(jù)
* */
public InputStream export() throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook(); // 創(chuàng)建工作簿對(duì)象
HSSFSheet sheet = workbook.createSheet("sheet1"); // 創(chuàng)建工作表
// 設(shè)置sheet表樣式
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對(duì)象
HSSFCellStyle style = this.getColumnStyle(workbook); //單元格樣式對(duì)象
// 定義所需列數(shù)
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(0); // 在索引2的位置創(chuàng)建行(最頂端的行開(kāi)始的第二行)
// 將列頭設(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ù)類(lèi)型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //設(shè)置列頭單元格的值
cellRowName.setCellStyle(columnTopStyle); //設(shè)置列頭單元格樣式
}
//將查詢(xún)出的數(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+1); //創(chuàng)建所需的行數(shù)
for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //設(shè)置單元格的數(shù)據(jù)類(lèi)型
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)出的列長(zhǎng)自動(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)前行未被使用過(guò),則新建一行
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 = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
ByteArrayOutputStream os=new ByteArrayOutputStream();
try {
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content=os.toByteArray();
InputStream is=new ByteArrayInputStream(content);
return is;
}
/*
* 列頭單元格樣式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 設(shè)置字體
HSSFFont font = workbook.createFont();
//設(shè)置字體大小
font.setFontHeightInPoints((short)11);
//字體加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設(shè)置字體名字
font.setFontName("Courier New");
//設(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;
}
/*
* 列信息單元格樣式
*/
public HSSFCellStyle getColumnStyle(HSSFWorkbook workbook) {
// 設(shè)置字體
HSSFFont font = workbook.createFont();
//設(shè)置字體名字
font.setFontName("Courier New");
//設(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;
}
}
- 接下來(lái)就是底層數(shù)據(jù)庫(kù)訪問(wèn)層查詢(xún)需要導(dǎo)出信息的一個(gè)類(lèi)中的方法了,新建一個(gè)類(lèi)VisitorDao.java,在類(lèi)中添加查詢(xún)所有信息的方法(該示例僅供參考,實(shí)際應(yīng)用根據(jù)個(gè)人情況進(jìn)行編寫(xiě))。
@Select("select * from visitors order by begin_date desc")
List<Visitor> findAll() throws Exception;
- 下面是業(yè)務(wù)層的代碼,新建一個(gè)接口VisitorService.java,創(chuàng)建該方法,返回一個(gè)輸入流。
InputStream getInputStream() throws Exception;
- 下面是業(yè)務(wù)實(shí)現(xiàn)的具體代碼,首先新建一個(gè)實(shí)現(xiàn)類(lèi)VisitorServiceImpl.java,然后讓它去實(shí)現(xiàn)業(yè)務(wù)層接口,重寫(xiě)getInputStream()方法。
/**
* 導(dǎo)出訪客記錄
* @return
* @throws Exception
*/
@Override
public InputStream getInputStream() throws Exception {
//Excel中的每列列名,依次對(duì)應(yīng)數(shù)據(jù)庫(kù)的字段
String[] title = new String[]{"ID","姓名","學(xué)號(hào)","聯(lián)系方式","訪問(wèn)地址","來(lái)訪時(shí)間","離開(kāi)時(shí)間","來(lái)訪原因"};
List<Visitor> visitors = visitorDao.findAll();//查詢(xún)所有需要導(dǎo)出信息的數(shù)據(jù)
List<Object[]> datalist = new ArrayList<>();
for (int i = 0; i < visitors.size(); i++) {
Object[] obj = new Object[8];
obj[0] = visitors.get(i).getId();
obj[1] = visitors.get(i).getName();
obj[2] = visitors.get(i).getSno();
obj[3] = visitors.get(i).getPhone();
obj[4] = visitors.get(i).getPlace();
obj[5] = visitors.get(i).getBegin_date();
obj[6] = visitors.get(i).getEnd_date();
obj[7] = visitors.get(i).getVisit_result();
datalist.add(obj);
}
WriteExcel excel = new WriteExcel(title,datalist);
return excel.export();
}
- 最后就是表現(xiàn)層的代碼,新建一個(gè)類(lèi)VisitorController.java,創(chuàng)建該方法,具體使用比較簡(jiǎn)單,前臺(tái)訪問(wèn)該方法即可。
/**
* 導(dǎo)出訪客信息
* @param response
* @throws Exception
*/
@RequestMapping("/visitorInfo")
public void export(HttpServletResponse response) throws Exception {
InputStream is = visitorService.getInputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("contentDisposition","attachment;filename=visitorInfo.xls");
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(is,outputStream);
}