[Java的] 阅读在Java中的Excel文件JXL – 读写Excel文件中的Java与JXL

在工作​​和数据处理的过程中,我们经常要与该文件特别是Excel文件交互. 今天,我会告诉你简单的步骤来读取记录JXL Excel文件. JXL是一个库包使我们能够交互称为Excel作为读, 记录,.., 你看到JXL包详细信息 这里.

首先你的 下载JXL (或 这里), 解压缩* .jar文件,然后复制只是项目, 右击选择 构建路径 / 添加到构建路径 能够使用该库. 现在,我们转而学习:
如何创建并写入Excel文件与JXL
如何阅读JXL Excel文件
如何打开和写入Excel文件必须JXL
代码插图

创建并写入Excel文件用JXL

步 1: 创建对象 将WritableWorkbook “点” 提交您的. 请注意,如果您的文件已经存在,它会被删除并重新创建.

WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));

步 2: 创建 WritableSheet – 你需要写数据表:

WritableSheet sheet = workbook.createSheet("name sheet", 0);

注意: 在功能上可以createSheet 2 争论, 第一个参数是表的字符串名称, 第二个参数 2 是表示片材的位置的整数。, 表位置开始 0.

步 3: 下一步,我们将通过方法的数据格式添加到细胞 addCell. 写数据到细胞, 我们将有 3 主要形式: 链, 数字和公式,分别由标签产生, 号码, 公式. 例:

sheet.addCell(new Label(0, 0, "Add a String to cell")); // add a String to cell A1
sheet.addCell(new Number(0, 1, 100)); // add number 100 to cell A2
sheet.addCell(new Formula(0, 3, "IF(A1=1,"one", "two")")); // add number 100 to cell A3

步 4: 当我们这样做 3, 我们要执行的命令 来完成数据记录

workbook.write();
workbook.close();

阅读与JXL Excel文件

步 1: 创建 工作簿 “点” 提交您的.

Workbook workbook = Workbook.getWorkbook(new File(fileName));

步 2: 拍摄 你想读. 你可以得到的位置或名称表表

Sheet sheet = workbook.getSheet(0);

步 3: 读每个单元的内容,在电子表格. 如果你想获得一个框,你可以做以下的内容: sheet.getCell(同, 排).getContents(). 但是,如果你想读在工作表中的整个小区采取的最后一排,并包含数据列 sheet.getRows()sheet.getColumns(), 并使用循环来读取每个单元. 看完后, 我们还需要 关闭工作簿 写数据时

for (int row = 0; row < rows; row++) {
	for (int col = 0; col < cols; col++) {
		Cell cell = sheet.getCell(col, row);
		System.out.print(cell.getContents() + "t");
	}
	System.out.println("n");
}
workbook.close();

打开并把数据导入到Excel与JXL

打开并把数据转换成Excel文件, 首先,我们需要得到 工作簿 从Excel文件应该写更多喜欢当我们读到. 然后创建一个 将WritableWorkbook 该工作簿已经采取,我们将与这个将WritableWorkbook正常.

Workbook workbook = Workbook.getWorkbook(new File(fileName));
WritableWorkbook writeWorkbook = Workbook.createWorkbook(new File(fileName), workbook);

演示代码

read wirte Excel file in java

package vietSource.net.IOFile;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

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

/**
 * ----------------- @author nguyenvanquan7826 -----------------
 * ---------------nguyenvanquan7826.wordpress.com --------------
 */
public class ReadWriteExcel {

	private final String fileName = "/home/nguyenvanquan7826/Desktop/nguyenvanquan7826.xls";

	// data to write file
	private Object[][] data = { { "STT", "Họ và tên", "Điểm", "Xếp loại" },
			{ "1", "Nguyễn Văn Quân", "9.0", "" },
			{ "2", "Phạm Thị Hà", "8.0", "" },
			{ "3", "Nguyễn Bá Cường", "8.5", "" },
			{ "4", "Vũ Công Tịnh", "9.0", "" },
			{ "5", "Phạm Trọng Khang", "8", "" },
			{ "6", "Mai Văn Tài", "8", "" } };

