tmp

package com.bfs.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExecutionMain extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 4757702907638719673L;

    //
    JTextField userTextTimeS;
    JTextField userTextTimeE;
    JTextField userTextJKousu;
    JTextField userTextSNaiyou;
    JTextField userTextBikou;

    //获取当前日期
    public static final Calendar RIGHTNOW = Calendar.getInstance();
    
    //当月sheet名
    public String sheetName;
    //当前日期
    public int day;
    public static final String SHEET_ = "月次報告_";
    public static final int ROW_ = 7;
    
    /** 工数 */
    public String str_yKousu;
    
    /** 月份转换用 */
    public final static HashMap<Integer, String> HASHMAP = new HashMap<Integer, String>();
    static {
        HASHMAP.put(1, "一月");
        HASHMAP.put(2, "二月");
        HASHMAP.put(3, "三月");
        HASHMAP.put(4, "四月");
        HASHMAP.put(5, "五月");
        HASHMAP.put(6, "六月");
        HASHMAP.put(7, "七月");
        HASHMAP.put(8, "八月");
        HASHMAP.put(9, "九月");
        HASHMAP.put(10, "十月");
        HASHMAP.put(11, "十一月");
        HASHMAP.put(12, "十二月");
    }
    
    /** 星期转换用 */
    public final static HashMap<Integer, String> SUNHASHMAP = new HashMap<Integer, String>();
    static {
        SUNHASHMAP.put(1, "周日");
        SUNHASHMAP.put(2, "周一");
        SUNHASHMAP.put(3, "周二");
        SUNHASHMAP.put(4, "周三");
        SUNHASHMAP.put(5, "周四");
        SUNHASHMAP.put(6, "周五");
        SUNHASHMAP.put(7, "周六");
    }

    XSSFWorkbook xssfWorkbook = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sunFm = new SimpleDateFormat("EEEE");

    public static void main(String args[]) {
        new ExecutionMain();
    }

    ExecutionMain() {

        // 高度设置用
        int hight = 0;

        // 外观风格
        JFrame.setDefaultLookAndFeelDecorated(true);

        // 创建及设置窗口
        JFrame frame = new JFrame("作业实际管理填写工具");
        frame.setLayout(null);
        frame.setSize(550, 200);
        // 允许用户自定义大小
        frame.setResizable(false);
        // 窗口初始化出现在屏幕中央
        frame.setLocationRelativeTo(null);
        // 点击x时动作为结束APP
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //获取当前月份
        int month = RIGHTNOW.get(Calendar.MONTH) + 1;
        day = RIGHTNOW.get(Calendar.DAY_OF_MONTH);
        int sun = RIGHTNOW.get(Calendar.DAY_OF_WEEK);
        /// 添加 “今天日期”
        JLabel toDay = new JLabel("今天是:"+month+""+day+"" + SUNHASHMAP.get(sun));
        toDay.setBounds(10, hight, 200, 20);
        frame.add(toDay);
        hight += 20;

        try {
            InputStream is = new FileInputStream("H:\\個人管理_障がい者\\000_管理\\100_作業実績\\作業実績管理シート_危紫航.xlsx");
            xssfWorkbook = new XSSFWorkbook(is);

            //获取当前月份sheet名        
            sheetName = SHEET_ + HASHMAP.get(month);
            //获取当前月份sheet
            Sheet sheet = xssfWorkbook.getSheet(sheetName);
            
            /// 添加 “マスク”
            Cell mskCell = sheet.getRow(ROW_+day).getCell(6);
            JLabel msk = new JLabel("マスク:"+ mskCell.toString());
            msk.setBounds(10, hight, 200, 20);
            frame.add(msk);
            
            /// 获取 “予想工数”
            Cell ysCell = sheet.getRow(ROW_+day).getCell(11);
            str_yKousu = ysCell.toString();
            
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "文件读取失败。");
            e.printStackTrace();
        }finally {
            if(xssfWorkbook != null) {
                try {
                    xssfWorkbook.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        hight += 20;
        //// 添加 “开始时间”
        JLabel timeS = new JLabel("开始时间");
        timeS.setBounds(10, hight, 65, 20);
        frame.add(timeS);
        // 添加 “开始时间” 输入
        userTextTimeS = new JTextField(10);
        userTextTimeS.setBounds(65, hight + 1, 65, 20);
        frame.add(userTextTimeS);
        
        //// 添加 “终了时间”
        JLabel timeE = new JLabel("终了时间");
        timeE.setBounds(130, hight, 65, 20);
        frame.add(timeE);
        // 添加 “终了时间” 输入
        userTextTimeE = new JTextField(10);
        userTextTimeE.setBounds(185, hight + 1, 65, 20);
        frame.add(userTextTimeE);

        //// 添加 “予想工数”
        JLabel yKousu = new JLabel("予想工数:"+ str_yKousu);
        yKousu.setBounds(260, hight + 1, 90, 20);
        frame.add(yKousu);
        
        //// 添加 “实际工数”
        JLabel jKousu = new JLabel("实际工数");
        jKousu.setBounds(350, hight, 65, 20);
        frame.add(jKousu);
        // 添加 “实际工数” 输入
        userTextJKousu = new JTextField(10);
        userTextJKousu.setBounds(415, hight + 1, 30, 20);
        frame.add(userTextJKousu);

        
        hight += 25;
        //// 添加 “作业概要”
        JLabel sagyouNaiyou = new JLabel("作业概要");
        sagyouNaiyou.setBounds(10, hight, 65, 20);
        frame.add(sagyouNaiyou);
        // 添加 “作业概要” 输入
        userTextSNaiyou = new JTextField(10);
        userTextSNaiyou.setBounds(65, hight + 1, 450, 20);
        frame.add(userTextSNaiyou);

        
        hight += 25;
        /// 添加 “備考”
        JLabel bikouNaiyou = new JLabel("備考");
        bikouNaiyou.setBounds(10, hight, 65, 20);
        frame.add(bikouNaiyou);
        // 添加 “備考” 输入
        userTextBikou = new JTextField(10);
        userTextBikou.setBounds(65, hight + 1, 300, 20);
        frame.add(userTextBikou);
        
        hight += 30;

        // 添加提交按钮
        JButton button = new JButton("提交");
        button.setBounds(10, hight, 60, 25);
        frame.add(button);
        button.addActionListener(this);

        // 显示窗口
        frame.setVisible(true);
    }

    /**
     * 点击提交时进行的动作
     */
    @Override
    public void actionPerformed(ActionEvent args) {
        try {
            OutputStream out = new FileOutputStream("H:\\個人管理_障がい者\\000_管理\\100_作業実績\\作業実績管理シート_危紫航.xlsx");
//            InputStream  is = new FileInputStream("H:\\個人管理_障がい者\\000_管理\\100_作業実績\\作業実績管理シート_危紫航.xlsx");
//            xssfWorkbook = new XSSFWorkbook("H:\\個人管理_障がい者\\000_管理\\100_作業実績\\作業実績管理シート_危紫航.xlsx");
            
            //创建当前月份sheet
            XSSFWorkbook XSSFbook = null;
            XSSFbook = new XSSFWorkbook(out);
            
            XSSFSheet sheet = null;
            sheet = XSSFbook.getSheet(sheetName);
            
            
            /// 设置 “实际工数”
            sheet.getRow(ROW_+day).getCell(12).setCellValue("111");
//            XSSFbook.write(out);
//            out.flush();
//            out.close();
            XSSFbook.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "文件读取失败。");
            e.printStackTrace();
            return;
        }finally {
            if(xssfWorkbook != null) {
                try {
                    xssfWorkbook.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
//
//        return;
//        // 按钮名取得
//        String actionCommand = args.getActionCommand();
//
//        if (actionCommand == "提交") {
//            // 获取输入的开始时间
//            String _userTextTimeS = userTextTimeS.getText().replaceAll("(?::|:)", "");
//            // 获取输入的结束时间
//            String _userTextTimeE = userTextTimeE.getText().replaceAll("(?::|:)", "");
//            // 时间check
//            // 开始时间
//            boolean flgS = timeCheck(_userTextTimeS);
//            if (!flgS) {
//                JOptionPane.showMessageDialog(this, "开始时间格式不正确,请重新输入。");
//                return;
//            }
//            // 终了时间
//            boolean flgE = timeCheck(_userTextTimeE);
//            if (!flgE) {
//                JOptionPane.showMessageDialog(this, "终了时间格式不正确,请重新输入。");
//                return;
//            }
//        }

    }

    /**
     * 时间验证用
     */
    public static boolean timeCheck(String time) {
        // 非空判断
        if (time.isEmpty() || time.length() != 4) {
            return false;
        }
        // 转换异常时处理
        try {
            Integer.parseInt(time);
        } catch (NumberFormatException n) {
            return false;
        }
        // 获取输入的时间
        // 获得时间小时部分
        String Tmhh = time.substring(0, 2);
        // 获得时间分钟部分
        String Tmmm = time.substring(2, 4);

        // 正则表达式
        String sPatternTmhh = "^[0][0-9]$|^[1][0-9]$|^[2][0-3]$";
        String sPatternTmmm = "^[0][0-9]$|^[1-5][0-9]$";

        Boolean flgH = Pattern.matches(sPatternTmhh, Tmhh);
        Boolean flgM = Pattern.matches(sPatternTmmm, Tmmm);
        if (flgH && flgM) {
            return true;
        } else {
            return false;
        }
    }

}

 

posted @ 2021-06-03 17:32  小摔哥#1  阅读(162)  评论(1编辑  收藏  举报