您的位置:首页 > 其它

jxl 的详细用法说明

2017-07-28 15:49 295 查看
package example_1;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.awt.Color;
import java.awt.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class Xample_2 {
public static void main(String [] args)throws IOException,Exception,WriteException{
WritableWorkbook book = Workbook.createWorkbook(new File("C:\\Users\\Administrator\\Desktop\\monkey\\测试.xls")); //创建xls
WritableSheet sheet = book.createSheet("sheet1", 0); //创建工作表

WritableFont font = new WritableFont(WritableFont.createFont("宋体"),20,WritableFont.BOLD);//创建设置 (字体、加粗、字体大小)
WritableCellFormat wc = new WritableCellFormat(font); //将字体设置集合
/*
* 设置背景颜色
*/
wc.setBackground(Colour.SEA_GREEN); // 在集合中添加设置背景颜色
/*
* 设置居中
*/
wc.setAlignment(Alignment.CENTRE); //水平居中对齐
wc.setVerticalAlignment(VerticalAlignment.CENTRE); //竖直方向居中对齐

Label label = new Label(0, 0, "你好 java",wc); //将坐标位置,文本内容和字体模式和背景添加集合
/*
* 坐标位置、合并单元格和设置高度与宽度
*/
Label label1 = new Label(0, 1, "第一列第二行"); //坐标概念:第一个数值是列数   第二个数值是行数
Label label2 = new Label(1 ,0,"第二列第一行");
Label label3 = new Label(2, 0, "合并单元格里的内容");

sheet.setRowView(0,800); //设置第一行的 高度
sheet.setColumnView(3, 20); //设置四列的 宽度
sheet.mergeCells(2, 0, 2, 1); //合并单元格
sheet.addCell(label); //将集合内容输入到指定工作表
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3); //合并单元格后输入内容
/*
* 插入图片
*/
File file = new File("C:\\Users\\Administrator\\Desktop\\monkey\\123.png"); //准备路径图片文件,后缀名必须是png
WritableImage png = new WritableImage(0, 6, 3, 9, file); //设置插入图片位置与大小,第一个组合值是位置是坐标,第二个组合值是以第一个坐标为起点的长度和宽度的行列值。
sheet.addImage(png); //写入图片

book.write(); //写入
book.close(); //关闭

/*
* 读取xls表内容
*/
try{
Workbook workbook = Workbook.getWorkbook(new File("C:\\Users\\Administrator\\Desktop\\monkey\\测试.xls")); //获取文件路径
Sheet s = workbook.getSheet(0); //获取工作表
Cell cell = s.getCell(0,0); //获取指定表格位置
String result = cell.getContents(); //获取内容
System.out.println(result);
}catch(IOException e1){
e1.printStackTrace();
}catch (Exception e2) {
e2.printStackTrace();
}

}
}

/*
*	 颜色翻译:
*	    BLACK  黑色
BLUE 蓝色
BLUE GREY 蓝灰色
BRIGHT GREEN 明亮的绿色
BROWN 棕色
CORAL 珊瑚
DARK BLUE 深蓝
DARK GREEN 深绿色
DARK PURPLE 深紫色
DARK_RED 深红
SEA GREEN 海上绿色
LIGHT BLUE 浅蓝

*/

在xls和sheet已经存在的情况下输入数据:

public void exportXls(String name,Map<String, String> map,int i)throws Exception,IOException,WriteException {
Set<String> setkey = map.keySet();
Iterator<String> it = setkey.iterator();
File file = new File(workspace_path+"\\"+name+".xls");
WritableFont font = new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.NO_BOLD);
WritableCellFormat wf = new WritableCellFormat(font);
wf.setAlignment(Alignment.CENTRE);
wf.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableWorkbook book; //先声明WritableWorkbook (打开的Excel文件)
if(file.exists()){ //判断文件是否存在
Workbook book_1 = Workbook.getWorkbook(file); //Excel存在,获得Excel文件
WritableWorkbook bWorkbook= Workbook.createWorkbook(file,book_1);// 打开一个Excel的副本,并且指定数据写回到原文件
book = bWorkbook; //把打开的Excel副本传回
}else{
WritableWorkbook book_2 = Workbook.createWorkbook(file); //Excel不存在,创建文件,并且打开
book = book_2; //把打开的Excel传出
}
WritableSheet sheet1; //声明 一个WritableSheet (工作表)
if((book.getSheet(0))!=null){ //获取0位置上的工作表,并且判断是否不等于null。
WritableSheet sheet = book.getSheet(0); //不等于null,工作表存在,获取0位置上的工作表。
sheet1 =sheet; //把获取的工作表传出
}else {
WritableSheet sheet = book.createSheet("sheet1", 0); //等于null,工作表不存在,创建0位置上的工作表。
sheet1 = sheet; // 把获取的工作表传出
Label label1 = new Label(0,0,"用例名",wf); //输入第一行的标题栏信息
Label label2 = new Label(1,0,"运行结果",wf);
Label label3 = new Label(2,0,"异常输出",wf);
Label label4 = new Label(3,0,"开始时间",wf);
Label label5 = new Label(4,0,"结束时间",wf);
sheet1.addCell(label1);
sheet1.addCell(label2);
sheet1.addCell(label3);
sheet1.addCell(label4);
sheet1.addCell(label5);
}
while (it.hasNext()){ //输入内容
String str = (String)it.next();
String text = (String)map.get(str);
if(str.equals("
954a
01")){
Label label1_1 = new Label(0,1+i,text,wf);
sheet1.addCell(label1_1);
}else if(str.equals("02")){
Label label2_1 = new Label(1,1+i,text,wf);
sheet1.addCell(label2_1);
}else if(str.equals("03")){
Label label3_1 = new Label(2,1+i,text,wf);
sheet1.addCell(label3_1);
}else if(str.equals("04")){
Label label4_1 = new Label(3,1+i,text,wf);
sheet1.addCell(label4_1);
}else if(str.equals("05")){
Label label5_1 = new Label(4,1+i,text,wf);
sheet1.addCell(label5_1);
}
}
book.write();
book.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: