jxl 的详细用法说明

来源:互联网 发布:html5个人网站源码 编辑:程序博客网 时间:2024/05/17 04:50
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")); //创建xlsWritableSheet 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"); //准备路径图片文件,后缀名必须是pngWritableImage 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("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();}