jxl新建excel的简单例子


jxl.jar的下载可以baidu下,或者在csdn的资源下载区也可以下载到。

如下地址也可下载,不需积分:

http://download.csdn.net/detail/laizhenhai88/4155005

 

 

import java.io.File;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.DateTime;
import jxl.write.Boolean;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelUtil {
	public static void main(String[] args) {
		//生成一个可读取的excel文件对象
		File xlsFile = new File("D:\\myfile.xls");
		WritableWorkbook workbook = null;
		WritableSheet sheet = null;
		try {
			workbook = Workbook.createWorkbook(xlsFile);
			//添加第一个工作表
			sheet = workbook.createSheet("sheetName", 0);
			//添加表头Label(列,行,内容) 不知道为什么要先列后行
			sheet.addCell(new Label(0, 0, "中文"));
			sheet.addCell(new Label(1, 0, "甲骨文"));
			
			sheet.addCell(new Label(0, 1, "整数"));
			sheet.addCell(new Number(1, 1, 30));
			
			sheet.addCell(new Label(0, 2, "小数"));
			sheet.addCell(new Number(1, 2, 30.12345));
			
			sheet.addCell(new Label(0, 3, "字体"));
			WritableFont wf = new WritableFont(WritableFont.TIMES,12,WritableFont.BOLD,true,UnderlineStyle.SINGLE,Colour.RED);
			WritableCellFormat cf = new WritableCellFormat(wf);
			sheet.addCell(new Label(1, 3, "加粗红色",cf));
			
			sheet.addCell(new Label(0, 4, "小数格式化"));
			NumberFormat nf = new NumberFormat("#.##");
			WritableCellFormat cf2 = new WritableCellFormat(nf);
			sheet.addCell(new Number(1, 4, 30.12345,cf2));
			
			sheet.addCell(new Label(0, 5, "布尔值"));
			sheet.addCell(new Boolean(1, 5, false));
			
			sheet.addCell(new Label(0, 6, "时间格式化"));
			DateFormat df = new DateFormat("yyyy-MM-dd hh:mm:ss");
			WritableCellFormat cf3 = new WritableCellFormat(df);
			sheet.addCell(new DateTime(1, 6, new java.util.Date(),cf3));
			
			sheet.mergeCells(0,7,1,8);
			sheet.addCell(new Label(0, 7, "合并单元格"));
			
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值