POI导出EXCEL

package com.tkqd.util.poi;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.HSSFCellUtil;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
 
 
 
 
 
public class ExcelUtil {
 
    private static final Logger log=Logger.getLogger(ExcelUtil.class);
   
 
    public static void writeWorkbook(HSSFWorkbook wb,String fileName){
 
        FileOutputStream fos=null;
 
        try {
            fos=new FileOutputStream(fileName);
            wb.write(fos);
        } catch (FileNotFoundException e) {
            log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
        } catch (IOException e) {
            log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
        } finally{
            try {
                if(fos!=null){
                    fos.close();
                }
            } catch (IOException e) {
                log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause()));
            }
        }
 
    }
 
   
 
    public static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){
        HSSFSheet sheet=wb.createSheet(sheetName);
        sheet.setDefaultColumnWidth(12);
        sheet.setGridsPrinted(false);
        sheet.setDisplayGridlines(false);
        return sheet;
 
    }
 
   
 
    public static HSSFRow createRow(HSSFSheet sheet,int rowNum,int height){
 
        HSSFRow row=sheet.createRow(rowNum);
        row.setHeight((short)height);
        return row;
    }
 
   
 
    public static CellStyle createCellStyle(HSSFWorkbook wb,shortbackgroundColor,short foregroundColor,short halign,Font font){
 
        CellStyle cs=wb.createCellStyle();
        cs.setAlignment(halign);
        cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        cs.setFillBackgroundColor(backgroundColor);
        cs.setFillForegroundColor(foregroundColor);
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFont(font);
cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
cs.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
cs.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 下边框
        return cs;
    }
 
   
 
    public static CellStyle createBorderCellStyle(HSSFWorkbook wb,shortbackgroundColor,short foregroundColor,short halign,Font font){
        CellStyle cs=wb.createCellStyle();
        cs.setAlignment(halign);
        cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        cs.setFillBackgroundColor(backgroundColor);
        cs.setFillForegroundColor(foregroundColor);
        cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cs.setFont(font);
        cs.setBorderLeft(CellStyle.BORDER_DASHED);
        cs.setBorderRight(CellStyle.BORDER_DASHED);
        cs.setBorderTop(CellStyle.BORDER_DASHED);
        cs.setBorderBottom(CellStyle.BORDER_DASHED); 
 
        return cs;
    }
 
   
 
    public static HSSFCell createCell(HSSFRow row,int cellNum,CellStyle style){
        HSSFCell cell=row.createCell(cellNum);
        cell.setCellStyle(style);
        return cell;
 
    }
 
   
 
    public static int mergeCell(HSSFSheet sheet,int firstRow,int lastRow,intfirstColumn,int lastColumn){
 
        return sheet.addMergedRegion(newCellRangeAddress(firstRow,lastRow,firstColumn,lastColumn));   
 
    }
 
   
 
    public static Font createFont(HSSFWorkbook wb,short boldweight,shortcolor,short size){
 
        Font font=wb.createFont();
        font.setBoldweight(boldweight);
        font.setColor(color);
        font.setFontHeightInPoints(size);
        return font;
 
    }
 
    public static String getCellStringValue(HSSFCell cell){
String strCell = "";      
if(cell == null){
return "";
}
try {
switch (cell.getCellType()) {  
case HSSFCell.CELL_TYPE_STRING:           
strCell = cell.getStringCellValue();           
break;   
case HSSFCell.CELL_TYPE_NUMERIC:            
strCell = String.valueOf(cell.getNumericCellValue());          
break; 
case HSSFCell.CELL_TYPE_BOOLEAN:           
strCell = String.valueOf(cell.getBooleanCellValue());           
break;    
case HSSFCell.CELL_TYPE_BLANK:         
strCell = "";           
break;     
default:            
strCell = "";          
break;        }      
if (strCell.equals("") || strCell == null) {           
return "";        }      
if (cell == null) {           
return "";        }
} catch (Exception e) {
e.printStackTrace();
return "";
}     
return strCell.trim();
}
 
    public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) { 
        for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) { 
            HSSFRow row = HSSFCellUtil.getRow(i, sheet); 
            for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) { 
                HSSFCell cell = HSSFCellUtil.getCell(row, j); 
                cell.setCellStyle(style); 
            
        
    
}
原文地址:https://www.cnblogs.com/wangdonghua/p/3320798.html