Java 利用pio生成Excel实例:
设置生成Excel的主要参数:
package com.gohigh.report;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.contrib.HSSFCellUtil;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import com.gohigh.cg.Download;
import com.gohigh.util.DBUtil;
import com.gohigh.util.DateUtil;
public class CreateMonthReport {
public static String path="app\\temp\\excel\\";
static{
path=new Download().getClassPath().replace("WEB-INF\\classes", "")+path;
}
private static HSSFFont fontStyle = null;
private static HSSFCellStyle cellStyle = null;
public static HSSFFont getTitFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("华文中宋");
fontStyle.setColor(HSSFColor.DARK_BLUE.index);
fontStyle.setFontHeightInPoints((short)16);
return fontStyle;
}
public static HSSFFont getHdrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)10);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
return fontStyle;
}
public static HSSFFont getShdrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)10);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFFont getContentFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)10);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFCellStyle getDefStyle(HSSFWorkbook wb) {
cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
return cellStyle;
}
// 大标题样式
public static HSSFCellStyle getTitStyle(HSSFWorkbook wb) {
cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setFont(getTitFont(wb));
return cellStyle;
}
//头级表头样式
public static HSSFCellStyle getHdrStyle(HSSFWorkbook wb) {
cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(getHdrFont(wb));
return cellStyle;
}
//表头样式
public static HSSFCellStyle getShdrStyle(HSSFWorkbook wb) {
cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(getShdrFont(wb));
return cellStyle;
}
// 内容样式(千位分隔符)
public static HSSFCellStyle getContentStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
//short doubleFormat = wb.createDataFormat().getFormat("_(* #,##0_);_(* (#,##0)");//HSSFDataFormat.getBuiltinFormat("#.##");
short doubleFormat = wb.createDataFormat().getFormat("###,###,##0");
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
//内容格式(整数)
public static HSSFCellStyle getContentStyle2(HSSFWorkbook wb){
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
//short doubleFormat = wb.createDataFormat().getFormat("_(* #,##0_);_(* (#,##0)");//HSSFDataFormat.getBuiltinFormat("#.##");
short doubleFormat = wb.createDataFormat().getFormat("###,###,##0.00");
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
//合计内容格式(整数)
// 内容样式(保留一位小数)
public static HSSFCellStyle getContentStyle1(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
short doubleFormat = wb.createDataFormat().getFormat("###,###,##0.00");
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
// 合计样式(千位分隔符)
public static HSSFCellStyle getContentSumStyle(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
if(0==wb.getSheetIndex("表(1)网络维护操作数据")){
HSSFSheet shell = wb.getSheet("表(1)网络维护操作数据");
shell.setColumnWidth(5, 3000);
shell.setColumnWidth(6, 3000);
shell.setColumnWidth(13, 3000);
shell.setColumnWidth(14, 3000);
shell.setColumnWidth(9, 3000);
shell.setColumnWidth(10, 3000);
shell.setColumnWidth(17, 3000);
shell.setColumnWidth(18, 3000);
shell.setColumnWidth(23, 3000);
shell.setColumnWidth(24, 3000);
shell.setColumnWidth(25, 3000);
shell.setColumnWidth(26, 3000);
}
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
//short doubleFormat = wb.createDataFormat().getFormat("_(* #,##0_);_(* (#,##0)");
//short doubleFormat = wb.createDataFormat().getFormat("#,##0.00_ ;[红色]-#,##0.00");
short doubleFormat = wb.createDataFormat().getFormat("###,###,##0.00");
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
//// 合计样式(千位分隔符)
public static HSSFCellStyle getContentSumStyle2(HSSFWorkbook wb) {
HSSFCellStyle cellStyle = wb.createCellStyle();
if(0==wb.getSheetIndex("表(1)网络维护操作数据")){
HSSFSheet shell = wb.getSheet("表(1)网络维护操作数据");
shell.setColumnWidth(5, 3000);
shell.setColumnWidth(6, 3000);
shell.setColumnWidth(13, 3000);
shell.setColumnWidth(14, 3000);
shell.setColumnWidth(9, 3000);
shell.setColumnWidth(10, 3000);
shell.setColumnWidth(17, 3000);
shell.setColumnWidth(18, 3000);
shell.setColumnWidth(23, 3000);
shell.setColumnWidth(24, 3000);
shell.setColumnWidth(25, 3000);
shell.setColumnWidth(26, 3000);
}
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
//short doubleFormat = wb.createDataFormat().getFormat("_(* #,##0_);_(* (#,##0)");
//short doubleFormat = wb.createDataFormat().getFormat("#,##0.00_ ;[红色]-#,##0.00");
short doubleFormat = wb.createDataFormat().getFormat("###,###,##0");
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
// 内容样式(保留两位小数{表6}),无背景色
public static HSSFCellStyle getContentSumStyle2(HSSFWorkbook wb,int bit) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
String numberFormat ="0";
if(bit>0){
numberFormat+=".";
}
for(int i=0;i<bit;i++){
numberFormat+="0";
}
short doubleFormat = wb.createDataFormat().getFormat(numberFormat);
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
// 百分比样式(合计)
public static HSSFCellStyle getPctStyle(HSSFWorkbook wb,int bit) {
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//自动换行
cellStyle.setWrapText(true);
cellStyle.setFont(CreateMonthReport.getContentFont(wb));
String numberFormat ="0";
if(bit>0){
numberFormat+=".";
}
for(int i=0;i<bit;i++){
numberFormat+="0";
}
numberFormat+="%";
short doubleFormat = wb.createDataFormat().getFormat(numberFormat);;
cellStyle.setDataFormat(doubleFormat);
return cellStyle;
}
public static boolean createExcel(String reportName,List conList1,List conList2,List conList3,List conList4,List conList5){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1=wb.createSheet("表(1)网络维护操作数据");
HSSFSheet sheet2=wb.createSheet("表(2)网络故障数据");
HSSFSheet sheet3=wb.createSheet("表(3)故障影响数据");
HSSFSheet sheet4=wb.createSheet("表(4)维护操作数据");
HSSFSheet sheet5=wb.createSheet("表(5)主要厂家设备故障数据");
HSSFSheet sheet6=wb.createSheet("表(6)网络运行质量分析");
HSSFSheet sheet7=wb.createSheet("表(7)维护能力分析");
//sheet1.setDefaultColumnStyle(HSSFDataFormat.getBuiltinFormat("0"), getDefStyle(wb));
Sheet1Report.CreateSheet(sheet1, wb,conList1);
Sheet2Report.CreateSheet(sheet2, wb,conList2);
Sheet3Report.CreateSheet(sheet3, wb,conList3);
Sheet4Report.CreateSheet(sheet4, wb,conList4);
Sheet5Report.CreateSheet(sheet5, wb,conList5);
Sheet6Report.CreateSheet(sheet6, wb);
Sheet7Report.CreateSheet(sheet7, wb);
try {
FileOutputStream fileOut = new FileOutputStream(path+reportName+".xls");
wb.write(fileOut);
fileOut.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args){
String currentDate = DateUtil.getCurrentDate();
String date = currentDate.substring(0,7);
//表1 网络维护基础数据
String sql1 = "select * from TBL_FILIALE_MR_NETBASEDATA where TO_CHAR(mrtime,'yyyy-mm')='"+date+"'";
List list1 = DBUtil.executeQuery(sql1);
//表2 网络故障数据
String sql2 = "select * from TBL_FILIALE_MR_NETFAULT where TO_CHAR(mrtime,'yyyy-mm')='"+date+"'";
List list2 = DBUtil.executeQuery(sql2);
//表3 故障影响数据
String sql3 = "select * from TBL_FILIALE_MR_FAULTAFFECT where TO_CHAR(mrtime,'yyyy-mm')='"+date+"'";
List list3 = DBUtil.executeQuery(sql3);
//表4 维护操作数据
String sql4 = "select * from TBL_FILIALE_MR_MAINOPEDATA where TO_CHAR(mrtime,'yyyy-mm')='"+date+"'";
List list4 = DBUtil.executeQuery(sql4);
//表5 主要厂商故障数据
String sql5 = "select * from TBL_FILIALE_MR_FACEQUFAULT where TO_CHAR(mrtime,'yyyy-mm')='"+date+"'";
List list5 = DBUtil.executeQuery(sql5);
CreateMonthReport.createExcel("workbook",list1,list2,list3,list4,list5);
}
// 正确显示合并后的单元格样式
public static void setRegionStyle(HSSFSheet sheet,CellRangeAddress region,HSSFCellStyle cs) {
for (int i = region.getFirstRow(); i <= region.getLastRow(); i ++) {
HSSFRow row = HSSFCellUtil.getRow(i,sheet);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
HSSFCell cell = HSSFCellUtil.getCell(row,(short)j);
cell.setCellStyle(cs);
}
}
}
}