栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

小项目 GIT生成公司EXCEL周报(5)EXCEL创建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小项目 GIT生成公司EXCEL周报(5)EXCEL创建

项目任务5 创建excel表格(1)  EXCEL jar 包导入

需要一些jar包,项目jar包都放在百度云了:

百度网盘 请输入提取码 提取码:gr6d

导入如下:

 大致格式:

第一行:合并9列,加粗居中【每周工作计划和进度记录表】

第二行:姓名:+配置姓名(加粗)岗位+配置岗位(加粗) 记录日期:【开始日期-结束日期】合并3列,

第三行是各种固定的标题,一共9列

第四行:开始,是实际的日志内容,工作时间,我这里都简单的定义为每天的上班时间到下班时间,目前进度100%,工期0.4,提前完成率0%备注全部完成

然后第四行到日志结束行前面要加个合并列

日志后1:北京蓝色字居中加粗

日志后2,写各种标题

首列加粗居中北京2行合并

今天写前三行内容,明天续写后续内容

.ExcelUtil.java

❤李癩❤李癩❤李癩

import com.weekpaper.bean.ConfigData;
import com.weekpaper.bean.LogContent;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class ExcelUtil {
    public static void createExcel(String fileName, Map> projectLogs, ConfigData data) {
        WritableWorkbook book = null;
        try{
            //1.创建文件
            File f = FileUtil.reCreateFile(fileName);
             
            //2. 创建Excel对象
            book = Workbook.createWorkbook(f);
             
            //3. 创建工作表
            WritableSheet sheet = book.createSheet("sheet_one", 0);
             
            //4. 配置工作表各列宽度,各行高度
            int totalSize = getTotalSize(projectLogs);
            configSheetInfo(sheet,totalSize);
             
            //5. 添加标题 第一行
            addTitle1(sheet);
             
            //6. 添加标题第二行,配置姓名等信息
            WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 11,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableCellFormat style2 = new WritableCellFormat(font); // 单元格定义
            addTitle2(sheet,style2,data);
             
            //7. 添加标题第三行,固定内容
            addTitle3(sheet,style2);
        } catch (IOException e) {
            System.out.println(e);
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(book!=null){
                try{
                    //写入数据并关闭文
                    book.write();
                    book.close();
                }catch (IOException e){
                 
                } catch (WriteException e) {
                    e.printStackTrace();
                }
     
            }
        }
    }
     
    private static void addTitle3(WritableSheet sheet, WritableCellFormat style2) throws WriteException {
        String[] cols = new String[]{
                "类型","工作任务名称","工作开始时间","计划完成时间","实际完成时间","目前进度(%)","工期(天)","提前完成率(%)","备注(必填)"
        };
         
        for(int i = 0; i < cols.length; i++){
            Label head3 = new Label(i,2,cols[i],style2);
            sheet.addCell(head3);
        }
        
    }
     
    private static void addTitle2(WritableSheet sheet, WritableCellFormat style2, ConfigData data) throws WriteException {
        style2.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色
        style2.setAlignment(jxl.format.Alignment.LEFT); // 设置对齐方式
        style2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
        sheet.mergeCells(2, 1, 4, 1);
         
        Map map = new HashMap<>();
        map.put(0,"姓名:"+data.getAuthor());
        map.put(1,"岗位:"+data.getCareer());
        map.put(2,"记录周期:"+DateUtil.toString(data.getStartDate())+"-"+DateUtil.toString(data.getEndDate()));
        map.put(5,"");
        map.put(6,"");
        map.put(7,"");
        map.put(8,"");
         
        for(Integer col:map.keySet()){
            Label head2 = new Label(col,1,map.get(col));
            sheet.addCell(head2);
        }
    }
     
    private static void addTitle1(WritableSheet sheet) throws WriteException {
        WritableFont wf_title = new WritableFont(WritableFont.createFont("宋体"), 16,
                WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
        WritableCellFormat wcf_title = new WritableCellFormat(wf_title); // 单元格定义
        wcf_title.setBackground(jxl.format.Colour.WHITE); // 设置单元格的背景颜色
        wcf_title.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
        wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //设置边框
     
        Label titleName = new Label(0,0,"每周工作计划和进度记录表",wcf_title);
        sheet.addCell(titleName);
        sheet.mergeCells(0, 0, 8, 0);
    }
     
    private static int getTotalSize(Map> projectLogs){
        int size = 0;
        for(List content:projectLogs.values()){
            size += content.size();
        }
        return size;
    }
     
    private static void configSheetInfo(WritableSheet sheet,int logLen) throws RowsExceededException {
        int[] width = new int[]{20,104,56,56,56,15,23,17,44};
        for(int i = 0; i < width.length; i++){
            sheet.setColumnView(i, width[i]); // 设置列的宽度
        }
         
        sheet.setRowView(logLen+3, 2000);//下周工作计划安排记录
        sheet.setRowView(logLen+5, 4000);//工作中存在的问题及解决方法
    }
}

❤李癩❤李癩❤李癩

其他修改文件:

WeekPaper.java

❤李癩❤李癩❤李癩

import com.weekpaper.bean.ConfigData;
import com.weekpaper.bean.LogContent;
import com.weekpaper.util.*;
 
import java.util.*;
 
public class WeekPaper {
     
    public static void main(String[] args) {
        if(args==null || args.length<1){
            LogUtil.severe("找不到配置文件!");
            System.exit(0);
        }
        //1.加载配置文件信息
        ConfigData cd = loadConfigFile(args[0]);
         
        //2. 创建Git日志文件读取日志内容
        Map> projectLogs = GitDealUtil.createLogTxt(cd);
         
        //3. 创建 excel 周报文件
        String excelFile = FileUtil.getFilePath(cd.getOutputPath(),DateUtil.toString(new Date())+"_"+cd.getAuthor()+".xls");
        ExcelUtil.createExcel(excelFile,projectLogs,cd);
    }
     
     
     
     
    private static ConfigData loadConfigFile(String path) {
        ConfigData cd = FileUtil.loadConfigData(path);
        if(cd == null){
            LogUtil.severe("配置内容有误!");
            System.exit(0);
        }else{
            LogUtil.info("获取配置文件"+ FileUtil.getFilePath(path)+"成功!");
//            LogUtil.info("获取配置文件"+ FileUtil.getFilePath(args[0])+"成功!信息如下:");
//            LogUtil.info(cd.toString());
        }
        return cd;
    }
     
     
}

❤李癩❤李癩❤李癩

FileUtil加方法

❤李癩❤李癩❤李癩

public static String getFilePath(String root,String name){
        return root+File.separator+name;
}

❤李癩❤李癩❤李癩

GitDealUtil.java

❤李癩❤李癩❤李癩

import com.weekpaper.bean.ConfigData;
import com.weekpaper.bean.LogContent;

import java.io.File;
import java.io.InputStream;
import java.util.*;

public class GitDealUtil {
    
    public static Map> createLogTxt(ConfigData cd){
        //2. 生成 git 执行命令行(windows,linux的类似,进入盘符的几个步骤有差别)
        String commandFilePath = createGitCommandFile(cd);
    
        //3. 执行 git 生成限定时间段日志
        boolean result = executeGitCommand(commandFilePath);
        //4. 读取抓到的日志内容
        Map originLogs = readGitLog(cd,result);
        //5. 生成日志对象
        return getListObjects(originLogs);
    }
    
    private static Map> getListObjects(Map logMsgs) {
        Map> logAns = new HashMap<>();
        for(Map.Entry item:logMsgs.entrySet()){
            String name = item.getKey();
            String content = item.getValue();
            String[] lines = content.split("n");
            //3.处理日志内容,抽象为对象
            List lcs = new ArrayList<>();
            LogContent lc = null;
            StringBuilder sb = new StringBuilder();
            boolean isMerge = false;
            for(String line:lines){
                if(line.startsWith("commit")){
                    if(lc!=null){
                        if(sb.length()>0){
                            sb.deleteCharAt(sb.length()-1);
                        }
                        lc.setContent(sb.toString());
                        sb.delete(0,sb.length());
                        if(!isMerge)
                            lcs.add(lc);
                        isMerge = false;
                    }
                    lc = new LogContent();
                    lc.setCommitVersion(line.substring("commit".length()+1).trim());
                }else if(line.startsWith("Author:")){
                    String authorAndEmail = line.substring("Author:".length()+1).trim();
                    lc.setAuthor(authorAndEmail.split(" ")[0]);
                    lc.setEmail(authorAndEmail.split(" ")[1]);
                    lc.setEmail(lc.getEmail().substring(1,lc.getEmail().length()-1));
                }else if(line.startsWith("Date:")){
                    lc.setDate(DateUtil.getUsDate(line.substring("Date:".length()+1).trim()));
                }else if(line.startsWith("Merge: ")){
                    isMerge = true;
                }else{
//                    commit 50ff270a2ba476db707df277d652t1234b732da8e
//                    Merge: 2677td2 1c873fe
//                    Author: xx 
//                    Date:   Wed Jun 1 13:55:26 2022 +0800
//
//                    Merge branch 'dev'
//                    Merge 合并分支格式的忽略
                    if(line.length()<"    ".length()) continue;
                    String str = line.substring("    ".length()).trim();
                    
                    if(str.length()>0){
                        sb.append(str);
                        sb.append("n");
                    }
                }
            }
            if(lc!=null){
                if(sb.length()>0){
                    sb.deleteCharAt(sb.length()-1);
                }
                lc.setContent(sb.toString());
                sb.delete(0,sb.length());
                if(!isMerge)
                    lcs.add(lc);
            }
            //由于时间倒序,所以需要反过来
            Collections.reverse(lcs);
            if(!lcs.isEmpty()){
                logAns.put(name,lcs);
            }
            
        }
        return logAns;
        
    }
    
    private static Map readGitLog(ConfigData cd, boolean result){
        if(cd == null || !result){
            LogUtil.severe("配置文件拿到空值或命令执行失败, 无法执行后续操作!");
            System.exit(0);
            return new LinkedHashMap<>();
            
        }
        
        Map map = new LinkedHashMap<>();
        int n = cd.getProjectPaths().size();
        for(int i = 0; i < n; i++) {
            String path = cd.getProjectPaths().get(i);
            File f = new File(path);
          
            if(f.exists() && f.isDirectory()){
                String fullPath = FileUtil.getTmpFilePath(cd.getOutputPath(),f.getName()+".txt");
                String msg = FileUtil.getInputMsg(fullPath);
                map.put(cd.getProjectNames().get(i),msg);
            }
        }
        
        
        return map;
    }
    
    private static boolean executeGitCommand(String path){
        if(path == null || path.length() == 0){
            LogUtil.severe("未生成可执行命令,无法进行后续流程!");
            System.exit(0);
            return false;
        }
        LogUtil.info("CMD /GIT命令开始执行...");
        Process p;
        boolean result = true;
        try {
            p = Runtime.getRuntime().exec(path);
            InputStream fis = p.getErrorStream();//p.getInputStream();
            String msg = FileUtil.getInputMsg(fis,false);
            p.waitFor();
            int i = p.exitValue();
            if (i != 0) {
                result = false;
                LogUtil.severe("CMD /GIT命令执行失败!请检查是否安装 git 环境,命令是否正确!提示信息为:"+msg);
            }else {
                LogUtil.info("CMD /GIT命令执行成功!");
            }
        } catch (Exception e) {
            result = false;
            LogUtil.severe("未知错误!"+e.getMessage());
        }
        return result;
    }
    
    
    private static String createGitCommandFile(ConfigData cd) {
        if(cd == null){
            LogUtil.severe("配置文件拿到空值,无法执行后续操作!");
            System.exit(0);
            return "";
        }
        LogUtil.info("开始生成"+DateUtil.toString(cd.getStartDate())+"到"+DateUtil.toString(cd.getEndDate())+"git 命令行,如下:");
//        盘符:
//        cd 【项目路径】
//        for /F %%i in ('git config --get user.name') do ( set name=%%i)
//        git log --author=%name% --since ==【开始时间】 --until=【结束时间】 > 【输出路径tmp】【项目名称英文】.txt
        
        if(cd.getProjectPaths()==null || cd.getProjectPaths().isEmpty()){
            LogUtil.severe("无可用项目!");
            System.exit(0);
        }
        
        int n = cd.getProjectPaths().size();
        if(cd.getProjectNames().size() commands = new ArrayList();
        for(int i = 0; i < n; i++){
            String path = cd.getProjectPaths().get(i);
            String name = cd.getProjectNames().get(i);
            LogUtil.info("开始创建项目"+name+"的 GIT 命令...");
            File f = new File(path);
            if(f.exists() && f.isDirectory()){
                //        盘符:[linux不用写这个]
                commands.add(path.split(":")[0]+":");
                //        cd 【项目路径】
                commands.add("cd "+path);
                //        for /F %%i in ('git config --get user.name') do ( set name=%%i)
                commands.add("for /F %%i in ('git config --get user.name') do ( set name=%%i)");
                //        git log --author=%name% --since ==【开始时间】 --until=【结束时间】 > D:programeclipseworkDESTconfig【项目名称英文】.txt
                
                File targetFilePath = FileUtil.reCreateFile(FileUtil.getTmpFilePath(cd.getOutputPath(),f.getName()+".txt"));
                //注意:不包含第一天,需要向前挪动1天
                commands.add("git log --author=%name% --since =="+DateUtil.getPreDayStr(cd.getStartDate())+" --until="+DateUtil.toString(cd.getEndDate())+" > "+
                        FileUtil.getShortPath(targetFilePath));
                LogUtil.info("创建项目"+name+"的 GIT 命令成功!");
            }else{
                LogUtil.info("创建项目"+name+"的 GIT 命令失败,找不到文件夹!");
            }
        }
        
        for(String command:commands){
            System.out.println(command);
        }
        
        if(!commands.isEmpty()){
            String commandPath = FileUtil.getTmpFilePath(cd.getOutputPath(),"cmd.bat");
            File commandFilePath = FileUtil.reCreateFile(commandPath);
            FileUtil.storeGitCommands(commands,FileUtil.getShortPath(commandFilePath));
            return commandPath;
        }
        return "";
        
    }
}

❤李癩❤李癩❤李癩

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号