2019最新财务管理计算器源码

这是一个2019年的最新财务管理计算器,具备按月份统计和历史收入统计功能,支持将数据持久化存储到本地Excel。源码包括UI界面、Excel文件操作和目录、sheet管理等部分。

2019最新财务计算器源码

第一次写博客,不喜勿喷!
分享原码,逻辑并不严谨,仅供谈论使用!
预览效果:

在这里插入图片描述
1.可以根据月份统计,或者历史收入统计计算。
2.支持持久化到本地Excel:
2.1:Excel保存默认目录:C:\Wage\
2.2:新建按钮是在 C:\Wage\ 下新建一个excel ,比如 2019.8月工资统计.xls
2.3:保存时:左侧输入框有值时以左侧为准.右侧是从“选择档案”的excel文件里选择已有的人。
3.3:一个excel中每个人占用一个sheet。
3.保存文件示例:
在这里插入图片描述
在这里插入图片描述
源码:
1.ui界面源码(入口文件)

package swing;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import javax.swing.JLayeredPane;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Color;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTabbedPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.DropMode;
import java.awt.FlowLayout;
import java.awt.Canvas;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JPasswordField;
import java.awt.Panel;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import swing.utils.ExcelNames;
import swing.utils.SheelNames;
import swing.utils.excel;

import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JProgressBar;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
import javax.swing.ListSelectionModel;
import javax.swing.JSeparator;
import javax.swing.JTree;
import javax.swing.JScrollBar;
import java.awt.Choice;

import javax.swing.SwingConstants;
import javax.swing.JToggleButton;
import javax.swing.JRadioButton;

