需求:根据Excel模板生成Excel,包括样式设置、单元格内容读取和填充、合并单元格等。
要求:1、开源免费;2、支持跨平台
设计:采用POI操作Excel
示例(对Excel2003文件的操作,即文件后缀是xls,单Sheet):
1. Excel模板
2. 生成的Excel
部分Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
测试代码:
/**
* POI通过模板生成Excel文件测试
*
* @throws Exception
*/
public static void genExcelTest() throws Exception {
String excel = "F:\\Users\\zyj\\Desktop\\2019-09-27 自由报表\\插件测试/生成的报表.xls";
String template = "F:\\Users\\zyj\\Desktop\\2019-09-27 自由报表\\插件测试/测试模板.xls";
List<Map<String, Object>> data = getData();
String[] cols = new String[] { "wfid", "wtid", "wtname" };
ExcelTemplete.genExcel(template, excel, cols, data);
}
/**
* 数据源测试数据
*
* @return
* @throws Exception
*/
private static List<Map<String, Object>> getData() throws Exception {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int num = 1;
Map<String, Object> map = null;
while (num <= 5) {
map = new HashMap<String, Object>();
map.put("wfid", 654211);
map.put("wtid", 654211001 + num);
map.put("wtname", "F" + StringUtils.leftPad(String.valueOf(num), 2, "0"));
list.add(map);
num++;
}
return list;
}
POI对Excel的操作:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
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.ss.util.CellRangeAddress;
public class ExcelTemplete {
/**
* POI通过模板生成Excel文件(单个Sheet)
*
* @param templete Excel模板的全路径
* @param excel
* @param cols
* @param data
* @throws IOException
*/
public static void genExcel(String templete, String excel, String[] cols, List<Map<String, Object>> data) throws IOException {
// 读取Excel模板
File file = new File(templete);
FileInputStream fis = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(fis);
genExcel(wb, cols, data);
// 如果excel文件目录不存在,创建
file = new File(excel);
if (!file.getParentFile().exists() && !file.getParentFile().isDirectory()) {
file.getParentFile().mkdirs();
}
// 创建excel文件(如果存在同名文件,会重新创建)
FileOutputStream output = new FileOutputStream(excel);
wb.write(output);
output.close();
}
/**
* POI对Excel基本操作
*
* @param wb Excel工作簿
* @param cols 数据的列名
* @param data 数据
* @throws IOException
*/
public static void genExcel(HSSFWorkbook wb, String[] cols, List<Map<String, Object>> data) throws IOException {
HSSFCellStyle style = wb.createCellStyle();
// 模板页
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
// 从第3行开始(第1行是标题,第2行是列名,之后是内容行)
int rowIndex = 2;
for (Map<String, Object> item : data) {
// 循环遍历,从第3行开始新建行
row = sheet.createRow(rowIndex);
for (int j = 0; j < cols.length; j++) {
// 设置单元格的样式、数据
cell = row.createCell(j);
cell.setCellStyle(style);
String value = item.get(cols[j]).toString();
cell.setCellValue(value);
}
rowIndex++;
}
// 合并最后一行数据,且合并后的单元格内容是第1行中的标题
// 读取单元格内容
cell = sheet.getRow(0).getCell(0);
String firsRowContent = getCellValue(cell) + " - 合并单元格测试";
// 合并单元格
CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex + 1, 0, cols.length - 1);
sheet.addMergedRegion(region);
// 设置单元格内容
row = sheet.createRow(rowIndex);
cell = row.createCell(0);
cell.setCellValue(firsRowContent);
}
/**
* 获取单元格内容文本(转化为字符串类型)
*
* @param cell Excel单元格
* @return String 单元格内容文本
*/
private static String getCellValue(HSSFCell cell) {
if (cell == null) {
return "";
}
String strCell = "";
switch (cell.getCellType()) {
case STRING:
strCell = cell.getStringCellValue();
break;
case NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
strCell = String.valueOf(cell.getErrorCellValue());
break;
case BLANK:
default:
strCell = "";
break;
}
if (strCell == null) {
return "";
}
return strCell;
}
}