java创建xls文档

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Main_CSDN_String/article/details/80688468
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();
		}
		
	}


}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页