java通过jxl对Excel进行简单操作

          对excel操作主要有两种方式,一种是通过jxl,一种是通过poi,本文主要讲述通过jxl对excel进行简单的操作,读取Excel,新建Excel,更改Excel内容

import java.io.File;
import java.io.IOException;

import jxl.Cell;
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 excelMain {
	static String[][] name = { { "十", "八", "九" }, { "四", "五", "六" } };

	public static void main(String[] args) {
		String filename = "E:\\ceshi.xls";
		// setExcel(name, filename);
		// try {
		// String[][] neirong = getExcel(filename);
		// for (int i = 0; i < neirong.length; i++)
		// for (int j = 0; j < neirong.length; j++)
		// System.out.println(neirong[i][j]);
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		updateExcel(name, filename);

	}

	/**
	 * 读取excel表
	 * 
	 * @param address
	 *            excel地址
	 * @return
	 * @throws IOException
	 */
	public static String[][] getExcel(String address) throws IOException {
		try {
			Workbook workbook = Workbook.getWorkbook(new File(address));
			Sheet sheet = workbook.getSheet(0);
			String[][] excel = new String[sheet.getRows()][sheet.getColumns()];
			for (int i = 0; i < sheet.getRows(); i++) {
				for (int j = 0; j < sheet.getColumns(); j++) {
					Cell cell = sheet.getCell(i, j);
					excel[i][j] = cell.getContents();
				}
			}
			workbook.close();
			return excel;
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 创建excel表
	 * 
	 * @param excel
	 *            往excel中存放的二维字符串数组
	 * @param address
	 *            存放excel的地址
	 * @return
	 * @throws IOException
	 * @throws WriteException
	 * @throws RowsExceededException
	 */
	public static boolean setExcel(String[][] excel, String address) {
		// 创建工作薄
		WritableWorkbook workbook;
		try {
			workbook = Workbook.createWorkbook(new File(address));

			// 创建新的一页
			WritableSheet sheet = workbook.createSheet("sheel1", 0);
			for (int i = 0; i < excel.length; i++) {
				for (int j = 0; j < excel.length; j++) {
					// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行左边,第三个参数为内容
					Label label = new Label(i, j, excel[i][j]);
					sheet.addCell(label);
				}
			}
			workbook.write();
			workbook.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/**
	 * 更改excel表
	 * 
	 * @param excel
	 *            需要更新的字符串数组
	 * @param address
	 *            文件的地址
	 * @return
	 */
	public static boolean updateExcel(String[][] excel, String address) {
		try {
			Workbook workbook = Workbook.getWorkbook(new File(address));
			WritableWorkbook workbooks = Workbook.createWorkbook(new File(
					address), workbook);
			WritableSheet sheet = workbooks.getSheet(0);
			for (int i = 0; i < excel.length; i++) {
				for (int j = 0; j < excel.length; j++) {
					if (excel[i][j] != null && excel[i][j] != "") {
						// 创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行左边,第三个参数为内容
						Label label = new Label(i, j, excel[i][j]);
						sheet.addCell(label);
					}
				}
			}
			workbooks.write();
			workbooks.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值