有个问题要注意,数据库导出Map数据如果值为空,这个字段和值不会查询出来
因为我上面配置没成功----execl列名(我用的是sql中文别名截取,就不用创建实体类了,如果你上面没问题就忽略)
第一步POM文件
<properties>
<!-- 版本管理 -->
<poi.version>3.17</poi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
第2步JAVA文件(注释最好看一下)
public class ExportExcel
{
public static List<String> keyList=new ArrayList<>();
public static int num;
**//fileName execl地址
//title execlsheet名
//list 你的列名(我的)
//dataset 你的Map数据
//index 目前的行数**
public static Workbook exportExcel(String fileName, String title,List<String> list,
List<LinkedHashMap<String, Object>> dataset, int index)
{
Sheet sheet=null;
Workbook workbook=null;
File file = new File(fileName);
if(index==0&&file.exists())file.delete();
if(!file.exists()){
if (fileName.endsWith("xlsx"))
{
workbook = new XSSFWorkbook();
} else if (fileName.endsWith("xls"))
{
workbook = new HSSFWorkbook();
} else
{
try
{
throw new Exception("invalid file name, should be xls or xlsx");
} catch (Exception e)
{
e.printStackTrace();
}
}
sheet=workbook.createSheet(title);
}else {
try(BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(fileName))
) {
workbook=new XSSFWorkbook(bufferedInputStream);
} catch (Exception e) {
e.printStackTrace();
}
sheet=workbook.getSheetAt(0);
}
keyList=new ArrayList<>(list);
**//列名转成数组**
String[] headers=keyList.toArray(new String[keyList.size()]);
// 列名
Row row;
if(index==0){
//添加第一行
row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++)
{
Cell cell = row.createCell(i);
sheet.setColumnWidth(i, 5000);
cell.setCellValue(headers[i]);
}
}else {
index= sheet.getLastRowNum();
}
Iterator<LinkedHashMap<String,Object>> it = dataset.iterator();
while (it.hasNext())
{
index++;
row = sheet.createRow(index);
Map map = it.next();
Set<String> mapKey = (Set<String>)map.keySet();
Iterator<String> iterator = mapKey.iterator();
num = 0;
while(iterator.hasNext()){
String key = iterator.next();
if(key.equals("ROW_ID")) {
continue;
}
if(num==5) {
System.out.println("aaa");
}
Cell cell = row.createCell(num);
**//空的值填空**
Cell cell1 = addNum(cell, key, row);
Object obj = map.get(key);
if(cell1!=null){
cell1 = row.createCell(num);
add(cell1,obj);
num++;
continue;
}
add(cell,obj);
num++;
}
}
try(FileOutputStream fos=new FileOutputStream(fileName);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);)
{
workbook.write(bufferedOutputStream);
} catch (IOException e)
{
e.printStackTrace();
}
return workbook;
}
public static Cell addNum(Cell cell,String key,Row row){
while (true){
if(num!=keyList.indexOf(key)){
cell.setCellValue("");
num++;
Cell cell1 = row.createCell(num);
addNum(cell1,key,row);
return cell1;
}else {
break;
}
}
return null;
}
public static void add(Cell cell,Object obj){
//对数字的处理(不然会导出文本格式)
if(obj instanceof BigDecimal){
cell.setCellValue(Integer.valueOf(String.valueOf(obj)));
}
else if (obj instanceof Double) {
cell.setCellValue(Double.parseDouble(String.valueOf(obj)));
} else {
cell.setCellValue(String.valueOf(obj));
}
}
}