POI導出工具類
阿新 • • 發佈:2018-04-16
JAVA POI import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExportUtils {
/**
* 導出
* @param data 數據
* @param columnTitleList 標題名稱
* @param columnCodeList 列參數
* @param fileName 文件名稱
* @param response
*/
public static void exportData(List<Map<String, Object>> data, String[] columnTitleList, String[] columnCodeList,
String fileName, HttpServletResponse response) {
HSSFWorkbook workbook = new HSSFWorkbook(); //定義一個新的工作簿
HSSFSheet sheet = workbook.createSheet(); // 創建第一個Sheet頁
HSSFRow row = sheet.createRow(0);
// 創建標題樣式
HSSFCellStyle headerStyle = (HSSFCellStyle) workbook .createCellStyle();// 創建標題樣式
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //設置垂直居中
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設置水平居中
HSSFFont headerFont = (HSSFFont) workbook.createFont(); //創建字體樣式
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗
headerFont.setFontName("Times New Roman"); //設置字體類型
headerFont.setFontHeightInPoints((short) 14); //設置字體大小
headerStyle.setFont(headerFont); //為標題樣式設置字體樣式
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框
//正文樣式
HSSFCellStyle cell_Style = (HSSFCellStyle) workbook .createCellStyle();// 設置字體樣式
cell_Style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell_Style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直對齊居中
cell_Style.setWrapText(true); // 設置為自動換行
HSSFFont cell_Font = (HSSFFont) workbook.createFont();
cell_Font.setFontName("宋體");
cell_Font.setFontHeightInPoints((short) 12);
cell_Style.setFont(cell_Font);
cell_Style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下邊框
cell_Style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左邊框
cell_Style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上邊框
cell_Style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右邊框
// 設置標題
for (int i = 0; i < columnTitleList.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
cell.setCellValue(columnTitleList[i]);
sheet.setColumnWidth(i, 256 * 20);
row.setHeight((short) (256*2));
}
int n = 0;
try {
for (Map<String, Object> map : data) {
row = sheet.createRow(++n);
for (int i = 0; i < columnCodeList.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(cell_Style);
if (map.get(columnCodeList[i]) != null) {
cell.setCellValue(map.get(columnCodeList[i]).toString());
} else {
cell.setCellValue("");
}
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
OutputStream os = null;
try {
response.reset();
response.setHeader("Content-Type", "application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
os = response.getOutputStream();
workbook.write(os);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
POI導出工具類