public class index {
	private Map <String,String> map;
	private JFrame frame;
	private JTextField textField_1;
	private JTextField textField;
	private JTable table;
	private JLabel label_5;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField textField_2;
	private Choice choice ;
	private ButtonGroup  bg1;
	private JRadioButton radioButton;
	private JRadioButton radioButton_1;
	private Choice choice_1;
	private JTextField textField_3;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					index window = new index();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace(); 
				}
			}
		});
	} 

	/**
	 * Create the application.
	 */
	public index() {
		initialize(); 
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		excel.createFile();
		 
		
		frame = new JFrame("北京市2019财务管理器");
		frame.setBounds(100, 100, 358, 449);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SpringLayout springLayout = new SpringLayout();
		frame.getContentPane().setLayout(springLayout);
		frame.setLocationRelativeTo(null);
		
		textField = new JTextField();
		textField.setHorizontalAlignment(SwingConstants.LEFT);
		springLayout.putConstraint(SpringLayout.SOUTH, textField, 31, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, textField, -128, SpringLayout.EAST, frame.getContentPane());
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel label = new JLabel("当月税前工资:");
		springLayout.putConstraint(SpringLayout.NORTH, label, 13, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, textField, -3, SpringLayout.NORTH, label);
		springLayout.putConstraint(SpringLayout.WEST, textField, 6, SpringLayout.EAST, label);
		springLayout.putConstraint(SpringLayout.WEST, label, 51, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(label);
		
		JButton button = new JButton("计算");
		springLayout.putConstraint(SpringLayout.NORTH, button, -1, SpringLayout.NORTH, textField);
		springLayout.putConstraint(SpringLayout.WEST, button, 6, SpringLayout.EAST, textField);
		frame.getContentPane().add(button);
		//按钮触发时间方法
		button.addActionListener(new ActionListener() {
			   
            // 当按钮被点击时,就会触发 ActionEvent事件
            // actionPerformed 方法就会被执行
            public void actionPerformed(ActionEvent e) {
            	double x2 = 0;
            	map=new HashMap<String,String>();
            	if(textField.getText().equals("")||textField.getText()==null) {
            		JOptionPane.showMessageDialog(null, "请输入当月工资"); 
            	}else {
            	int i=Integer.parseInt(textField.getText());
            	map.put("dangyuegongzi",i+"");
            	//养老
            	double bx2=i*0.2;
            	double bx=i*0.08;
            	map.put("yanglao1",bx+"");
            	map.put("yanglao2",bx2+""); 
            	//医疗
            	double yl=(i*0.02)+3;
            	double yl2=i*0.1;
            	map.put("yiliao2",yl2+""); 
            	map.put("yiliao",yl+""); 
            	//失业保险
            	double sy=i*0.002;
            	double sy2=i*0.01;
            	map.put("shiye",sy+""); 
            	map.put("shiye2",sy2+""); 
            	//工伤
            	double gs2=i*0.0048;
            	map.put("gongshang",gs2+""); 
            	//生育
            	double syb2=i*0.008;
            	map.put("shengyu",syb2+""); 
            	//住房
            	double zf=i*0.12;
            	double zf2=i*0.12;
            	map.put("zhufang",zf+"");
            	map.put("zhufang2",zf2+""); 
            	//纳税
            	double ns=0;
            	//纳税部分
            	double x=i-bx-yl-sy-zf-5000; 
            	
            	//获取计算期数
            	int qs=Integer.parseInt(choice.getSelectedItem());
            	//获取工资月份
            	int moth=Integer.parseInt(choice_1.getSelectedItem());
            	map.put("qishu", qs+"");
            	map.put("moth", moth+"");
            	
            	if(qs==1){
	            	ns=index.ns(x);
	            	if(x>0) {
	            		map.put("num", x+"");
	            	}else {
	            		map.put("num", "0");
	            	}
	            	
            	}else{
            		
            		//当月应纳税总额 
            		x=i*qs-5000*qs-(bx+yl+sy+zf)*qs;
            		if(x>0) {
	            		map.put("num", x+"");
	            	}else {
	            		map.put("num", "0");
	            	}
            		//获取单选按钮状态
                	if(radioButton.isSelected()) {
                		//上月应纳税总额
                		x2=x-(i-5000-(bx+yl+sy+zf));
                		//获取本月应缴纳税额
                		ns=index.ns(x)-index.ns(x2);
                	}else if(radioButton_1.isSelected()) {
                		//上月应纳税总额
                		x2=Integer.parseInt(textField_2.getText());
                	
                		//获取本月应缴纳税额
                		ns=index.ns(i-bx-yl-sy-zf-5000+x2)-index.ns(x2);
                	}
            	} 
            	map.put("nashui",ns+"");
        		table.setModel(new DefaultTableModel(
    			new Object[][] {
    				{"\u540D\u5B57", "个人扣额", "公司扣额"},
    				{"养老保险", bx, bx2},
    				{"医疗保险",yl, yl2},
    				{"失业保险", sy, sy2},
    				{"工伤保险", 0, gs2},
    				{"生育保险", 0, syb2},
    				{"住房公积金", zf, zf2},
    				{"纳税", ns, 0},
    				
    			},
    			new String[] {
    				"\u540D\u79F0", "\u989D\u5EA6", "\u516C\u53F8"
    			}
    		) {
    			Class[] columnTypes = new Class[] {
    				String.class, Object.class, Object.class
    			};
    			public Class getColumnClass(int columnIndex) {
    				return columnTypes[columnIndex];
    			}
    		});
    		table.getColumnModel().getColumn(0).setMinWidth(40);
            	
    		label_5.setText((int)(bx+yl+sy+zf)+"");;
    		lblNewLabel.setText((int)(bx2+yl2+sy2+gs2+syb2+zf2)+"");;
    		lblNewLabel_1.setText((int)(ns)+"");
    		lblNewLabel_2.setText((int)(i-(bx+yl+sy+zf)-ns)+"");
    		map.put("shuihou", (int)(i-(bx+yl+sy+zf)-ns)+"");
    		
            }     
            }
        });
		
		
		
		
		table = new JTable();
		springLayout.putConstraint(SpringLayout.WEST, table, 28, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, table, -31, SpringLayout.EAST, frame.getContentPane());
		table.setToolTipText("");
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		DefaultTableCellRenderer   r   =   new   DefaultTableCellRenderer();   
		r.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,r);

		frame.getContentPane().add(table);
		
		JLabel label_1 = new JLabel("税后工资:");
		springLayout.putConstraint(SpringLayout.WEST, label_1, 0, SpringLayout.WEST, button);
		frame.getContentPane().add(label_1);
		
		JLabel label_2 = new JLabel("个人五险:");
		frame.getContentPane().add(label_2);
		
		JLabel label_3 = new JLabel("公司五险:");
		springLayout.putConstraint(SpringLayout.WEST, label_3, 0, SpringLayout.WEST, button);
		springLayout.putConstraint(SpringLayout.SOUTH, label_3, -72, SpringLayout.SOUTH, frame.getContentPane());
		frame.getContentPane().add(label_3);
		
		JLabel label_4 = new JLabel("纳税:");
		springLayout.putConstraint(SpringLayout.NORTH, label_1, 6, SpringLayout.SOUTH, label_4);
		springLayout.putConstraint(SpringLayout.NORTH, label_4, 6, SpringLayout.SOUTH, label_3);
		springLayout.putConstraint(SpringLayout.WEST, label_4, 0, SpringLayout.WEST, button);
		frame.getContentPane().add(label_4);
		
		label_5 = new JLabel("待计算--");
		springLayout.putConstraint(SpringLayout.NORTH, label_2, 0, SpringLayout.NORTH, label_5);
		springLayout.putConstraint(SpringLayout.EAST, label_2, -6, SpringLayout.WEST, label_5);
		springLayout.putConstraint(SpringLayout.WEST, label_5, 294, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, label_5, -10, SpringLayout.EAST, frame.getContentPane());
		label_5.setHorizontalAlignment(SwingConstants.RIGHT);
		frame.getContentPane().add(label_5);
		
		lblNewLabel = new JLabel("待计算--");
		springLayout.putConstraint(SpringLayout.SOUTH, label_5, -6, SpringLayout.NORTH, lblNewLabel);
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 0, SpringLayout.NORTH, label_3); 
		springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 0, SpringLayout.EAST, label_5);
		frame.getContentPane().add(lblNewLabel);
		
		lblNewLabel_1 = new JLabel("待计算--");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 0, SpringLayout.NORTH, label_4);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_1, 0, SpringLayout.WEST, label_5);
		frame.getContentPane().add(lblNewLabel_1);
		
		lblNewLabel_2 = new JLabel("待计算--");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel_2, 0, SpringLayout.NORTH, label_1);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_2, 6, SpringLayout.EAST, label_1);
		frame.getContentPane().add(lblNewLabel_2);
		
		JLabel label_6 = new JLabel("期数:");
		springLayout.putConstraint(SpringLayout.NORTH, label_6, 14, SpringLayout.SOUTH, textField);
		springLayout.putConstraint(SpringLayout.WEST, label_6, 0, SpringLayout.WEST, label);
		frame.getContentPane().add(label_6);
		
		choice = new Choice();
		springLayout.putConstraint(SpringLayout.NORTH, choice, 12, SpringLayout.SOUTH, textField);
		springLayout.putConstraint(SpringLayout.WEST, choice, 3, SpringLayout.EAST, label_6);
		springLayout.putConstraint(SpringLayout.EAST, choice, 76, SpringLayout.EAST, label_6);
		choice.add("1");
		choice.add("2");
		choice.add("3");
		choice.add("4");
		choice.add("5");
		choice.add("6");
		choice.add("7");
		choice.add("8");
		choice.add("9");
		choice.add("10");
		choice.add("11");
		choice.add("12");
		frame.getContentPane().add(choice);
		
		JLabel label_7 = new JLabel("历史累计收入:");
		springLayout.putConstraint(SpringLayout.NORTH, label_7, 16, SpringLayout.SOUTH, choice);
		springLayout.putConstraint(SpringLayout.WEST, label_7, 0, SpringLayout.WEST, label);
		frame.getContentPane().add(label_7);
		
		textField_2 = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, textField_2, -3, SpringLayout.NORTH, label_7);
		springLayout.putConstraint(SpringLayout.WEST, textField_2, 4, SpringLayout.EAST, label_7);
		springLayout.putConstraint(SpringLayout.EAST, textField_2, -95, SpringLayout.EAST, frame.getContentPane());
		textField_2.setHorizontalAlignment(SwingConstants.LEFT);
		textField_2.setText("0");
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		radioButton = new JRadioButton("根据月份");
		springLayout.putConstraint(SpringLayout.NORTH, table, 12, SpringLayout.SOUTH, radioButton);
		radioButton.setVerticalAlignment(SwingConstants.BOTTOM);
		springLayout.putConstraint(SpringLayout.SOUTH, radioButton, -279, SpringLayout.SOUTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, radioButton, 0, SpringLayout.WEST, label);
		frame.getContentPane().add(radioButton);
		radioButton.setSelected(true);
		
		radioButton_1 = new JRadioButton("根据历史收入");
		springLayout.putConstraint(SpringLayout.NORTH, radioButton_1, 0, SpringLayout.NORTH, radioButton);
		springLayout.putConstraint(SpringLayout.WEST, radioButton_1, 31, SpringLayout.EAST, radioButton);
		frame.getContentPane().add(radioButton_1);
	
		bg1=new ButtonGroup();
		bg1.add(radioButton); 
		bg1.add(radioButton_1);
		
		JLabel lblexcel = new JLabel("保存到excel:");
		springLayout.putConstraint(SpringLayout.NORTH, lblexcel, 389, SpringLayout.NORTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, lblexcel, 10, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(lblexcel);
		
		JButton button_1 = new JButton("保存");
		springLayout.putConstraint(SpringLayout.WEST, button_1, 0, SpringLayout.WEST, choice);
		frame.getContentPane().add(button_1);
		
		choice_1 = new Choice();
		springLayout.putConstraint(SpringLayout.SOUTH, choice_1, 0, SpringLayout.SOUTH, choice);
		springLayout.putConstraint(SpringLayout.EAST, choice_1, -47, SpringLayout.EAST, frame.getContentPane());
		choice_1.add("1");
		choice_1.add("2");
		choice_1.add("3");
		choice_1.add("4");
		choice_1.add("5");
		choice_1.add("6");
		choice_1.add("7");
		choice_1.add("8");
		choice_1.add("9");
		choice_1.add("10");
		choice_1.add("11");
		choice_1.add("12");
		frame.getContentPane().add(choice_1);
		
		JLabel label_8 = new JLabel("工资月份:");
		springLayout.putConstraint(SpringLayout.WEST, choice_1, 6, SpringLayout.EAST, label_8);
		springLayout.putConstraint(SpringLayout.NORTH, label_8, 0, SpringLayout.NORTH, label_6);
		springLayout.putConstraint(SpringLayout.WEST, label_8, 6, SpringLayout.EAST, choice);
		frame.getContentPane().add(label_8);
		
		textField_3 = new JTextField();
		springLayout.putConstraint(SpringLayout.SOUTH, textField_3, -42, SpringLayout.SOUTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, button_1, 6, SpringLayout.SOUTH, textField_3);
		springLayout.putConstraint(SpringLayout.WEST, textField_3, 4, SpringLayout.WEST, lblexcel);
		frame.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		Choice choice_2 = new Choice();
		springLayout.putConstraint(SpringLayout.WEST, choice_2, 108, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, choice_2, -6, SpringLayout.NORTH, button_1);
		springLayout.putConstraint(SpringLayout.EAST, choice_2, -9, SpringLayout.WEST, label_4);
		springLayout.putConstraint(SpringLayout.EAST, textField_3, -6, SpringLayout.WEST, choice_2);
		
		frame.getContentPane().add(choice_2);
		
		JLabel label_9 = new JLabel("请输入或选择名称:");
		springLayout.putConstraint(SpringLayout.NORTH, choice_2, 7, SpringLayout.SOUTH, label_9);
		springLayout.putConstraint(SpringLayout.NORTH, textField_3, 7, SpringLayout.SOUTH, label_9);
		springLayout.putConstraint(SpringLayout.WEST, label_9, 10, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(label_9);
		
		Label label_10 = new Label("选择档案:");
		springLayout.putConstraint(SpringLayout.SOUTH, label_10, -93, SpringLayout.SOUTH, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.NORTH, label_9, 8, SpringLayout.SOUTH, label_10);
		springLayout.putConstraint(SpringLayout.SOUTH, table, -11, SpringLayout.NORTH, label_10);
		springLayout.putConstraint(SpringLayout.WEST, label_10, 10, SpringLayout.WEST, frame.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, label_10, 67, SpringLayout.WEST, frame.getContentPane());
		frame.getContentPane().add(label_10);
		
		Choice choice_3 = new Choice();
		springLayout.putConstraint(SpringLayout.WEST, label_2, 3, SpringLayout.EAST, choice_3);
		springLayout.putConstraint(SpringLayout.EAST, choice_3, 0, SpringLayout.EAST, textField);
		springLayout.putConstraint(SpringLayout.WEST, choice_3, 6, SpringLayout.EAST, label_10);
		springLayout.putConstraint(SpringLayout.SOUTH, choice_3, -7, SpringLayout.NORTH, label_9);
		ExcelNames.en(choice_3); 
		frame.getContentPane().add(choice_3);
		choice_3.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				String excelName=choice_3.getSelectedItem();
				SheelNames.en(choice_2, excelName); 
			}
		});
		
		JButton button_2 = new JButton("新建");
		springLayout.putConstraint(SpringLayout.NORTH, button_2, 6, SpringLayout.SOUTH, choice_2);
		springLayout.putConstraint(SpringLayout.WEST, button_2, 3, SpringLayout.EAST, button_1);
		frame.getContentPane().add(button_2);
		
		
		
		//新建excel方法
		button_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String inputValue = JOptionPane.showInputDialog("请输入文件名称");
				String excel3=choice_3.getSelectedItem();
				try {
					if(inputValue!=null&&!inputValue.equals("")) {
						
						if(excel3==null) {
							excel3="";
						}
						excel.createExcel(inputValue);
						ExcelNames.en(choice_3);
					}
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "文件创建失败"); 
					e1.printStackTrace();
				}
				
			}
		});
		
		
		//保存excel方法
		button_1.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
			String nshui=lblNewLabel_1.getText();
			String excelpath=choice_3.getSelectedItem();
			String c2=choice_2.getSelectedItem();
			String c21=textField_3.getText();
			
			if(nshui.equals("待计算--")) {
				JOptionPane.showMessageDialog(null, "请先计算记过后保存。"); 
			}else if(excelpath==null) {
				JOptionPane.showMessageDialog(null, "请创建档案excel后在保存"); 
			}else if(c2==null&&c21==null) {
				JOptionPane.showMessageDialog(null, "请选择保存的sheet名称"); 
			}else {
				String name=textField_3.getText();
				String filename=null;
				if(name==null||name.equals("")) {
					name=choice_2.getSelectedItem();
					filename=choice_3.getSelectedItem();
					
				}else {
					filename=choice_3.getSelectedItem();
					
				}
				try {
					excel.write(name,filename,map);
					String excelName=choice_3.getSelectedItem();
					SheelNames.en(choice_2, excelName); 
					JOptionPane.showMessageDialog(null, "保存成功"); 
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "保存失败"); 
					e1.printStackTrace();
				}
				}
			}
		});
		
	}
	
	//根据应缴纳总金税额计算税额
	public static double ns(double x) {
	 double ns=0;	
	 
	 	if(x>0&&x<=36000) {
	 		ns=x*0.03;
 		}else if (x>36000&&x<=144000) {
 		ns=x*0.1-2520;
		}else if (x>144000&&x<300000) {
			ns=x*0.2-16920;
		}else if (x>300000&&x<=420000) {
			ns=x*0.25-31920;
		}else if (x>420000&&x<=660000) {
			ns=x*0.3-52920;
		}else if (x>660000&&x<=960000) {
			ns=x*0.35-85920;
		}else if (x>960000) {
			ns=x*0.45-181920;
		}
		
		return ns;
	}
}

2.excel文件操作:

package swing.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class excel {
	private static List<String> lis=new ArrayList<String>();
	
	static String ExlFilePath="C:\\Wage\\";
/**
	* 数据填充excel表格
	 * @param sheelname
	 * @param map
	 * @throws IOException
	 */
//String sheetName,Map<String,String> map
public static void write(String sheelname,String filename,Map<String,String> map) throws IOException {
		
		FileInputStream fis=new FileInputStream("C:\\Wage\\"+filename);
		
		HSSFWorkbook hw=new HSSFWorkbook(fis);
		
		fis.close();
		//***
		HSSFSheet hs = null;
		
		List <String> lis=getSheetName(filename);
		
		for(String ls:lis) {
			if(ls.equals(sheelname)) {
				hs=hw.getSheet(sheelname);
				break;
			}
		}
		if(hs==null) {
			
			hs=hw.createSheet(sheelname);
			HSSFRow row0=hs.createRow(0);
			
			row0.createCell(0).setCellValue("名字");
			row0.createCell(1).setCellValue("工资月份");
			row0.createCell(2).setCellValue("期数");
			row0.createCell(3).setCellValue("税前工资");
			row0.createCell(4).setCellValue("生育保险");
			row0.createCell(5).setCellValue("养老保险(个人)");
			row0.createCell(6).setCellValue("养老保险(公司)");
			row0.createCell(7).setCellValue("失业保险(个人)");
			row0.createCell(8).setCellValue("失业保险(公司)");
			row0.createCell(9).setCellValue("工伤保险(公司)");
			row0.createCell(10).setCellValue("医疗保险(个人)");
			row0.createCell(11).setCellValue("医疗保险(公司)");
			row0.createCell(12).setCellValue("住房公积金(个人)");
			row0.createCell(13).setCellValue("住房公积金(公司)");
			row0.createCell(14).setCellValue("交税");
			row0.createCell(15).setCellValue("年度以预工资纳税总额");
			row0.createCell(16).setCellValue("税后工资");
		}
		
		
		int i=hs.getLastRowNum()+1;
		
		Row row =hs.createRow(i);
		
		
			row.createCell(0).setCellValue(sheelname);
			row.createCell(1).setCellValue(map.get("moth"));
			row.createCell(2).setCellValue(map.get("qishu"));
			row.createCell(3).setCellValue(map.get("dangyuegongzi"));
			row.createCell(4).setCellValue(map.get("shengyu"));
			row.createCell(5).setCellValue(map.get("yanglao1"));
			row.createCell(6).setCellValue(map.get("yanglao2"));
			row.createCell(7).setCellValue(map.get("shiye"));
			row.createCell(8).setCellValue(map.get("shiye2"));
			row.createCell(9).setCellValue(map.get("gongshang"));
			row.createCell(10).setCellValue(map.get("yiliao"));
			row.createCell(11).setCellValue(map.get("yiliao2"));
			row.createCell(12).setCellValue(map.get("zhufang"));
			row.createCell(13).setCellValue(map.get("zhufang2"));
			row.createCell(14).setCellValue(map.get("nashui"));
			row.createCell(15).setCellValue(map.get("num"));
			row.createCell(16).setCellValue(map.get("shuihou"));
			
		FileOutputStream fos =new FileOutputStream(ExlFilePath+filename);
		
		hw.write(fos);
		
		fos.flush();
		fos.close();
		
		
		
		 
	}

//返回sheel列表
public static List getSheetName(String name) {
	List <String>list=new ArrayList<String>();
	try {
		FileInputStream fis=new FileInputStream(ExlFilePath+name);
		
		HSSFWorkbook xw=new HSSFWorkbook(fis);
		
		for(int i=0;i<xw.getNumberOfSheets();i++) {
			String sheetname=xw.getSheetAt(i).getSheetName();
			list.add(sheetname);
			
		}
	
		
	} catch (IOException e) {
		// TODO 自动生成的 catch 块
		e.printStackTrace();
	}
	
	
	return list;
}

//启动程序时创建程序文件夹
public static void createFile() {
	File file=new File(ExlFilePath);
	if(!file.exists()) {
		
		file.mkdirs();
	}
	
}

//获取所有资源excel
public static List<String>getExcelName(String filePath){
	
	File file=new File(filePath);
	 
	File[] fs=file.listFiles();
	
	for(File fi:fs) {
		if(fi.isDirectory()) {
			getExcelName(fi.getAbsolutePath());
		}else {
			if(fi.getName().split("\\.")[fi.getName().split("\\.").length-1].equals("xls")) {
				lis.add(fi.getPath().replace(ExlFilePath,""));
				
			}
			
		}
	}

	return lis;
}

//创建excel文件
public static void createExcel(String filepath) throws IOException {  
	String path="";
	File fi; 

	//根据斜杠判断创建的是否是根目录文件  
	if(filepath.indexOf("\\")!=-1) {
		
		String [] files= filepath.split("\\\\");
		for(int i=0;i<files.length-1;i++) {
			path=path+files[i]+"\\";
			
			
		}
		fi=new File(ExlFilePath+filepath+".xls");
		
	}else {
		fi=new File(ExlFilePath+filepath+".xls");
	}
	

	
	
	if(!fi.exists()) {
		   String sb="";
			HSSFWorkbook hw=new HSSFWorkbook();  
			
			if(path!="") {
				sb=path.substring(0,path.length()-1);
				File fi2=new File(ExlFilePath+sb);
				fi2.mkdirs();
			}
			
			FileOutputStream fos=new FileOutputStream(fi);
			hw.write(fos);
			fos.close();
			
//			fi.createNewFile();
			
		
	}
	
}

}

3.获取目录下所有excel:

package swing.utils;

import java.awt.Choice;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class ExcelNames {
	
	/**
	 * 渲染excel表下拉列表
	 * @param ch
	 */
	public static void en(Choice ch) {
		String path = new excel().ExlFilePath;
		List <String> excelNames=new ArrayList<>();
		excelNames=excel.getExcelName(path);
		
		ch.removeAll();
		for(String li:removeDuplicate(excelNames)) {
			ch.add(li); 
		}
	 
		
	} 
	//list去重
	public static List<String> removeDuplicate(List list) {   
	    HashSet h = new HashSet(list);   
	    list.clear();   
	    list.addAll(h);   
	    
	    return list;   
	}   

}

4.获取excel中的sheet:

package swing.utils;

import java.awt.Choice;
import java.util.ArrayList;
import java.util.List;

public class SheelNames {
	/**
	 * 渲染sheelname表下拉列表
	 * @param ch
	 */
	public static void en(Choice ch,String name) {
		List <String> SheetNames=new ArrayList<>();
		SheetNames=excel.getSheetName(name);
		ch.removeAll();
		for(String li:SheetNames) { 
			ch.add(li); 
		}
	
		
	} 


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值