POI導出EXCEL文檔,表格水平垂直居中、表頭添加顏色等常用屬性方法


package utils;
 
import ggframework.bottom.log.GGLogger;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
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.HSSFRichTextString;
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;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
 
 
public class ExcelUtil {
 
    public static File getSimpolDataExcel(List<Map<String, Object>> data, String name) {
        //創建Excel工作簿對象,對應一個Excel文件
        HSSFWorkbook book = new HSSFWorkbook();
        //創建Excel工作表對象
        HSSFSheet sheet = book.createSheet(name);
        sheet.setVerticallyCenter(true);
        int index = 0;
        
      //創建單元格,並設置值表頭 設置表頭居中
        HSSFCellStyle styleMain = book.createCellStyle();
        //水平居中
        styleMain.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //垂直居中
        styleMain.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        
        for (Map<String, Object> map : data) {
            int cellNum = 0;
            //創建Excel工作表行
            HSSFRow row = sheet.createRow(index);
            //設置行高 單位像素
            row.setHeight((short)400);
            //創建單元格,並設置值表頭 設置表頭居中
            HSSFCellStyle styleTitle = book.createCellStyle();
            //水平居中
            styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            //垂直居中
            styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            //設置背景色
            styleTitle.setFillForegroundColor(IndexedColors.TEAL.getIndex());
            styleTitle.setFillPattern(CellStyle.SOLID_FOREGROUND);
            
            styleTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            styleTitle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            styleTitle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            styleTitle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            if(index==0){
                int i=0;
                for(Map.Entry<String, Object> entry: map.entrySet()) {
                 //設置列的寬度 單位像素
                 sheet.setColumnWidth(i++, 6000);
                 //創建Excel單元格
                    HSSFCell cell = row.createCell(cellNum);
                    //設置Excel單元格值
                    cell.setCellValue(entry.getKey()==null?"--":entry.getKey().toString());
                    cell.setCellStyle(styleTitle);
                    cellNum++;
               }    
            }else{
                
                for(Map.Entry<String, Object> entry: map.entrySet()) {
                     HSSFCell cell = row.createCell(cellNum);
                     cell.setCellValue(entry.getValue()==null?"--":entry.getValue().toString());
                     cell.setCellStyle(styleMain);
                     cellNum++;
                }
               }
            index++;
        }
        //設置生成的文件臨時存放目錄
        File dir = new File("temp/userfb");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String fileName =("temp/userfb") + File.separator + name + ".xls"; 
        File file = new File(fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            book.write(fos);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
       return file;
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM