很簡單 我封裝好的poi導出類 能夠拿來直接用java
package com.xst.common;正則表達式
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
public class newExcel <T,J>{
public static final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
public void exportExcel(String title, String[] headers,Collection<?> dataList, Object sumData,OutputStream out, String pattern,int[] width) throws Exception{
exportSumMoneyExcel(title, headers, dataList,sumData,out,pattern,width);
}
@SuppressWarnings("unchecked")
private void exportSumMoneyExcel(String title, String[] headers,Collection<?> dataList, Object sumData,OutputStream out, String pattern,int[] width) throws Exception{
try {
//聲明一個工做薄
SXSSFWorkbook workbook=new SXSSFWorkbook(1000);
//生成表格
SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(title);
for(int i = 0;i < width.length;i++){
sheet.setColumnWidth(i,width[i]*256+184);
}
// 1.標題的樣式
CellStyle style = workbook.createCellStyle();
//標題背景色爲灰色
//style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
//前置背景色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//下邊框樣式
style.setBorderBottom(CellStyle.BORDER_THIN);
//左邊框樣式
style.setBorderLeft(CellStyle.BORDER_THIN);
//右邊框樣式
style.setBorderRight(CellStyle.BORDER_THIN);
//上邊框樣式
style.setBorderTop(CellStyle.BORDER_THIN);
//設置單元格內容水平居中
style.setAlignment(CellStyle.ALIGN_CENTER);
// 標題的字體樣式
Font font = workbook.createFont();//生成字體
//設置字體顏色
font.setColor(IndexedColors.BLACK.index); //設置字體顏色爲黑色
//設置字體大小
font.setFontHeightInPoints((short) 12);
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體加粗
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
// 把字體應用到當前的樣式
style.setFont(font);
// 內容樣式
CellStyle style2 = workbook.createCellStyle();
//內容背景色爲黃色
style2.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.index);
style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(CellStyle.BORDER_THIN);
style2.setBorderLeft(CellStyle.BORDER_THIN);
style2.setBorderRight(CellStyle.BORDER_THIN);
style2.setBorderTop(CellStyle.BORDER_THIN);
style2.setAlignment(CellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 內容字體樣式
Font font2 = workbook.createFont();
font2.setColor(IndexedColors.BLACK.index); //設置數據字體爲黑色
font2.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// 把字體應用到當前的樣式
style2.setFont(font2);
// 合計的樣式
CellStyle style3 = workbook.createCellStyle();
//內容背景色爲白色
style3.setFillForegroundColor(IndexedColors.WHITE.index);
style3.setFillPattern(CellStyle.SOLID_FOREGROUND);
style3.setBorderBottom(CellStyle.BORDER_THIN);
style3.setBorderLeft(CellStyle.BORDER_THIN);
style3.setBorderRight(CellStyle.BORDER_THIN);
style3.setBorderTop(CellStyle.BORDER_THIN);
style3.setAlignment(CellStyle.ALIGN_CENTER);
style3.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 內容字體樣式
Font font3 = workbook.createFont();
font3.setColor(IndexedColors.BLUE.index); //設置數據字體爲綠色
font3.setBoldweight(Font.BOLDWEIGHT_NORMAL);
// 把字體應用到當前的樣式
style3.setFont(font3);
// 產生表格標題行
SXSSFRow row = (SXSSFRow) sheet.createRow(0);
//設置行高
row.setHeight((short)20);
//行高的像素數
row.setHeightInPoints(20);
for (short i = 0; i < headers.length; i++) {
SXSSFCell cell=(SXSSFCell) row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(headers[i]);
}
// 遍歷集合數據,產生數據行
Iterator<?> it = dataList.iterator();
int index = 0;
while (it.hasNext()) {
index++;
row = (SXSSFRow) sheet.createRow(index);
T t = (T) it.next();
// 利用反射,根據javabean屬性的前後順序,動態調用getXxx()方法獲得屬性值
Field[] fields = t.getClass().getDeclaredFields();
for (short i = 0; i < fields.length; i++) {
SXSSFCell cell=(SXSSFCell) row.createCell(i);
cell.setCellStyle(style2);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try {
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
Object value = getMethod.invoke(t, new Object[] {});
if(value==null){
value=" ";
}
String textValue = null;
if (value instanceof Boolean) {
boolean bValue = (Boolean) value;
textValue = "男";
if (!bValue) {
textValue = "女";
}
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
}else if (value instanceof byte[]) {
/*// 有圖片時,設置行高爲60px;
row.setHeightInPoints(60);
// 設置圖片所在列寬度爲80px,注意這裏單位的一個換算
sheet.setColumnWidth(i, (short) (35.7 * 80));
// sheet.autoSizeColumn(i);
byte[] bsValue = (byte[]) value;
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
1023, 255, (short) 6, index, (short) 6, index);
// anchor.setAnchorType(2);
patriarch.createPicture(anchor, workbook.addPicture(
bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));*/
} else {
// 其它數據類型都看成字符串簡單處理
textValue = value.toString();
}
// 若是不是圖片數據,就利用正則表達式判斷textValue是否所有由數字組成
if (textValue != null) {
// Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Pattern p = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches() && textValue.length()<12) {
// 是數字看成double處理
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
}catch (Exception e) {
e.printStackTrace();
}finally {
// 清理資源
}
}//for
}//while
if (sumData!=null) {
SXSSFSheet sheet2=(SXSSFSheet) workbook.getSheetAt(0); //獲取到工做表,由於一個excel可能有多個工做表
SXSSFRow row2=(SXSSFRow)sheet2.getRow(0); //獲取第一行(excel中的行默認從0開始,因此這就是爲何,一個excel必須有字段列頭),即,字段列頭,便於賦值
row2=(SXSSFRow) sheet2.createRow((short)(sheet2.getLastRowNum()+1));
SXSSFCell cell0=(SXSSFCell)row2.createCell(0);
cell0.setCellStyle(style3);
cell0.setCellValue("合計:");
Field[] fields = sumData.getClass().getDeclaredFields();
try {
for (short i = 0; i < fields.length; i++) {
SXSSFCell cell = (SXSSFCell)row2.createCell(i+1);
cell.setCellStyle(style3);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
Class tCls = sumData.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[] {});
Object value = getMethod.invoke(sumData, new Object[] {});
if(value==null){
value=" ";
}
String textValue = null;
if (value instanceof Boolean) {
boolean bValue = (Boolean) value;
textValue = "男";
if (!bValue) {
textValue = "女";
}
} else if (value instanceof Date) {
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
}else if (value instanceof byte[]) {
/*// 有圖片時,設置行高爲60px;
row.setHeightInPoints(60);
// 設置圖片所在列寬度爲80px,注意這裏單位的一個換算
sheet.setColumnWidth(i, (short) (35.7 * 80));
// sheet.autoSizeColumn(i);
byte[] bsValue = (byte[]) value;
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
1023, 255, (short) 6, index, (short) 6, index);
// anchor.setAnchorType(2);
patriarch.createPicture(anchor, workbook.addPicture(
bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));*/
} else {
// 其它數據類型都看成字符串簡單處理
textValue = value.toString();
}
// 若是不是圖片數據,就利用正則表達式判斷textValue是否所有由數字組成
if (textValue != null) {
// Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Pattern p = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches() && textValue.length()<12) {
// 是數字看成double處理
cell.setCellValue(Double.parseDouble(textValue));
} else {
cell.setCellValue(textValue);
}
}
}//for
} catch (Exception e) {
e.printStackTrace();
} finally {
// 清理資源
}
}//if
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}//方法
}//類apache
大家複製好直接用:接下來的代碼就是大家每個controller裏面的代碼了app
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formatDate = sdf.format(date);
String filename= new String((formatDate.replace("-", "") +" 銷售物料管理.xls").getBytes(),"iso-8859-1");
response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+filename);
response.setContentType("application/msexcel");
String[] headers = { "原料名稱","價格(元)"};
int [] width={28,15};
String title = "銷售物料管理";
newExcel<Settb109ExtA,Settb109ExtA> excel = new newExcel<Settb109ExtA,Settb109ExtA>();
OutputStream out = response.getOutputStream();
excel.exportExcel(title, headers,settb109selectList, null, out, title, width);
out.close();xss
中間的樣式你們能夠上官網上面進行查詢和學習 今天我只分享了封裝好的poi學習