java操作有下拉框选择的excel_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java操作有下拉框选择的excel

java操作有下拉框选择的excel

 2012/1/3 9:14:43  八岭书生  http://liangq.iteye.com  我要评论(0)
  • 摘要:从http://sourceforge.net/projects/jexcelapi/files/中下载jxl.jar包然后导入到工程中packagetest;/***读取Excel文件的内容*@paramfile待读取的文件*@return生成Excel的类*/importjava.io.File;importjava.util.ArrayList;importjava.util.List;importjxl.Workbook;importjxl.format.Border
  • 标签:excel Java 操作

从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;
	 }
}

?

发表评论
用户名: 匿名