package com.poi.excle;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
public class ExcelStyleUtil {
private static ExcelStyleUtil getExcelUtil = null;
public static ExcelStyleUtil get(){
if(null == getExcelUtil){
getExcelUtil = new ExcelStyleUtil();
}
return getExcelUtil;
}
/**
* @Description 設置第幾行內容
* @param cell 行對象
* @param row 列對象
* @param sheet
* @param content 內容
* @param Height 高度
* @return
*/
public ExcelStyleUtil rowContent(HSSFCell cell,HSSFRow row,HSSFSheet sheet,String content,int Height){
cell.setCellValue(content);
row.setHeightInPoints(Height);
return get();
}
/**
* @Description 設置單元格寬度5000
* @param column 多少列
* @param sheet
* @return
*/
public HSSFSheet setWidth(int column ,HSSFSheet sheet){
for(int i=0;i<=column;i++){
sheet.setColumnWidth(i, 5000);
}
return sheet;
}
/**
* @Description 設置合併單元格
* @param wbSheet
* @param firstRow :合併的開始行
* @param lastRow:合併的結束行
* @param firstCol: 合併的開始列
* @param lastColL: 合併的結束列
* @return
*/
public HSSFSheet setMergeCell(HSSFSheet wbSheet,int firstRow, int lastRow, int firstCol, int lastCol){
CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
get().setCellBorder(cellRangeAddress, wbSheet);
wbSheet.addMergedRegion(cellRangeAddress);
return null;
}
/**
* @Description 設置邊框線
* @param cellRangeAddress
* @param sheet
*/
public void setCellBorder(CellRangeAddress cellRangeAddress ,HSSFSheet sheet){
RegionUtil.setBorderLeft(1, cellRangeAddress, sheet);
RegionUtil.setBorderBottom(1, cellRangeAddress, sheet);
RegionUtil.setBorderRight(1, cellRangeAddress, sheet);
RegionUtil.setBorderTop(1, cellRangeAddress, sheet);
}
/**
* @Description //標題樣式 :加粗,垂直居中
* @param isBold 是否加粗
* @param FontISize 字體大小
*/
public HSSFCellStyle setTitleStyle(HSSFWorkbook wb, boolean isBold, int FontISize) {
// 標題樣式(加粗,垂直居中)
HSSFCellStyle cellStyle = wb.createCellStyle();
center(cellStyle);
Font font = wb.createFont();
font.setBold(isBold); //加粗
font.setFontHeightInPoints((short) FontISize); //設置標題字體大小
cellStyle.setFont(font);
return cellStyle;
}
/**
* @Description 樣式居中 加邊框
* @param cellStyle
*
*/
public HSSFCellStyle center(HSSFCellStyle cellStyle) {
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
// setBackdropColor(cellStyle);
return cellStyle;
}
/**
*
* @Description 邊框
* @param cellStyle
* @return
*/
public ExcelStyleUtil setFrame(HSSFCellStyle cellStyle){
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
return get();
}
/**
*
* @Description 背景色灰色
* @param cellStyle
* @return
*/
public ExcelStyleUtil setBackdropColor(HSSFCellStyle cellStyle){
cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);//水平居中
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);//垂直居中
cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);//垂直居中
return get();
}
public static void main(String[] args) throws FileNotFoundException, IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("table"); //創建table工作薄
ExcelStyleUtil.get().setWidth(9, sheet);
//標題
HSSFRow row;
row = sheet.createRow(0);//創建表格行
HSSFCell cell;
cell = row.createCell(0);//根據表格行創建單元格
ExcelStyleUtil.get().rowContent(cell,row,sheet, "貴省危貨運輸車輛在甘肅境內運行報警統計反饋表", 35).setMergeCell(sheet,0,0,0,9);
HSSFCellStyle titleStyle = ExcelStyleUtil.get().setTitleStyle(wb, true, 16);
ExcelStyleUtil.get().setFrame(titleStyle);
cell.setCellStyle(titleStyle);
row = sheet.createRow(1);//創建表格行
cell = row.createCell(0);//根據表格行創建單元格
ExcelStyleUtil.get()
.rowContent(cell,row,sheet, "填報單位:甘肅省交通運輸廳 填報時間:X年X月", 18)
.setMergeCell(sheet,1,1,0,9);
row.setHeightInPoints(18);
HSSFCellStyle stylew = wb.createCellStyle();
stylew = ExcelStyleUtil.get().center(stylew);
cell.setCellStyle(stylew);
Object[] datas = {"序號","車牌號碼","駕駛員","道路運輸證號","從業資格證號","所屬運輸企業名稱","裝載貨物","報警時間","報警地點","報警原因"};
row = sheet.createRow(2);//創建表格行
for(int i = 0; i < datas.length; i++){
cell = row.createCell(i);//根據表格行創建單元格
// row.setHeightInPoints(18);
// cell.setCellValue(String.valueOf(datas[i]));
ExcelStyleUtil.get().rowContent(cell,row,sheet, String.valueOf(datas[i]), 18);
HSSFCellStyle style = wb.createCellStyle();
ExcelStyleUtil.get().setFrame(style).setBackdropColor(style).center(style);
style.setWrapText(true);
cell.setCellStyle(style);
}
// //內容
Object[][] datas3 = {{"序號測試","車牌號碼測試","駕駛員測試","道路運輸證號測試","從業資格證號測試","所屬運輸企業名稱測試","裝載貨物測試",
"報警時間測試","報警地點測試","報警原因測試"},{"序號測試","車牌號碼測試","駕駛員測試","道路運輸證號測試","從業資格證號測試",
"所屬運輸企業名稱測試","裝載貨物測試","報警時間測試","報警地點測試","報警原因測試"}};
for(int i = 0; i < datas3.length; i++) {
row = sheet.createRow(3+i);//創建表格行
for(int j = 0; j < datas3[i].length; j++) {
cell = row.createCell(j);//根據表格行創建單元格
cell.setCellValue(String.valueOf(datas3[i][j]));
HSSFCellStyle style = wb.createCellStyle();
ExcelStyleUtil.get().setFrame(style);
style.setWrapText(true);
cell.setCellStyle(style);
}
}
wb.write(new FileOutputStream("E:/demo.xls"));
System.out.println("導出完成,已保存到E:/demo.xls");
}
}