2.设置单元格样式
// 实例化样式对象
HSSFCellStyle cellStyle = workbook.createCellStyle();
// 两端对齐
cellStyle.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY);
// 垂直居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 填充图案---填充方式
cellStyle.setFillPattern(HSSFCellStyle.DIAMONDS);
// 设置前景色 (这个要写在背景色的前面)
cellStyle.setFillForegroundColor(HSSFColor.RED.index);
// 设置背景颜色
cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
// 设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_SLANTED_DASH_DOT);
// 边框颜色
cellStyle.setBottomBorderColor(HSSFColor.DARK_RED.index);
// 日期展示格式
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
//将样式应用于单元格
cell.setCellStyle(cellStyle);
//将样式应用到行
row.setRowStyle(cellStyle);
3.设置字体
// 实例化字体对象
HSSFFont fontStyle = workbook.createFont();
// 字体
fontStyle.setFontName("宋体");
// 高度
fontStyle.setFontHeightInPoints((short)12);
// 字体
font.setColor(HSSFColor.BLUE.index);
// 加粗
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 斜体
font.setItalic(true);
// 下划线
font.setUnderline(HSSFFont.U_SINGLE);
// 将字体应用于单元格样式中
cellStyle.setFont(font);
个人应用
下面给大家分享下个人的整体使用过程!

处理的action
//用于导出的数据集合
List<PBillBean> dataset = new ArrayList<PBillBean>();
//填充dataset
for (int i = 0; i < 10; i++) {
PBillBean bean = new PBillBean();
dataset.add(bean);
}
//临时文件
File tempFile = null;
try {
//Excel导出工具类
ExportExcel<PBillBean> ex = new ExportExcel<PBillBean>();
//导出的标题列
String[] headers = { "标题1", "标题2", "标题3", "标题4", "标题5", "标题6" };
//时间格式化
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
//要保存的文件名
String filename = "bill_" + format.format(new Date()) + ".xls";
//要保存的根目录
String rootDir = request.getSession().getServletContext().getRealPath("/");
//要保存的目录路径
String path = rootDir + File.separator + "tempfile";
File saveDir = new File(path);
if (!saveDir.exists()) {
saveDir.mkdirs();// 如果文件不存在则创建文件夹
}
//文件路径
path = path + File.separator + filename;
tempFile = new File(path); //初始化临时文件
//输出流
OutputStream out = new FileOutputStream(tempFile);
//实例化Excel表格
HSSFWorkbook workbook = new HSSFWorkbook();
//创建工作表单
String[] sheetNames = { "对账报表" };
for (int i = 0; i < sheetNames.length; i++) {
workbook.createSheet(sheetNames[i]);
}
//导出到Excel
ex.exportExcel(sheetNames[0], headers, dataset, out,
"yyyy-MM-dd HH:mm", workbook);
try {
//保存文件
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}
out.close();
// 以流的形式下载文件。
BufferedInputStream fis = new BufferedInputStream(
new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + tempFile.length());
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=utf-8");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (tempFile != null && tempFile.exists()) {
tempFile.delete();// 删除临时文件
}
}










