package com.oemp.club.util;
import java.awt.Color;
import java.awt.List;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.oemp.util.UtilTools;
public class ExportExcel {
/**
* PIO導出
* title 以,分隔
*/
public static String DownLoadWeeklyReportExcel(String[] title,String path) throws Exception{
String title_list = "活動名稱,公司名稱,省/市,聯繫人,職位,手機號,EMAIL,錯誤原因";
title=title_list.split(",");
XSSFWorkbook wb = null;
try {
// 第一步,創建一個webbook,對應一個Excel文件
wb = new XSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
Sheet sheet = wb.createSheet("固定週報模板");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
sheet.setDefaultRowHeight((short)(290));
for(int i=0;i<50;i++){
sheet.setColumnWidth(i,10*300); //設置寬度
}
//總標題
Row row1 =sheet.createRow(0);
row1.setHeightInPoints(30);
Cell cell = row1.createCell(0);
cell.setCellStyle(reportStyle(wb));
cell.setCellValue("媽媽俱樂部微信數據週報"+UtilTools.formatDate(new Date(),"yyyy-MM-dd"));
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
//報表1
int tb=1;
createTableNote(wb,sheet,tb,"1. SUMMARY (註冊&粉絲 BY WEEK)","時間:默認最近13自然周");
//表格頭
String title1="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet,tb,title1);
tb=tb+2;
createTableContent(wb,sheet,tb);
/*
for(int i=0;i<list.size();i++){
SqlBean detail = list.get(i);
row = sheet.createRow(i + 1);
for(int k=0;k<cloumn.length;k++){
String labelText = "";
if(detail.containsKey(cloumn[k].trim())){
if(detail.get(cloumn[k].trim())!=null){
labelText = detail.get(cloumn[k].trim()).toString();
}
}
row.createCell(k).setCellValue(labelText);
}
}*/
// 報表2
int tb1=tb+12;
createTableNote(wb,sheet,tb1,"2. Top5 Follow source","時間:默認最近1自然周");
String title2="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet,tb1,title2);
tb1=tb1+2;
createTableContent(wb,sheet,tb1);
// 報表3
int tb2=tb1+12;
createTableNote(wb,sheet,tb2,"3. Top3 UnFollow source","時間:默認最近1自然周");
String title3="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet,tb2,title3);
tb2=tb2+2;
createTableContent(wb,sheet,tb2);
// 報表4
int tb3=tb2+12;
createTableNote(wb,sheet,tb3,"5. MEMBER STAGE","時間:截至最近1週週日爲止");
String title4="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet,tb3,title4);
tb3=tb3+2;
createTableContent(wb,sheet,tb3);
// 報表5
int tb4=tb3+12;
createTableNote(wb,sheet,tb4,"6. Menu Click","時間:默認最近13自然周 ");
String title5="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet,tb4,title5);
tb4=tb4+2;
createTableContent(wb,sheet,tb4);
//第二個sheet
sheet2(wb);
}catch(Exception ex){
ex.printStackTrace();
}finally{
if (wb != null) {
FileOutputStream fout = new FileOutputStream(path);
try {
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return path;
}
/**
* create table content
*/
public static void createTableContent(XSSFWorkbook wb,Sheet sheet,int tb){
for(int i=0;i<10;i++){
Row row = sheet.createRow(i + tb + 1 );
for(int k=0;k<27;k++){
Cell cell= row.createCell(k);
cell.setCellValue(i+"&"+k);
if(i==9){
cell.setCellStyle(cellSummayStyle(wb));
}else{
cell.setCellStyle(cellStyle(wb));
}
}
}
}
/**
* create table Note
*/
public static void createTableNote(XSSFWorkbook wb,Sheet sheet,int tb,String cell1,String cell2){
//小標題
Row table2_row =sheet.createRow(tb);
Cell table2_cell =table2_row.createCell(0);
table2_cell.setCellValue(cell1);
table2_cell.setCellStyle(titleStyle(wb));
//小標題
Row table2_row2 =sheet.createRow(tb+1);
Cell table2_cell2 =table2_row2.createCell(0);
table2_cell2.setCellValue(cell2);
table2_cell2.setCellStyle(titleStyle2(wb));
Cell table2_cell3 =table2_row2.createCell(2);
table2_cell3.setCellValue("關注來源:默認全部 ");
table2_cell3.setCellStyle(titleStyle2(wb));
//合併單元格
sheet.addMergedRegion(new CellRangeAddress(tb,tb,0,2));
}
/**
* 動態表頭樣式
*/
public static void createTableHeader(XSSFWorkbook wb,Sheet sheet,int rows,String title){
Row row4 =sheet.createRow(rows+2);
String [] header = title.split(",");
for(int i=0;i<header.length;i++){
Cell cell4 =row4.createCell(i);
cell4.setCellValue(header[i]);
if(i<2){
cell4.setCellStyle(cellHeaderStyle1(wb));
}else if(i>=2&&i<7){
cell4.setCellStyle(cellHeaderStyle2(wb));
}else if(i>=7&&i<11){
cell4.setCellStyle(cellHeaderStyle3(wb));
}else{
cell4.setCellStyle(cellHeaderStyle4(wb));
}
}
}
/**
* 第二個sheet
*
*
* 新版用法 3.8版
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based) from 行
2, //last row (0-based) to 行
1, //first column (0-based) from 列
1 //last column (0-based) to 列
));
*
*
*/
public static boolean sheet2(XSSFWorkbook wb){
Sheet sheet2 = wb.createSheet("固定週報模板Weekly-粉絲統計by Day");
sheet2.setDefaultRowHeight((short)(290));
for(int i=0;i<50;i++){
sheet2.setColumnWidth(i,10*300); //設置寬度
}
//總標題
Row row1 =sheet2.createRow(0);
row1.setHeightInPoints(30);
Cell cell = row1.createCell(0);
cell.setCellStyle(reportStyle(wb));
cell.setCellValue("媽媽俱樂部微信數據週報"+UtilTools.formatDate(new Date(),"yyyy-MM-dd"));
sheet2.addMergedRegion(new CellRangeAddress(0,0,0,3));
int tb=1;
createTableNote(wb,sheet2,tb,"4. DETAIL(註冊&粉絲 BY DAY)","時間:默認最近1自然周");
//表格頭
String title="時間,渠道來源,新增粉絲數,取消關注人數,取關率,淨增粉絲數,粉絲總數,新增會員數,新增會員註冊率,會員總數,註冊率,新增備孕會員,新增孕早會員,新增孕中會員,新增孕晚,新增0-6個月,新增7-12個月會員,新增1-3歲會員,新增3歲以上會員,新增備孕會員,新增孕早佔比,新增孕中佔比,新增孕晚,新增0-6個月,新增7-12個月佔比,新增1-3歲佔比,新增3歲以上佔比";
createTableHeader(wb,sheet2,tb,title);
createTableContent(wb,sheet2,tb+2);
return true;
}
/**
* 自定義顏色
* 藍色(大標題)
*/
public static Color blue(){
return new Color(0,112,192);
}
/**
* 自定義顏色
* 淡藍色
*/
public static Color blue2(){
return new Color(0,176,240);
}
/**
* 自定義顏色
* 綠色
*/
public static Color green(){
return new Color(146,208,80);
}
/**
* 自定義顏色
* 黃色
*/
public static Color orange(){
return new Color(255,192,0);
}
/**
* 自定義顏色
* 白色
*/
public static Color white(){
return new Color(255,255,255);
}
/**
* 自定義顏色
* 黑色
*/
public static Color black(){
return new Color(0,0,0);
}
/**
* 自定義顏色
* 粉紅色
*/
public static Color pink(){
return new Color(255,55,174);
}
/**
* 自定義顏色
* 灰色
*/
public static Color gray(){
return new Color(217,217,217);
}
/**
* 自定義單元格樣式
* 大標題
* @param args
*/
public static CellStyle reportStyle(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(new XSSFColor(blue()));
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setWrapText(true);
return style;
}
/**
* 自定義單元格樣式
* 表頭
* @param args
*/
public static CellStyle titleStyle(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);// 字體顏色
font.setFontHeightInPoints((short)10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
return style;
}
/**
* 自定義單元格樣式
* 表頭
* @param args
*/
public static CellStyle titleStyle2(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);// 字體顏色
font.setFontHeightInPoints((short)10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellStyle(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
/*
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
*/
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellSummayStyle(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.BLACK.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(gray()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellHeaderStyle1(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
/*style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);*/
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(blue()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellHeaderStyle2(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
/*style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);*/
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(blue2()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellHeaderStyle3(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
/*style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);*/
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(green()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellHeaderStyle4(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
/*style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);*/
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(orange()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
/**
* 自定義單元格樣式
* 內容
* @param args
*/
public static CellStyle cellHeaderStyle5(XSSFWorkbook wb) {
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(HSSFColor.WHITE.index);// 字體顏色
font.setFontHeightInPoints((short) 9);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體增粗
font.setFontName("微軟雅黑");
// 把字體應用到當前的樣式
style.setFont(font);
/*style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);*/
XSSFCellStyle styleTemp=((XSSFCellStyle)style);
styleTemp.setAlignment(CellStyle.ALIGN_CENTER);
styleTemp.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
//控制顏色
styleTemp.setFillForegroundColor(new XSSFColor(pink()));
styleTemp.setFillPattern(CellStyle.SOLID_FOREGROUND);
return style;
}
public static void main(String[] args) {
try {
DownLoadWeeklyReportExcel(null,"D:\\test.xlsx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Excel導出模板
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.