Apache POI HSSFWorkbook 操作excel

public String exportFunctionAct(HttpServletRequest request, HttpServletResponse response) throws Exception{
//设置导出文件弹出框的格式
String fileName=URLEncoder.encode("交易明细表.xls", "UTF-8");
fileName = fileName.replaceAll("\\+", "%20");
        response.setHeader("Cache-control", "private");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Accept-Ranges", "bytes");
        response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
//以下代码操作excel表
HSSFWorkbook workbook = new HSSFWorkbook();//建立一个工作簿
HSSFSheet sheet = workbook.createSheet();//创建一个工作表,名称为默认
HSSFRow row0 = sheet.createRow((short)0);//在索引0的位置创建行
HSSFRow row1 = sheet.createRow((short)1);//在索引1的位置创建行

//合并单元格
sheet.addMergedRegion(new Region(0, (short)0, 1, (short)0));
sheet.addMergedRegion(new Region(0, (short)1, 0, (short)3));
sheet.addMergedRegion(new Region(0, (short)4, 0, (short)6));
sheet.addMergedRegion(new Region(0, (short)7, 0, (short)9));
sheet.setColumnWidth(0, 3800);//设置列宽

//设置字体
HSSFFont headFont = workbook.createFont();
headFont.setFontName("黑体");
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();//在工作簿的基础上建立样式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// cellStyle.setFillBackgroundColor(HSSFColor.YELLOW.index2);//设置填充背景色
// cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index2);//设置填充前景色
// cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//取前景色
cellStyle.setFont(headFont);

HSSFCellStyle cellStyle1 = workbook.createCellStyle();//在工作簿的基础上建立样式
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// cellStyle1.setFillForegroundColor(HSSFColor.YELLOW.index2);//设置填充前景色
// cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//取前景色
ops = response.getOutputStream();
workbook.write(ops);
}

你可能感兴趣的:(poi,Excel,HSSFWorkbook)