java操作有下拉框选择的excel

从http://sourceforge.net/projects/jexcelapi/files/中下载jxl.jar包


然后导入到工程中


package test;

/**
 * 读取Excel文件的内容
 * @param file  待读取的文件
 * @return      生成Excel的类
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

//创建Excel文件
public class CreateXLS {
	public static void main(String args[]) {
		try {
			WritableWorkbook wwb = Workbook.createWorkbook(new File("test.xls"));
			WritableSheet ws = wwb.createSheet(" Sheet1 ", 0);
		    Label labelMergeCells = new Label(0, 0, "安全对象导入模板", getHeader());
		    ws.addCell(labelMergeCells);
		 	ws.mergeCells(0,0,9,0);
		 	ws.setRowView(0,800);//设置行高

		 	String[] fieldToTitle = new String[] {"Ip地址", "标准系统", "SVN地址"};

		 	//添加表头
		    for(int t=0;t<fieldToTitle.length;t++){
		    	Label label = new Label(t, 1, fieldToTitle[t], getTitle());
		    	if (label.getString().equals("标准系统")) {
		    		for(int td=0; td<10; td++) {
		    			Label subLabel = new Label(t, td, "");
		    			WritableCellFeatures wcf = new WritableCellFeatures();
		    			List angerlist = new ArrayList();
		    			angerlist.add("电话");//可从数据库中取出
		    			angerlist.add("手机");
		    			angerlist.add("呼机");
		    			wcf.setDataValidationList(angerlist);
		    			subLabel.setCellFeatures(wcf);
		    			ws.addCell(subLabel);
		    		}
		    	}
		    	// 将定义好的单元格添加到工作表中
		    	ws.addCell(label);
		    	ws.setColumnView(t, 30);
		    }

			// 写入数据并关闭文件
			wwb.write();
			wwb.close();

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	/**
	  * 设置头的样式
	  * @return
	  */
	 public static WritableCellFormat getHeader(){
	  WritableFont font = new  WritableFont(WritableFont.TIMES, 24 ,WritableFont.BOLD);//定义字体
	  try{
	   font.setColour(Colour.BLUE);//蓝色字体
	  } catch (WriteException e1){
	   e1.printStackTrace();
	  }
	  WritableCellFormat format = new  WritableCellFormat(font);
	  try{
	   format.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
	   format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中
	   format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//黑色边框
	   format.setBackground(Colour.YELLOW);//黄色背景
	  } catch (WriteException e){
	   e.printStackTrace();
	  }
	  return format;
	 }

	 /**
	  * 设置标题样式
	  * @return
	  */
	 public static WritableCellFormat getTitle(){
	  WritableFont font = new  WritableFont(WritableFont.TIMES, 14);
	  try{
	   font.setColour(Colour.BLUE);//蓝色字体
	  } catch (WriteException e1){
	   // TODO 自动生成 catch 块
	   e1.printStackTrace();
	  }
	  WritableCellFormat format = new  WritableCellFormat(font);

	  try {
	   format.setAlignment(jxl.format.Alignment.CENTRE);
	   format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
	   format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
	  } catch (WriteException e){
	   // TODO 自动生成 catch 块
	   e.printStackTrace();
	  }
	  return format;
	 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值