天天看點

使用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