码迷,mamicode.com
首页 > 其他好文 > 详细

Excel之JXL创建

时间:2015-06-21 12:01:15      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:jxl excel

创建excel文件,并写入数据


1.创建Project,导入 :jarcommons-io-2.2.jar    jxl.jar  poi-3.11-20141221.jar  去下载jar

2.编写代码:

package com.imooc.excel;

import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JxlExpExcel {

	/**
	 * JXL创建Excel文件
	 * @author David
	 * @param args
	 */
	public static void main(String[] args) {
		String[] title = {"id","name","sex"};
		boolean flag=true;
		//创建Excel文件
		File file = new File("C:/Users/Administrator/Documents/Downloads/excel/jxl_test1.xls");
		try {
			file.createNewFile();
			//创建工作簿
			WritableWorkbook workbook = Workbook.createWorkbook(file);
			//创建sheet
			WritableSheet sheet = workbook.createSheet("sheet1",1);
			Label label = null;
			//第一行设置列名
			for (int i = 0; i < title.length; i++) {
				label = new Label(i,0,title[i]);
				sheet.addCell(label);	
			}
			//追加数据
			for (int i = 1; i < 10; i++) {
				label = new Label(0,i,"a" + i);
				sheet.addCell(label);
				label = new Label(1,i,"user" + i);
				sheet.addCell(label);
				label = new Label(2,i,"男");
				sheet.addCell(label);
			}
			//写入数据
			workbook.write();
			workbook.close();
			
		} catch (Exception e) {
			flag=false;
			e.printStackTrace();
		}finally{
			System.out.println(flag);
		}
	}
}
3.结果

技术分享

Excel之JXL创建

标签:jxl excel

原文地址:http://blog.csdn.net/pengweid/article/details/46580719

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!