您的位置:首页 > 其它

POI导出EXCEL文档、水平垂直居中、表头添加颜色

2017-12-12 15:07 405 查看
如下工具类添加表头颜色、水平垂直居中、设置列宽和行高:
package utils;import ggframework.bottom.log.GGLogger;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;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.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.util.CellRangeAddress;public class ExcelUtil {public static File getSimpolDataExcel(List<Map<String, Object>> data, String name) {//创建Excel工作簿对象,对应一个Excel文件HSSFWorkbook book = new HSSFWorkbook();//创建Excel工作表对象HSSFSheet sheet = book.createSheet(name);sheet.setVerticallyCenter(true);int index = 0;//创建单元格,并设置值表头 设置表头居中HSSFCellStyle styleMain = book.createCellStyle();//水平居中styleMain.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中styleMain.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);for (Map<String, Object> map : data) {int cellNum = 0;//创建Excel工作表行HSSFRow row = sheet.createRow(index);//设置行高 单位像素row.setHeight((short)400);//创建单元格,并设置值表头 设置表头居中HSSFCellStyle styleTitle = book.createCellStyle();//水平居中styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//垂直居中styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置背景色styleTitle.setFillForegroundColor(IndexedColors.TEAL.getIndex());styleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);styleTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN);styleTitle.setBorderLeft(HSSFCellStyle.BORDER_THIN);styleTitle.setBorderRight(HSSFCellStyle.BORDER_THIN);styleTitle.setBorderTop(HSSFCellStyle.BORDER_THIN);if(index==0){int i=0;for(Map.Entry<String, Object> entry: map.entrySet()) {//设置列的宽度 单位像素sheet.setColumnWidth(i++, 6000);//创建Excel单元格HSSFCell cell = row.createCell(cellNum);//设置Excel单元格值cell.setCellValue(entry.getKey()==null?"--":entry.getKey().toString());cell.setCellStyle(styleTitle);cellNum++;}}else{for(Map.Entry<String, Object> entry: map.entrySet()) {HSSFCell cell = row.createCell(cellNum);cell.setCellValue(entry.getValue()==null?"--":entry.getValue().toString());cell.setCellStyle(styleMain);4000cellNum++;}}index++;}//设置生成的文件临时存放目录File dir = new File("temp/userfb");if (!dir.exists()) {dir.mkdirs();}String fileName =("temp/userfb") + File.separator + name + ".xls";File file = new File(fileName);try {FileOutputStream fos = new FileOutputStream(file);book.write(fos);fos.close();} catch (IOException e) {e.printStackTrace();}return file;}}
前端HTML页面代码:
<a href="javascript:void(0);" style="display: none;" id="a_exportCode" data-toggle="popover" data-placement="top" ></a>
<a href="javascript:void(0);" style="display: none;" id="a_exportCode" data-toggle="popover" data-placement="top" ></a>
		//导出统计数据function exportCode() {$("#a_exportCode").attr('href','/apiStore/ApiLogManagerAction/exportData?appKey='+$("#app_key").val()+'&start_date='+$("#startDate").val())+"&endDate="+$("#end_date").val()+"&name="+$("#name").val();$("#a_exportCode").attr('download','code'+new Date().getTime()+".pdf");$("#a_exportCode")[0].click();}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: