TimeRecording

<pre name="code" class="java">package net.tqm.control;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import net.tqm.util.DBUtil;
import net.tqm.util.OrderedLinkedHashMap;

public class TimeRecording {
	static File file = new File("db/timeRecord");
	@SuppressWarnings("unchecked")
	private static final HashMap<String, String> histroyFromFile = (HashMap<String, String>) DBUtil
			.readFromFile(file);
	static HashMap<String, String> historyRecord = (histroyFromFile == null) ? new LinkedHashMap<String, String>()
			: histroyFromFile;

	public static void insertRecord(String ovetTimeDay, String leaveTime) {
		historyRecord.put(ovetTimeDay, leaveTime);
		historyRecord = OrderedLinkedHashMap.getOrder(historyRecord);
		DBUtil.writeToFile(historyRecord, file);
	}

	public static void queryHistoryRecord(JTextArea textArea) throws Exception {
		textArea.setText("");
		if (historyCheck())
			fetchDataTo(textArea);
	}

	private static boolean historyCheck() {
		if (historyRecord.isEmpty()) {
			JOptionPane.showMessageDialog(null, "没有历史数据!");
			return false;
		}
		return true;
	}

	private static void fetchDataTo(JTextArea textArea) {
		Iterator<String> dayIterator = historyRecord.keySet().iterator();
		while (dayIterator.hasNext()) {
			String key = dayIterator.next();
			data2TextArea(textArea, key);
		}
	}

	private static void fetchDataTo(File file) throws WriteException,
			RowsExceededException {

		WritableWorkbook book = createBook(file);
		WritableSheet sheet = createSheet(book);
		Iterator<String> dayIterator = historyRecord.keySet().iterator();
		int row = 0;
		while (dayIterator.hasNext()) {
			String key = dayIterator.next();
			data2ExcelObj(sheet, key, row++);
		}
		writeTo(book);
		JOptionPane.showMessageDialog(null, "导出成功!");
	}

	private static WritableSheet createSheet(WritableWorkbook book)
			throws WriteException, RowsExceededException {
		// 生成名为“第一页”的工作表,参数0表示这是第一页
		WritableSheet sheet = book.createSheet("第一页", 0);
		// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
		// 以及单元格内容为test
		Label label1 = new Label(0, 0, "day");
		Label label2 = new Label(1, 0, "leavetime");
		// 将定义好的单元格添加到工作表中
		sheet.addCell(label1);
		sheet.addCell(label2);
		return sheet;
	}

	private static WritableWorkbook createBook(File file) {
		WritableWorkbook book = null;
		try {
			book = Workbook.createWorkbook(file);
		} catch (IOException e1) {
			System.out.println("catch3");
			e1.printStackTrace();
		}
		return book;
	}

	private static void writeTo(WritableWorkbook book) {
		// 写入数据并关闭文件
		try {
			book.write();
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void data2TextArea(JTextArea textArea, String key) {
		textArea.append(key + " ==" + historyRecord.get(key) + "\n");
	}

	private static void data2ExcelObj(WritableSheet sheet, String key, int row) {
		Label label1;
		try {
			/*
			 * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为789.123
			 */
			label1 = new Label(0, ++row, key);
			sheet.addCell(label1);

			label1 = new Label(1, row, historyRecord.get(key));
			sheet.addCell(label1);

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static void timeAmount() {// TODO 统计overtime总时间,分月份统计,修改时间,删除时间等

	}

	public static void export2Excel() {
		File selectedFile = selectXLS();
		if (fileTypeCheck(selectedFile))
			try {
				fetchDataTo(selectedFile);
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	private static boolean fileTypeCheck(File selectedFile) {
		if (selectedFile == null || selectedFile.getName().equals(""))
			return false;
		String fileSuffix = selectedFile.getName().substring(
				selectedFile.getName().lastIndexOf(".") + 1);
		if (!fileSuffix.equals("xls")) {
			JOptionPane.showMessageDialog(null, "文件类型不对,请确认为xls类型!");
			return false;
		}
		return true;
	}

	private static File selectXLS() {
		JFileChooser jFileChooser = new JFileChooser();
		jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		jFileChooser.showOpenDialog(null);
		File selectedFile = jFileChooser.getSelectedFile();

		return selectedFile;
	}
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值