所有导入jar包路径
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
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.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
POI-Excel默认样式
/**
* @Title:getPOIStyle
* @Description:TODO 获取POI-Excel默认样式
* @author:马家立
* @date:2020-7-6 11:33:21
* @param workbook
* @return XSSFCellStyle
* @throws Exception
*/
public static XSSFCellStyle getPOIStyle(XSSFWorkbook workbook) throws Exception {
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 居中
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
// 加粗字体
XSSFFont font = workbook.createFont();
// 字体大小
font.setFontHeight(12);
// 字体颜色
//font.setColor(new XSSFColor(Color.green));
//font.setColor(new XSSFColor(new Color(9, 99, 9)));//rgb颜色
// 字体加粗
// font.setBold(true);
style.setFont(font);
return style;
}
POI-Excel标题样式
/**
* @Title:getPOITitleStyle
* @Description:TODO 获取POI-Excel标题样式
* @author:马家立
* @date:2020-7-6 11:34:12
* @param workbook
* @return XSSFCellStyle
* @throws Exception
*/
public static XSSFCellStyle getPOITitleStyle(XSSFWorkbook workbook) throws Exception {
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 居中
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
// 加粗字体
XSSFFont font = workbook.createFont();
// 字体大小
font.setFontHeight(15);
// 字体颜色
// font.setColor(Font.COLOR_RED);
// 字体加粗
font.setBold(true);
style.setFont(font);
return style;
}
创建单元格,写入数据
/**
* @Title:creatCell
* @Description:TODO 创建单元格,写入数据
* @author:马家立
* @date:2020-7-6 12:03:54
* @param xssfRow--行
* @param content--内容
* @param line--第几列
* @param style--样式
* @throws Exception
* void
*/
public static void creatCell(XSSFRow xssfRow, String content, int line, XSSFCellStyle style) throws Exception {
// 创建列
XSSFCell cell = xssfRow.createCell(line);
if (null!=content&&!"".equals(content)) {
// 写入内容
cell.setCellValue(content);
}
// 设置样式
cell.setCellStyle(style);
}
POI-Excel写入内容并生成文件
/**
* @Title:writeExcel
* @Description:TODO Excel写入内容并生成文件
* @author:马家立
* @date:2020-7-6 11:43:51
* @param filePath--文件路径(C:\Users\userName\Desktop\")
* @param fileName--文件名字,带后缀(POIExcel.xls)
* @throws Exception
* void
*/
public void writeExcel(String filePath,String fileName) throws Exception {
XSSFWorkbook workbook = null;
FileOutputStream fout = null;
try {
// 路径若为空则默认为桌面
if(null==filePath||"".equals(filePath)) {
filePath = "C:\Users\"+System.getenv().get("USERNAME")+"\Desktop\";
}
// 文件名若为空则默认为POIExcel.xls
if(null==fileName||"".equals(fileName)) {
fileName = "POIExcel.xls";
}
/**
* -- 第一步:创建一个webbook,对应一个Excel文件
*/
workbook = new XSSFWorkbook();
/**
* -- 第二步:在webbook中添加一个sheet,对应Excel文件中的sheet
*/
XSSFSheet sheet = workbook.createSheet("子表名");
/**
* --第三步:声明格式写入内容
*/
// 声明标题样式
XSSFCellStyle titleStyle = getPOITitleStyle(workbook);
// 声明内容样式
XSSFCellStyle contentStyle = getPOIStyle(workbook);
// 合并单元格格式:起始行号,终止行号, 起始列号,终止列号
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(region);
// 第一行写入数据
int row = 0;
XSSFRow xssfRow = sheet.createRow(row);
creatCell(xssfRow, "第一行第一列", 0, titleStyle);
// 第二行写入数据
row++;
xssfRow = sheet.createRow(row);
creatCell(xssfRow, "第二行第一列", 0, contentStyle);
creatCell(xssfRow, "第二行第二列", 1, contentStyle);
creatCell(xssfRow, "第二行第三列", 2, contentStyle);
// 设置每一列的宽度为自适应
int lines = 3;
for (int i = 0; i < lines; i++) {
sheet.autoSizeColumn(i, true);
sheet.setColumnWidth(i, (sheet.getColumnWidth(i) * 17) / 10);
}
/**
* --创建多个表
*/
XSSFSheet sheet2 = workbook.createSheet("子表名2");
// 合并单元格格式:起始行号,终止行号, 起始列号,终止列号
sheet2.addMergedRegion(region);
// 第一行写入数据
row = 0;
xssfRow = sheet2.createRow(row);
creatCell(xssfRow, "第一行第一列222", 0, titleStyle);
// 第二行写入数据
row++;
xssfRow = sheet2.createRow(row);
creatCell(xssfRow, "第二行第一列222", 0, contentStyle);
creatCell(xssfRow, "第二行第二列222", 1, contentStyle);
creatCell(xssfRow, "第二行第三列222", 2, contentStyle);
// 设置每一列的宽度为自适应
lines = 3;
for (int i = 0; i < lines; i++) {
sheet2.autoSizeColumn(i, true);
sheet2.setColumnWidth(i, (sheet.getColumnWidth(i) * 17) / 10);
}
/**
* --第四步:创建File文件,把workbook写入File
*/
File file = new File(filePath+fileName);
if (!file.exists()) {
// 文件不存在则创建新文件
file.createNewFile();
fout = new FileOutputStream(file);
workbook.write(fout);
} else {
// 文件存在则覆盖写入数据
fout = new FileOutputStream(file);
workbook.write(fout);
}
} finally {
if (null != fout) {
fout.close();
}
if (null != workbook) {
workbook.close();
}
}
}
main函数POI方式导出Excel
public static void main(String[] args) {
POIExcel poiExcel = new POIExcel();
try {
// 测试POI写入Excel
poiExcel.writeExcel(null, null);
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}