	// create and write new file *.xls
	private void writeFileExcel() {
		WritableWorkbook workbook;
		// create workbook
		try {
			workbook = Workbook.createWorkbook(new File(fileName));

			// create sheet
			WritableSheet sheet1 = workbook.createSheet("KTPM K10B", 0);

			// create Label and add to sheet
			sheet1.addCell(new Label(0, 0, "DANH SÁCH SINH VIÊN TIÊU BIỂU"));

			// row begin write data
			int rowBegin = 2;
			int colBegin = 0;

			for (int row = rowBegin, i = 0; row < data.length + rowBegin; row++, i++) {
				for (int col = colBegin, j = 0; col < data[0].length + colBegin; col++, j++) {
					Object obj = data[i][j];
					sheet1.addCell(new Label(col, row, (String) data[i][j]));
				}
			}
			// write file
			workbook.write();

			// close
			workbook.close();
		} catch (IOException e) {
			System.out.println("Error create file\n" + e.toString());
		} catch (RowsExceededException e) {
			System.out.println("Error write file\n" + e.toString());
		} catch (WriteException e) {
			System.out.println("Error write file\n" + e.toString());
		}
		System.out.println("create and write success");
	}

	// open and read file *.xls
	private void readFileExcel() {
		Workbook workbook;
		try {
			// create workbook to open file
			workbook = Workbook.getWorkbook(new File(fileName));
			// get sheet want read
			Sheet sheet = workbook.getSheet(0);
			// get number row and col contain data
			int rows = sheet.getRows();
			int cols = sheet.getColumns();

			System.out.println("Data in file:");
			// read data in each cell
			for (int row = 0; row < rows; row++) {
				for (int col = 0; col < cols; col++) {
					Cell cell = sheet.getCell(col, row);
					System.out.print(cell.getContents() + "\t");
				}
				System.out.println("\n");
			}
			// close
			workbook.close();
		} catch (BiffException e) {
			System.out.println("File not found\n" + e.toString());
		} catch (IOException e) {
			System.out.println("File not found\n" + e.toString());
		}
	}

	// open and write file is exists
	private void openAndWriteFileExcel() {
		Workbook workbook;
		WritableWorkbook writeWorkbook;
		try {
			// open file
			workbook = Workbook.getWorkbook(new File(fileName));
			// create file copy of root file to write file
			writeWorkbook = Workbook.createWorkbook(new File(fileName),
					workbook);

			// get sheet to write
			WritableSheet sheet1 = writeWorkbook.getSheet(0);
			int col = 3;
			int rowBegin = 3;
			// write data (formula)
			for (int row = rowBegin; row < data.length + rowBegin - 1; row++) {
				Formula f = new Formula(col, row, "IF(C" + (row + 1)
						+ ">8, \"Xuất sắc\", \"Giỏi\")");
				sheet1.addCell(f);
			}
			writeWorkbook.write();

			// close
			writeWorkbook.close();
		} catch (IOException e) {
			System.out.println("File not found\n" + e.toString());
		} catch (RowsExceededException e) {
			System.out.println("File not found\n" + e.toString());
		} catch (WriteException e) {
			System.out.println("File not found\n" + e.toString());
		} catch (BiffException e) {
			System.out.println("File not found\n" + e.toString());
		}
		System.out.println("open and write success");
	}

	private void showMenu() {
		System.out.println();
		System.out.println("Select an integer for process:");
		System.out.println("1 - Create new file and wrire data");
		System.out.println("2 - Read file exits");
		System.out.println("3 - Open and write to file exits");
	}

	public static void main(String[] args) {
		ReadWriteExcel rwExcel = new ReadWriteExcel();
		while (true) {
			rwExcel.showMenu();
			Scanner scan = new Scanner(System.in);
			int select = Integer.parseInt(scan.nextLine());
			switch (select) {
			case 1:
				rwExcel.writeFileExcel();
				break;
			case 2:
				rwExcel.readFileExcel();
				break;
			case 3:
				rwExcel.openAndWriteFileExcel();
				break;
			default:
				scan.close();
				break;
			}
		}
	}
}

阅读更多 (阅读更多): Java的API的Excel教程