poi導出excel之java

pom文件:java

<!-- excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>git

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>github

工具類:apache

package com.vai.aio.frame.utils;app

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;工具

import com.github.pagehelper.util.StringUtil;
/**
* 做者: 豆豆<br>
* 時間: 2018年6月6日 下午3:01:12<br>
* 描述 :<br>
*/
public class ExcelUtil {
/**
* <br/>
* 做者:豆豆<br/>
* 時間:2018年6月6日下午4:07:05<br/>
* 描述:<br/>
* @param response
* @param expList:待導出的list列表
* @param fileName:文件名/表格標題
* @param heads:表頭
* @param fields:類的字段名,順序跟heads與之對應
* @throws Exception void<br/>
* --------------------其餘說明--------------------<br/>

*/
public static void doExport(HttpServletResponse response, List<?> expList,
String fileName, String[] heads,String[] fields) throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
int rowNo = 0; //行號
int colNo = 0; //列號
Row nRow = null;
Cell nCell = null;
for(int t=0;t<heads.length;t++){
if(t==0){
sheet.setColumnWidth(t, 10*256);
}else{
sheet.setColumnWidth(t, 20*256);
}

}
if(!StringUtil.isEmpty(fileName)){
nRow = sheet.createRow(rowNo);
nRow.setHeightInPoints(40);
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, heads.length-1)); //合併單元格,新對象,不會覆蓋合併的那些單元格,只是遮住
rowNo++;
nCell = nRow.createCell(0);
nCell.setCellValue(fileName);
nCell.setCellStyle(bigTilteStyle(wb));
}
//寫表頭
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(28); //設置行高
for(int i=0;i<heads.length;i++){
nCell = nRow.createCell(i);
nCell.setCellValue(heads[i]);
nCell.setCellStyle(headStyle(wb)); //綁定樣式
}

for(int j=0;j<expList.size();j++){
colNo = 0; //初始化
Object obj = expList.get(j); //獲取到每條記錄

nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(21);

nCell = nRow.createCell(colNo++);
nCell.setCellValue(j+1);
nCell.setCellStyle(textStyle(wb));

for(int k=0;k<fields.length;k++){
Field field=obj.getClass().getDeclaredField(fields[k]);
if(field==null){
new RuntimeException("字段名映射錯誤");
}
field.setAccessible(true);
nCell = nRow.createCell(colNo++);
nCell.setCellValue(field.get(obj)==null?"":field.get(obj).toString());
nCell.setCellStyle(textStyle(wb));
}
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //生成流對象
wb.write(byteArrayOutputStream);
download(byteArrayOutputStream, response, StringUtil.isEmpty(fileName)?"noName.xls":fileName+".xls"); //彈出下載框,用戶就能夠直接下載
}字體

/**
* @param byteArrayOutputStream 將文件內容寫入ByteArrayOutputStream
* @param response HttpServletResponse 寫入response
* @param returnName 返回的文件名
*/
public static void download(ByteArrayOutputStream byteArrayOutputStream, HttpServletResponse response, String returnName)
throws IOException {
response.setContentType("application/octet-stream;charset=utf-8");
returnName = response.encodeURL(new String(returnName.getBytes(), "iso8859-1")); // 保存的文件名,必須和頁面編碼一致,不然亂碼
response.addHeader("Content-Disposition", "attachment;filename=" + returnName);
response.setContentLength(byteArrayOutputStream.size());編碼

ServletOutputStream outputstream = response.getOutputStream(); // 取得輸出流
byteArrayOutputStream.writeTo(outputstream); // 寫到輸出流
byteArrayOutputStream.close(); // 關閉
outputstream.flush(); // 刷數據
}excel

// 大標題樣式
public static CellStyle bigTilteStyle(Workbook wb) {
// 建立一個單元格樣式對象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 橫向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 縱向居中code

Font curFont = wb.createFont(); // 建立字體對象
curFont.setFontName("華文隸書"); // 設置字體
curFont.setFontHeightInPoints((short) 20); // 設置字體大小

curStyle.setFont(curFont); // 將字體對象綁定到樣式對象上

return curStyle;
}

// 表頭樣式
public static CellStyle headStyle(Workbook wb) {
// 建立一個單元格樣式對象
CellStyle curStyle = wb.createCellStyle();
curStyle.setAlignment(CellStyle.ALIGN_CENTER); // 橫向居中
curStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 縱向居中

Font curFont = wb.createFont(); // 建立字體對象
curFont.setFontName("微軟雅黑"); // 設置字體
curFont.setFontHeightInPoints((short) 10); // 設置字體大小
curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
curStyle.setFont(curFont); // 將字體對象綁定到樣式對象上

// 畫線
curStyle.setBorderTop(CellStyle.BORDER_THIN); // 細實線
curStyle.setBorderBottom(CellStyle.BORDER_THIN);
curStyle.setBorderLeft(CellStyle.BORDER_THIN);
curStyle.setBorderRight(CellStyle.BORDER_THIN);

return curStyle;
}

// 文本樣式
public static CellStyle textStyle(Workbook wb) {
CellStyle xStyle = wb.createCellStyle();
Font xFont = wb.createFont();
xStyle.setFont(xFont);
xStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 縱向居中
// 畫線
xStyle.setBorderTop(CellStyle.BORDER_THIN); // 細實線
xStyle.setBorderBottom(CellStyle.BORDER_THIN);
xStyle.setBorderLeft(CellStyle.BORDER_THIN);
xStyle.setBorderRight(CellStyle.BORDER_THIN);
return xStyle;
}
}

調用工具類:

List<ProductTypeVo> ls = mapper.queryProductType(productTypeVo); String fileName="設備類型設置表"; String[]title=new String[]{"編號","類型名稱","計量單位","類型描述","建立時間","備註"}; String [] field=new String []{"typeName","unitname","typeDesc","createTime","remark"}; ExcelUtil.doExport(response, ls, fileName, title, field);

相關文章
相關標籤/搜索