首先,准备要Excel模板
Excel模板需要制定设计模板:
maven包
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
上代码:
/**
* 导入Excel模板 生成Java代码
*
* @param args
*/
public static void main(String[] args) {
try {
//Excel模板文件地址
FileInputStream excelFile = new FileInputStream(new File("D:\\Desktop\\报表设计.xlsx"));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
//生成Java代码文件地址
FileOutputStream outputStream = new FileOutputStream(new File("D:\\javaFile\\dome.java"));
StringBuffer sb = new StringBuffer();
sb.append("import com.kii.util.ExcelUtils;\n" + "import org.apache.poi.ss.usermodel.*;\n"
+ "import org.apache.poi.ss.util.CellRangeAddress;\n"
+ "import org.apache.poi.xssf.usermodel.XSSFWorkbook;"
+ "import java.io.*;\n\n\n\n\n\n");
sb.append("public class ClassName {\n\n\n");
sb.append(" public static void fieldName(){\n\n");
sb.append(" XSSFWorkbook workbook = new XSSFWorkbook();\n"
+ " Sheet sheet = workbook.createSheet(\"测试工作簿01\");\n"
+ " Row row = null;\n"
+ " Cell cell = null;\n");
while (iterator.hasNext()) {
Row currentRow = null;
currentRow = iterator.next();
Iterator<Cell> cellIterator = null;
Cell currentCell = null;
cellIterator = currentRow.iterator();
int count = 0;
while (cellIterator.hasNext()) {
currentCell = cellIterator.next();
if (currentCell.getColumnIndex() == 0 && currentCell.toString().equals("标题")) {
sb.append(" //标题 \n"
+ " row = sheet.createRow(0);\n"
+ " cell = row.createCell(0);\n"
+ " sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));\n"
+ " cell.setCellStyle(ExcelUtils.setTitleStyle(workbook, 1, 1, true));\n"
+ " cell.setCellValue(\"");
currentRow = iterator.next();
cellIterator = currentRow.iterator();
currentCell = cellIterator.next();
sb.append(currentCell.toString());
sb.append("\");\n");
break;
} else if (currentCell.getColumnIndex() == 0 && currentCell.toString().equals("文本区域")) {
break;
} else if (!currentCell.toString().equals("") && !(currentCell.toString().equals("表格区域"))) {
if (count == 0) {
sb.append(" row = sheet.createRow(")
.append(currentCell.getRowIndex())
.append(");\n");
count++;
}
sb.append(" cell = row.createCell(")
.append(currentCell.getColumnIndex()).append(");\n")
.append(" cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));\n")
.append(" cell.setCellValue(\"").append(currentCell).append(":\");\n");
} else if (currentCell.toString().equals("表格区域")) {
currentRow = iterator.next();
cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
currentCell = cellIterator.next();
if (!currentCell.toString().equals("")) {
if (count == 0) {
sb.append(" row = sheet.createRow(")
.append(currentCell.getRowIndex())
.append(");\n");
count++;
}
sb.append(" cell = row.createCell(")
.append(currentCell.getColumnIndex()).append(");\n")
.append(" cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));\n")
.append(" cell.setCellValue(\"")
.append(currentCell)
.append("\");\n");
}
}
}
}
sb.append("\n");
}
sb.append(" try {\n"
+ " FileOutputStream outputStream = new FileOutputStream(\"D:\\\\javaFile\\\\结果.xlsx\");\n"
+ " workbook.write(outputStream);\n"
+ " workbook.close();\n"
+ " } catch (IOException e) {\n"
+ " e.printStackTrace();\n"
+ " }\n\n");
sb.append("\n}\n\n");
sb.append("\n\n}");
outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8));
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ExcelUtils工具类
package com.kii.util;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Excel 工具类
*/
public class ExcelUtils {
/**
* @param workbook 对象
* @param fontSize 字体大小
* @param align 对齐方式
* 0 : 默认方式(左对齐)
* 1 : 居中
* 2 : 右对齐
* @param bold 是否加粗
*/
public static CellStyle setTitleStyle(XSSFWorkbook workbook, int fontSize, int align, boolean bold) {
//样式
CellStyle style = workbook.createCellStyle();
//字体
Font font = workbook.createFont();
//居中
style.setAlignment(HorizontalAlignment.CENTER);
//字体大小
font.setFontHeightInPoints((short) 32);
//加粗
font.setBold(true);
style.setFont(font);
return style;
}
/**
* @param workbook 对象
* @param fontSize 字体大小
* @param align 对齐方式
* 0 : 默认方式(左对齐)
* 1 : 居中
* 2 : 右对齐
* @param bold
* @return
*/
public static CellStyle setContentStyle(XSSFWorkbook workbook, int fontSize, int align, boolean bold) {
//样式
CellStyle style = workbook.createCellStyle();
//字体
Font font = workbook.createFont();
//居中
//style.setAlignment(HorizontalAlignment.CENTER);
//字体大小
font.setFontHeightInPoints((short) 12);
//加粗
font.setBold(true);
style.setFont(font);
return style;
}
/**
* @param workbook 对象
* @param fontSize 字体大小
* @param align 对齐方式
* 0 : 默认方式(左对齐)
* 1 : 居中
* 2 : 右对齐
* @param bold
* @return
*/
public static CellStyle setTableTitleStyle(XSSFWorkbook workbook, int fontSize, int align, boolean bold) {
//样式
CellStyle style = workbook.createCellStyle();
//字体
Font font = workbook.createFont();
//居中
//style.setAlignment(HorizontalAlignment.CENTER);
//字体大小
font.setFontHeightInPoints((short) 11);
//加粗
font.setBold(true);
style.setFont(font);
return style;
}
}
运行后,将生成的Java代码,复制到编译工具中
注:博主生成的是一个类中的方法,需要使用main方法去调用,或者直接把方法中的代码,复制到main方法中,下列就是博主使用复制到main方法中测试的
public static void main(String[] args) {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("测试工作簿01");
Row row = null;
Cell cell = null;
//标题
row = sheet.createRow(0);
cell = row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
cell.setCellStyle(ExcelUtils.setTitleStyle(workbook, 1, 1, true));
cell.setCellValue("测试标题");
row = sheet.createRow(3);
cell = row.createCell(0);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容1:");
cell = row.createCell(4);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容2:");
row = sheet.createRow(4);
cell = row.createCell(4);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容3:");
row = sheet.createRow(5);
cell = row.createCell(0);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容4:");
row = sheet.createRow(6);
cell = row.createCell(0);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容5:");
cell = row.createCell(4);
cell.setCellStyle(ExcelUtils.setContentStyle(workbook, 1, 1, true));
cell.setCellValue("文本内容6:");
row = sheet.createRow(8);
cell = row.createCell(0);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题1");
cell = row.createCell(1);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题2");
cell = row.createCell(2);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题3");
cell = row.createCell(3);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题4");
cell = row.createCell(4);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题5");
cell = row.createCell(5);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题6");
cell = row.createCell(6);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题7");
cell = row.createCell(7);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题8");
cell = row.createCell(8);
cell.setCellStyle(ExcelUtils.setTableTitleStyle(workbook, 1, 1, true));
cell.setCellValue("标题9");
try {
FileOutputStream outputStream = new FileOutputStream("D:\\javaFile\\结果.xlsx");
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
执行生成Java代码后,导出的Excel
博主只是帮忙生成了一个简单的模板,根据业务的需求做调整,即可。