1、maven 依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
创建Excel 表格 方法
public class ExcelUtil {
public static XSSFWorkbook createExcelObject(String[] title, String[] key, List<Map<String, Object>> list) {
return createExcelObject(title, key, list, new java.awt.Color(255, 255, 255));
}
public static XSSFWorkbook createExcelObject(String[] title, String[] key, List<Map<String, Object>> list, java.awt.Color headerForeground) {
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(new XSSFColor(headerForeground));
Font font = wb.createFont();
font.setFontHeightInPoints((short) 14);
font.setBold(true);
style.setFont(font);
XSSFRow row = sheet.createRow((int) 0);
for (int i = 0; i < title.length; i++) {
XSSFCell cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
XSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HorizontalAlignment.CENTER);
Font font2 = wb.createFont();
font2.setFontHeightInPoints((short) 12);
font2.setBold(false);
style2.setFont(font2);
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
Map<String, Object> maps = list.get(i);
if (key != null && key.length > 0) {
for (int i1 = 0; i1 < key.length; i1++) {
XSSFCell cell = row.createCell(i1);
cell.setCellValue(maps.get(key[i1]).toString());
cell.setCellStyle(style2);
}
} else {
Integer integer = 0;
for (Map.Entry entry : maps.entrySet()) {
XSSFCell cell = row.createCell(integer);
cell.setCellValue(entry.getValue().toString());
cell.setCellStyle(style2);
integer++;
}
}
}
return wb;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return null;
}
}
3、调用
public static void main(String[] args) {
String[] title = {"第一列", "第二列", "第三列", "第四列","第五列"};
String[] key = {"columns1", "columns2", "columns3", "columns4","columns15"};
List<Map<String,Object>> valueList=new ArrayList<>();
for (int i = 0; i < 108; i++) {
Map<String,Object> valueMap=new HashMap<>();
valueMap.put(key[0],String.format("第%d行_第%d列", i+1,1));
valueMap.put(key[1],String.format("第%d行_第%d列", i+1,2));
valueMap.put(key[2],String.format("第%d行_第%d列", i+1,3));
valueMap.put(key[3],String.format("第%d行_第%d列", i+1,4));
valueMap.put(key[4],String.format("第%d行_第%d列", i+1,5));
valueList.add(valueMap);
}
XSSFWorkbook wb = ExcelUtil.createExcelObject(title,key,valueList,new java.awt.Color(217,217,217));
try {
if (wb == null){
throw new Exception("excel文件解析异常");
}
String date = DateUtil.dateToString(LocalDateTime.now(), "yyyyMMdd");
String hHmmss = new SimpleDateFormat("HHmmss").format(new Date());
String localPath = "E:" + File.separator +"Download" +File.separator ;
File outFileRoot = new File(localPath);
if (!outFileRoot.exists()){
outFileRoot.mkdirs();
}
String outFilePath=localPath+"导出"+date+hHmmss+".xlsx";
System.out.println(" outFilePath="+outFilePath);
wb.write(new FileOutputStream(outFilePath));
wb.close();
}catch (Exception e){
e.printStackTrace();
}
}
生成文件,如图:
|