使用jxl读取excel表格中的数据(包括日期数据),并追加到另外一个表格里

package com.dlj;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ConvertFile {
	public static void main(String[] args)
			throws IOException, RowsExceededException, WriteException, BiffException, IndexOutOfBoundsException,
			ParseException {
		File convertFile = new File("D:\\convert.xls");
		WritableWorkbook workbook = Workbook.createWorkbook(convertFile);
		workbook.createSheet("First Sheet", 0);
		workbook.write();
		workbook.close();
		// 要读取的excel文件的路径
		File files = new File("D:\\excelfile");
		judgeFile(files);

	}

	// 查找文件
	public static void judgeFile(File f)
			throws RowsExceededException, BiffException, WriteException, IndexOutOfBoundsException, IOException,
			ParseException {

		if (f.isDirectory()) {
			System.out.println("directory:" + f.getName());
			File[] files = f.listFiles();
			for (File file : files) {
				judgeFile(file);
			}
		} else {
			System.out.println("read filename:" + f.getName());
			convertFile(f);

		}
	}

	// 读取文件内容,并写到excel里面
	public static void convertFile(File f)
			throws BiffException, IOException, RowsExceededException, WriteException, ParseException {
		// 1 读取文件
		Workbook rwb = Workbook.getWorkbook(f);
		// 获取文件的sheet
		Sheet rs = rwb.getSheet(1);
		// 或者rwb.getSheet(0)
		// 获取数据
		// Label LceWen=new Label(4,2,);
		String cewen = rs.getCell(4, 2).getContents();
		String zuankongzong = rs.getCell(11, 2).getContents();
		String gongchengmingcheng = rs.getCell(3, 3).getContents();
		String zuobiaoN = rs.getCell(11, 3).getContents();
		String zuankongbian = rs.getCell(3, 4).getContents();
		String licheng = rs.getCell(7, 4).getContents();
		String zuobiaoE = rs.getCell(11, 4).getContents();
		String yibiaobianhao = rs.getCell(3, 5).getContents();
		String kongshen = rs.getCell(6, 5).getContents();
		String zuankongriqi = convertDate(rs.getCell(11, 5).getContents());
		String kongkougaocheng = rs.getCell(3, 6).getContents();
		String ceshen = rs.getCell(6, 6).getContents();
		String cewenriqi = convertDate(rs.getCell(11, 6).getContents());
		// 插入数据,前面的坐标是列,后面的坐标是行
		// 先获取要插入的excel表格的行数
		File convertFile = new File("D:\\convert.xls");
		Workbook work = Workbook.getWorkbook(convertFile);
		// 获取行
		int rows = work.getSheet(0).getRows();
		WritableWorkbook workbook = Workbook.createWorkbook(convertFile, work); //必须要这样写,否则每次都是创建一个新的sheet,无法实现追加数据
		WritableSheet sheet = workbook.getSheet(0);
		System.out.println("rows:" + rows);
		sheet.addCell(new Label(0, rows, cewen));
		sheet.addCell(new Label(1, rows, zuankongzong));
		sheet.addCell(new Label(2, rows, gongchengmingcheng));
		sheet.addCell(new Label(3, rows, zuobiaoN));
		sheet.addCell(new Label(4, rows, zuankongbian));
		sheet.addCell(new Label(5, rows, licheng));

		sheet.addCell(new Label(6, rows, zuobiaoE));
		sheet.addCell(new Label(7, rows, yibiaobianhao));
		sheet.addCell(new Label(8, rows, kongshen));

		sheet.addCell(new Label(9, rows, zuankongriqi));
		sheet.addCell(new Label(10, rows, kongkougaocheng));
		sheet.addCell(new Label(11, rows, ceshen));

		sheet.addCell(new Label(12, rows, cewenriqi));
		workbook.write();
		workbook.close();

	}

	// 转换日期
	public static String convertDate(String s) throws ParseException {
		if (s == null || "".equals(s)) {
			return "";
		}
		// 将excel读取日期时遇到数字 转化为日期
		// Excel 的一个有趣之处就是,当您试图将数字转换为日期时,程序会假定该数字是一个序列号,
		// 代表自 1900 年 1 月 1 日起所发生的天数。自 1900 年 1 月 1 日 算起的第 39331 天就是 2007 年 9 月 6 日
		String rtn = "1900-01-01";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date1 = new Date();
		date1 = format.parse("1900-01-01");
		long i1 = date1.getTime();
		// 这里要减去2,(Long.parseLong(s)-2) 不然日期会提前2天,具体原因不清楚,
		// 估计和java计时是从1970-01-01开始有关
		// 而excel里面的计算是从1900-01-01开始
		i1 = i1 / 1000 + ((Long.parseLong(s) - 2) * 24 * 3600);
		date1.setTime(i1 * 1000);
		rtn = format.format(date1);
		return rtn;
	}
}

github上的源码:https://github.com/lijiedong/jxldemo
参考连接:https://blog.csdn.net/debbykindom/article/details/7254371
https://www.xuebuyuan.com/1688997.html

你可能感兴趣的:(JAVA)