package com.sk.utils;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;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;import org.apache.poi.hssf.util.HSSFColor;/** * 生成导出Excel文件对象 * * @author chico * */public class ExportExcel { private static HSSFFont fontStyle = null; private static HSSFCellStyle cellStyle = null; public static HSSFFont getTitleFont(HSSFWorkbook wb) { fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints((short) 12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体加粗 return fontStyle; } public static HSSFFont getContentFont(HSSFWorkbook wb) { fontStyle = wb.createFont();// fontStyle.setFontName("宋体");// fontStyle.setFontHeightInPoints((short) 12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; } public static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook){ cellStyle = workbook.createCellStyle(); // 在工作薄的基础上建立一个样式 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 顶边框 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底边框 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中// cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景颜色 cellStyle.setFont(getTitleFont(workbook)); return cellStyle; } public static HSSFCellStyle getContentStyle(HSSFWorkbook workbook){ cellStyle = workbook.createCellStyle(); // 在工作薄的基础上建立一个样式 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 顶边框 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底边框 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中// cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景颜色// cellStyle.setFont(getContentFont(workbook)); return cellStyle; } public static HSSFCellStyle getLeftContentStyle(HSSFWorkbook workbook){ cellStyle = workbook.createCellStyle(); // 在工作薄的基础上建立一个样式 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); // 左边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); // 右边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 顶边框 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 底边框 cellStyle.setVerticalAlignment(HSSFCellStyle.ALIGN_FILL);//垂直居中// cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中// cellStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); // 填充的背景颜色// cellStyle.setFont(getContentFont(workbook)); return cellStyle; } public static void createTitleRow(HSSFRow curRow,HSSFWorkbook workbook){ HSSFCell curCell = curRow.createCell(0); curCell.setCellStyle(getTitleStyle(workbook)); curCell.setCellValue("姓名"); HSSFCell curCell2 = curRow.createCell(1); curCell2.setCellStyle(getTitleStyle(workbook)); curCell2.setCellValue("项目"); } public static void createContentRow(HSSFCell curCell,HSSFWorkbook workbook,String value){ curCell.setCellStyle(ExportExcel.getContentStyle(workbook)); curCell.setCellValue(value); } public static void createLeftContentRow(HSSFCell curCell,HSSFWorkbook workbook,String value){ curCell.setCellStyle(ExportExcel.getLeftContentStyle(workbook)); curCell.setCellValue(value); } public static void main(String[] args) { HSSFWorkbook workbook = new HSSFWorkbook(); // 建立一个工作薄 HSSFSheet sheet = workbook.createSheet(); sheet.setDefaultRowHeightInPoints(15); sheet.setDefaultColumnWidth(50); HSSFRow curRow = sheet.createRow(0); HSSFCell curCell = curRow.createCell(0); curCell.setCellStyle(getTitleStyle(workbook)); curCell.setCellValue("姓名"); HSSFCell curCell2 = curRow.createCell(1); curCell2.setCellStyle(getTitleStyle(workbook)); curCell2.setCellValue("项目"); FileOutputStream fos; try { fos = new FileOutputStream("D:\\test.xls"); workbook.write(fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 填充图案 // 假设什么定义了一个样式,想在填充第一个单元格的时候填充红,第二格单元格填充蓝色。 // 如果: // HSSFCellStyle cellStyle = workbook.createCellStyle(); // 创建一个样式 // cellStyle.setFillForegroundColor(HSSFColor.RED.index); // 设置颜色为红色 // cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // HSSFCell cell1 = row.createCell((short) 1); // 给单元格cell1填充红色 // cell1.setCellStyle(cellStyle); // 若: // cellStyle.setFillForegroundColor(HSSFColor.BLUE.index); // 设置颜色为蓝色 // HSSFCell cell2 = row.createCell((short) 2); // 给单元格cell2填充蓝色 // cell2.setCellStyle(cellStyle); }}