java生成excel文档

时间:2023-03-09 03:27:48
java生成excel文档
要做一个后台自动化,要先预先生成一份文档,以下内容生成了文档
首先下载jxl.jar包,下载地址:http://download.csdn.net/detail/prstaxy/4469935
1.生成一个简单的excel文档
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(new File("F:\\test.xls"));
//创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
//创建要显示的具体内容
Label formate = new Label(0,0,"数据格式");
sheet.addCell(formate);
Label floats = new Label(1,0,"浮点型");
sheet.addCell(floats);
Label integers = new Label(2,0,"整型");
sheet.addCell(integers);
Label booleans = new Label(3,0,"布尔型");
sheet.addCell(booleans);
Label dates = new Label(4,0,"日期格式");
sheet.addCell(dates); Label example = new Label(0,1,"数据示例");
sheet.addCell(example);
//浮点数据
Number number = new Number(1,1,3.1415926535);
sheet.addCell(number);
//整形数据
Number ints = new Number(2,1,15042699);
sheet.addCell(ints);
Boolean bools = new Boolean(3,1,true);
sheet.addCell(bools);
//日期型数据
Calendar c = Calendar.getInstance();
Date date = c.getTime();
WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT1);
DateTime dt = new DateTime(4,1,date,cf1);
sheet.addCell(dt);
//把创建的内容写入到输出流中,并关闭输出流 } catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
workbook.write();
System.out.println("写入成功");
workbook.close(); */

生成的内容为

java生成excel文档

2.写入大数据,在指定的列数据中生成一个随机数

public String  createOrderFile() throws RowsExceededException, WriteException {

        // 生成随机单号
randomePlatformID= getRandomNumber(11);
randomePlatformID = "PM" + randomePlatformID;

File file = new File(""); //获取项目的绝对地址
         String abspath = file.getAbsolutePath();

        createUrl= abspath + "\\OfflineOrders.xls";
Log.logInfo("地址是:" + createUrl);
WritableWorkbook workbook = null ;
WritableSheet sheet = null;
try {
workbook = Workbook.createWorkbook(new File(createUrl));
sheet = workbook.createSheet("First Sheet", 0);
} catch (IOException e) {
e.printStackTrace();
Log.logInfo("创建文件失败");
}
String[] cellText = { "平台", "所属人", "对方店铺账号", "付款ID", "平台单号", "订单运费", "发货仓库", "发货渠道", "收件人国家简码"
, "公司名称", "收件人姓名","州", "城市", "地址1", "地址2", "邮编", "电话", "Email", "SKU1", "数量1", "单价", "币种" };
String[] contentText = { "JOOM", "JM3901556", "", "", "PM789", "", "SZ", "DEDHL", "DE", "", "Daniela Weihrauch"
,"Berlin", "Berlin", "Stellingdamm 9", "", "55555", "+49 3068838320", "", "109036701", "3", "17","USD" };
// 生成列头
for (int i = 0; i < cellText.length; i++) {
Label columnHeader = new Label(i, 0, cellText[i]);
try {
sheet.addCell(columnHeader);
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
// 填入正文内容
for (int j = 0; j < contentText.length; j++) {
Label columnBody;
if (j == 4) {// 平台订单号单独处理
Log.logInfo("生成随机10位数平台订单编号为:" + randomePlatformID);
columnBody = new Label(j, 1, randomePlatformID);
} else {
columnBody = new Label(j, 1, contentText[j]);
}
sheet.addCell(columnBody);
}
try {
workbook.write();
Log.logInfo("写入成功");
try {
workbook.close();
} catch (WriteException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} return randomePlatformID; }
java生成excel文档