java创建xls文档

package com.dinstar.sim.test;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.dinstar.sim.model.TblSim;


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


public class ExcelTest1 {
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		try {
			write();
//			read();
//		} catch (BiffException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		} 
		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();
		}
	}
	public static void write() throws IOException, RowsExceededException, WriteException{
		// 打开名字是test.xls的文档
		WritableWorkbook book = Workbook.createWorkbook(new File("d:/test/test.xls"));
		// 创建一个表名字为Sheet 表示这是第一页
		WritableSheet sheet = book.createSheet("Sheet_1", 0);
	
		// 定义单元格 定义行和列  内容定义为test
		Label label = new Label(0,0,"test"); 
		// 将上面的内容 添加到实例的文档中 
		sheet.addCell(label);
		
		//使用add numeric创建单元格。警告:一定使用package-path集成
		//否则将被抛出路径错误。
		//列c 和行 r value 789.123
		Number number = new Number(1,0,789.123);
		// 将上面定义的单元格 添加到文档中
		sheet.addCell(number);
		// 将上面定义的所有单元格添加到case中。
		book.write(); 
		// 结束
		book.close();
	}
	
	public static void read() throws BiffException, IOException{
		Workbook book = Workbook.getWorkbook(new File("d:/test/test.xls"));
		
		Sheet sheet = book.getSheet(0);
		
		int rows = sheet.getRows();
		
		int columns = sheet.getColumns();
		
		System.out.println("row="+rows+",columns="+columns);
		
		Map<Integer, String> heads = new HashMap<Integer, String>();
		for (int a = 0; a < columns; a++) {
			heads.put(a, sheet.getCell(a,0).getContents());
		}
		
		List<String> imsiList = new ArrayList<String>();
		List<TblSim> simList = new ArrayList<TblSim>();
		
		outer:
			for (int i = 1; i < rows; i++) {
				TblSim sim = new TblSim();
				sim.setGrpUuid(1);
				sim.setDomainUuid(1);
				
				for (int j=0;j<columns;j++) {
					Cell cell = sheet.getCell(j,i);
					String result = cell.getContents();
					System.out.println(result);
					
					switch (j) {
					case 0:
						if("Mobile Phone".equals(heads.get(0).toString())){
							sim.setMobile(result);
						}else if("IMSI".equals(heads.get(0).toCharArray())){
							sim.setImsi(result);
							sim.setOperator(result.substring(0,6));
						}
						break;
					case 1:
						if("Mobile Phone".equals(heads.get(1).toString())){
							sim.setMobile(result);
						}else if("IMSI".equals(heads.get(1).toString())){
							sim.setImsi(result);
							sim.setOperator(result.substring(0,6));
							if(imsiList.contains(result)){
								continue outer;
							}else{
								imsiList.add(result);
							}
						}
						break;
					case 2:
						if("Mobile Phone".equals(heads.get(2).toString())){
							sim.setMobile(result);
						}else if("IMSI".equals(heads.get(2).toString())){
							sim.setImsi(result);
							sim.setOperator(result.substring(0,6));
							if(imsiList.contains(result)){
								continue outer;
							}else{
								imsiList.add(result);
							}
						}
						break;
					default:
						break;
					}
				}
				simList.add(sim);
			}
		for (TblSim ts : simList) {
			System.err.println(ts.getMobile()+", "+ts.getOperator()+", "+ts.getImsi());
			book.close();
		}
		
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值