Jxl.jar包对Excel文档进行简单操作

package com.achievo;

import java.io.*;
import jxl.*;
import jxl.write.*;

public class JxlDemo{
	
	public static void main (String[] args)throws Exception {
		JxlDemo jd = new JxlDemo();
		jd.createXLS();
		jd.readXLS();
    }
    //创建XLS文档,并写入数据
    public  void createXLS()throws Exception{
    	//create new XLS document
		jxl.write.WritableWorkbook book = jxl.Workbook.createWorkbook(new File("测试.xls"));
		//生成名为“第一页”的工作表,参数0表示是第一页 
		jxl.write.WritableSheet sheet = book.createSheet("第一页",0);
		//在Label对象的构造出单元格第一列第一行(0,0,)的值为test
		jxl.write.Label label = new jxl.write.Label(0,0,"test");
		//将定义好的Label对象利用sheet对象添加到工作表中
		sheet.addCell(label);
		//生产数据的单元格
		jxl.write.Number number = new jxl.write.Number(1,0,123.21);
		sheet.addCell(number);
		//写入数据并进行关闭
		book.write();
		book.close();
	}
	//一次型读取XLS文档
	public  void readXLS()throws Exception{
		//获取XLS对象
		jxl.Workbook book = jxl.Workbook.getWorkbook(new File("测试.xls"));
		//获取XLS第一页的工作表
		jxl.Sheet sheet = book.getSheet(0);
		//进行读取,可根据自己的实际情况去读取
		int i=0;
		do{
			int j=0;
			String str1;
			
				do{
					jxl.Cell cell;
					try{
						//通过j,i的坐标找到单元格
						 cell = sheet.getCell(j,i);
					}catch(ArrayIndexOutOfBoundsException ex){
						break;
					}
					//用String接收单元格的值
					str1= cell.getContents();
					System.out.println(str1);
					j++;
				}while(true);
			i++;
		}while(true);
		
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的 Java 实现: ``` import java.io.File; import java.util.ArrayList; import java.util.List; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class Appliances { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) throws Exception { if (brand.length() < 4) { throw new Exception("Brand must be at least 4 characters."); } this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) throws Exception { if (price < 0) { throw new Exception("Price cannot be negative."); } this.price = price; } public static void main(String[] args) { List<Appliances> goods = new ArrayList<>(); Appliances a1 = new Appliances(); try { a1.setBrand("Brand1"); a1.setPrice(100.0); } catch (Exception e) { e.printStackTrace(); } goods.add(a1); Appliances a2 = new Appliances(); try { a2.setBrand("Brand2"); a2.setPrice(200.0); } catch (Exception e) { e.printStackTrace(); } goods.add(a2); File file = new File("goodsInfo.xls"); try { WritableWorkbook workbook = Workbook.createWorkbook(file); WritableSheet sheet = workbook.createSheet("Goods", 0); sheet.addCell(new Label(0, 0, "Brand")); sheet.addCell(new Label(1, 0, "Price")); int row = 1; for (Appliances a : goods) { sheet.addCell(new Label(0, row, a.getBrand())); sheet.addCell(new Label(1, row, String.valueOf(a.getPrice()))); row++; } workbook.write(); workbook.close(); } catch (Exception e) { e.printStackTrace(); } // TODO: Implement data modification and retrieval using jxl.jar } } ``` 这个实现定义了 `Appliances` 类,并含了 `brand` 和 `price` 字段,以及相应的 getter 和 setter 方法。在 setter 方法中,如果品牌长度小于 4 或价格小于 0,就抛出异常。在 `main` 方法中,创建了两个 `Appliances` 对象,并将它们添加到一个 `ArrayList` 中。然后,使用 `jxl.jar` 库将商品信息写入 Excel 文件 `goodsInfo.xls` 中。最后,需要实现数据修改和检索,这里还没有完成。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值