package com.ruoyi.common.utils.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class ExcelWriter {

    private static final Logger log = LoggerFactory.getLogger(ExcelWriter.class);

    public static void write(List<Map<String, Object>> data, String fileName) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        try (FileOutputStream outputStream = new FileOutputStream(fileName)) {
            workbook.createSheet("Sheet1");
            if (data.size() != 0) {
                Row headerRow = workbook.getSheetAt(0).createRow(0);
                int colNum = 0;
                for (String key : data.get(0).keySet()) {
                    Cell cell = headerRow.createCell(colNum++);
                    cell.setCellValue(key);
                }
                int rowNum = 1;
                for (Map<String, Object> item : data) {
                    Row row = workbook.getSheetAt(0).createRow(rowNum++);
                    for (Object value : item.values()) {
                        Cell cell = row.createCell(colNum++);
                        if (value instanceof String) {
                            cell.setCellValue((String) value);
                        } else if (value instanceof Integer) {
                            cell.setCellValue((Integer) value);
                        } else if (value instanceof Double) {
                            cell.setCellValue((Double) value);
                        }
                    }
                }
            }
            workbook.write(outputStream);
        }
    }

    public static void write(List<Map<String, Object>> data, HttpServletResponse response, String sheetName) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        try {
            workbook.createSheet(sheetName);
            if (data.size() != 0) {
                Row headerRow = workbook.getSheetAt(0).createRow(0);
                int colNum = 0;
                for (String key : data.get(0).keySet()) {
                    Cell cell = headerRow.createCell(colNum++);
                    cell.setCellValue(key);
                }
                int rowNum = 1;
                for (Map<String, Object> item : data) {
                    colNum = 0;
                    Row row = workbook.getSheetAt(0).createRow(rowNum++);
                    for (Object value : item.values()) {
                        Cell cell = row.createCell(colNum++);
                        if (value instanceof String) {
                            cell.setCellValue((String) value);
                        } else if (value instanceof Integer) {
                            cell.setCellValue((Integer) value);
                        } else if (value instanceof Double) {
                            cell.setCellValue((Double) value);
                        }
                    }
                }
            }
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error("导出Excel异常{}", e.getMessage());
        } finally {
            IOUtils.closeQuietly(workbook);
        }
    }
}
