java_HSSFWorkbook导出Excel(已解决合并单元格显示部分边框问题)

来源:互联网 发布:java软件设计师培训 编辑:程序博客网 时间:2024/05/29 04:08
/**
* 导出Excel
* @param sstjid
* @return boolean
*/
public synchronized String exportExcel(String sstjid){

Zshgadj gadj = ZshgadjService.service.findById_edit(sstjid);
short rowHeight_x = 700;//小单元格行高

word_index++;
ExcelDic += (new Date().getYear()+1900)+"/";
String path = ExcelDic + "report_"+ word_index + ".xls";

String rootPath = PathKit.getWebRootPath().replace("\\", "/");
rootPath = rootPath.substring(0, rootPath.lastIndexOf("/"));

HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sheet1");//打开Excel表格在底端可以看到
        sheet.setColumnWidth(0, 7800);//设置列宽
        sheet.setColumnWidth(1, 9600);
        sheet.setColumnWidth(2, 7800);
        sheet.setColumnWidth(3, 9600);
        
        
        //设置样式
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 样式对象    
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平    
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框    
        HSSFFont font = workbook.createFont();//设置字体
        font.setFontName("Arial");    
        font.setFontHeightInPoints((short) 20);//设置字体大小  
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 
        cellStyle.setFont(font);
        
        //设置样式
        HSSFCellStyle cellStyle_T = workbook.createCellStyle(); // 样式对象    
        cellStyle_T.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle_T.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平    
        cellStyle_T.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
        cellStyle_T.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
        cellStyle_T.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
        cellStyle_T.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        cellStyle_T.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); //设置颜色
        HSSFFont font_t = workbook.createFont();//设置字体
        font_t.setFontName("Arial");    
        font_t.setFontHeightInPoints((short) 11);//设置字体大小  
        font_t.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗 
        cellStyle_T.setFont(font_t);
        
        //设置样式
        HSSFCellStyle cellStyle_C = workbook.createCellStyle(); // 样式对象    
        cellStyle_C.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直    
        cellStyle_C.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框    
        cellStyle_C.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框    
        cellStyle_C.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框    
        cellStyle_C.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        HSSFFont font_c = workbook.createFont();//设置字体
        font_c.setFontName("Arial");    
        font_c.setFontHeightInPoints((short) 11);//设置字体大小  
        cellStyle_C.setFont(font_c);
        cellStyle_C.setWrapText(true);// 自动换行 
        
        int indexRow = 0;
        // 创建第一行   
        HSSFRow row0 = sheet.createRow(indexRow);  
        row0.setHeight((short) 1100);// 设置行高      
        // 创建第一列   
        HSSFCell cell0 = row0.createCell(indexRow++);   
        cell0.setCellValue(new HSSFRichTextString("表"));   
        cell0.setCellStyle(cellStyle);      
        //合并表头单元格
        setRegionStyle(sheet, new Region(0,(short)0,0,(short)3),cellStyle);
        sheet.addMergedRegion(new Region(
        0 //first row (0-based) from 行  
        ,(short)0 //first column (0-based) from 列     
        ,0//last row  (0-based)  to 行
        ,(short)3//last column  (0-based)  to 列     
        ));
   
        
        // 创建第二行   
        HSSFRow row1 = sheet.createRow(indexRow++);   
        row1.setHeight(rowHeight_x);
        HSSFCell cell1_1 = row1.createCell(0);   
        cell1_1.setCellValue(new HSSFRichTextString("姓名"));   
        cell1_1.setCellStyle(cellStyle_T); 
        HSSFCell cell2_1 = row1.createCell(1);   
        cell2_1.setCellValue(new HSSFRichTextString(gadj.getStr(Zshgadj.column_xahzxm)));   
        cell2_1.setCellStyle(cellStyle_C);
        HSSFCell cell3_1 = row1.createCell(2);   
        cell3_1.setCellValue(new HSSFRichTextString("审核人"));   
        cell3_1.setCellStyle(cellStyle_T);
        HSSFCell cell4_1 = row1.createCell(3);   
        cell4_1.setCellValue(new HSSFRichTextString(gadj.getStr(Zshgadj.column_shr)));   
        cell4_1.setCellStyle(cellStyle_C);
        
        
        // 创建第三行  (合并单元格)
        HSSFRow row9 = sheet.createRow(indexRow++);
        row9.setHeight((short)1500);
        HSSFCell cell1_9 = row9.createCell(0);   
        cell1_9.setCellValue(new HSSFRichTextString("简要情况"));   
        cell1_9.setCellStyle(cellStyle_T); 
        HSSFCell cell2_9 = row9.createCell(1);   
        cell2_9.setCellValue(new HSSFRichTextString(gadj.getStr(Zshgadj.column_jyaq)));
        //合并表头单元格
        setRegionStyle(sheet, new Region(indexRow-1,(short)1,indexRow-1,(short)3),cellStyle_C);
        sheet.addMergedRegion(new Region(indexRow-1,(short)1,indexRow-1,(short)3));
        
        
        FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(rootPath + path);
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
    if(workbook != null){
    workbook.close();
    }
    if(outputStream != null){
    outputStream.close();
            }
} catch (IOException e) {
e.printStackTrace();
}
}
        
return path;

}



/**
* 设置单元格边框(解决合并单元格显示部分边框问题)
* @param sheet 
* @param region
* @param cs
*/
@SuppressWarnings("deprecation")
public static void setRegionStyle(HSSFSheet sheet, Region region, HSSFCellStyle cs) {
 for (int i = region.getRowFrom(); i <= region.getRowTo(); i++) {
  HSSFRow row = HSSFCellUtil.getRow(i, sheet);
  for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
   HSSFCell cell = HSSFCellUtil.getCell(row, (short) j);
   cell.setCellStyle(cs);
  }
 }
}

0 0
原创粉丝点击