package com.hs.api.service.Impl;

import com.hs.api.service.ExcelService;
import me.chyxion.xls.TableToXls;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Override
    public String tableToXls(String realPath,String tableStr) {
        try{
            File directory = new File(realPath + "/excel");
            if(!directory.exists() && !directory.isDirectory()){
                directory.mkdir();
            }
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            String fileName = uuid + ".xls";
            FileOutputStream fout = new FileOutputStream(directory + "/" + fileName);
            TableToXls.process(tableStr,fout);
            fout.close();
            return "excel/" + fileName;
        }catch(Exception ex){
            log.error(ex.getMessage(),ex);
        }
        return null;
    }
}
