Workbook wb = new HSSFWorkbook(); 粗体 样式设置
package com.rmsClient.util.retireInforExcelOuput; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; 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.Cell; 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.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import com.rmsClient.entity.po.PageResult; import com.rmsClient.entity.po.RetireInforItem; import com.rmsClient.util.LogUtil; /** * 从数据库中读取工资的字段,然后动态生成excel模板 * * @author qiulinhe * * 2017年2月20日 下午5:41:35 */ public class ExcelOutputUtil implements ExcelConstraint { public static void main(String[] args) { String[] headStrings = { "姓名", "身份证号" }; // retireInforExcelModel("导出信息excel模板", "D://ceshi.xls", headStrings); FileOutputStream out = null; try { Workbook wb = new HSSFWorkbook(); // 输出excel对象 out = new FileOutputStream("D://ceshi.xls"); Sheet sheet = wb.createSheet("测试背景颜色"); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(20); // 产生表格标题行 Row row = sheet.createRow(0); for (int i = 0; i < headStrings.length; i++) { CellStyle style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Cell cell = row.createCell((short) i); cell.setCellValue(headStrings[i]); cell.setCellStyle(style); } // 输出excel wb.write(out); out.close(); System.out.println("在D盘成功生成了excel,请去查看"); LogUtil.info("在D盘成功生成了excel,请去查看"); } catch (FileNotFoundException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } catch (IOException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } } } } /** * 信息导入里面的:导出信息excel模板 * * @param excelTitle * excel文件标题 * @param filePath * 导出文件路径 * @param pageResult * excel标题 */ public static void retireInforExcelModel(String excelTitle, String filePath, PageResult<RetireInforItem> pageResult) { // 获取信息项,也就是excel的标题 List<RetireInforItem> retireItems = pageResult.getResultList(); FileOutputStream out = null; try { // excel对象 HSSFWorkbook wb = new HSSFWorkbook(); // sheet对象 // HSSFSheet sheet = wb.createSheet(excelTitle); // 输出excel对象 out = new FileOutputStream(filePath); // 从数据库中读取字段,设置输入的规则,设置某一列的输入规则 Sheet sheet = RetireinforConstraint.settingAllColumnSheet(excelTitle, wb, retireItems); sheet.setDefaultColumnWidth(20); sheet.setDefaultRowHeightInPoints(40); // 产生表格标题行 Row row = sheet.createRow(0); for (int i = 0; i < retireItems.size(); i++) { CellStyle style = wb.createCellStyle(); // 给单元格设置背景颜色 style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 创建边框 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框 style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框 style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框 // 设置居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 // 设置字体和文字大小 Font font2 = wb.createFont(); font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 font2.setFontHeightInPoints((short) 12); // font2.setColor(HSSFColor.RED.index);// 字体颜色:红色 font2.setColor(HSSFColor.BLACK.index);// 字体颜色:黑色 style.setFont(font2);// 选择需要用到的字体格式 Cell cell = row.createCell((short) i); cell.setCellValue(retireItems.get(i).getName()); cell.setCellStyle(style); } // 输出excel wb.write(out); out.close(); System.out.println("在D盘成功生成了excel,请去查看"); LogUtil.info("在D盘成功生成了excel,请去查看"); } catch (FileNotFoundException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } catch (IOException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { LogUtil.error("生成信息模板出错" + e); e.printStackTrace(); } } } } }
最新评论 0