java 操作execl

本文提供了一个使用Java导出Excel文件的示例代码。该示例展示了如何创建包含特定列标题的工作簿,并填充数据。此外,还介绍了如何设置单元格格式、列宽等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void exportExcel(List xwList, String excelName) throws Exception {
String[] header = { "信访署名", "阅信人/接待人", "信访目的", "一级分类", "二级分类", "三级分类",
"人数", "信访形式", "主送单位/问题属地" }; // 列标题
File file=new File("G:\\test\\");
if(!file.exists()){file.mkdir();}
OutputStream os=new FileOutputStream("G:\\test\\"+"testexcel.xls");
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet(excelName, 0);
WritableCellFormat wcf_header = getHeader();
WritableCellFormat wcf_center = getCellCls();
for (int i = 0; i < header.length; i++) {
sheet.addCell(new Label(i, 0, header[i], wcf_header));
}
int row = 1;
try {
for (int i = 0; i < xwList.size(); i++) {
Object[] obj = (Object[]) xwList.get(i);
sheet.addCell(new Label(0, row, StringUtil.isNull(obj[2]) ? ""
: obj[2].toString(), wcf_center));
sheet.setColumnView(0, 13);
sheet.addCell(new Label(1, row, StringUtil.isNull(obj[18]) ? ""
: obj[18].toString(), wcf_center));
sheet.setColumnView(1, 18);
sheet.addCell(new Label(2, row, StringUtil.isNull(obj[3]) ? ""
: obj[3].toString(), wcf_center));
sheet.setColumnView(2, 12);
sheet.addCell(new Label(3, row, StringUtil.isNull(obj[23]) ? ""
: obj[23].toString(), wcf_center));
sheet.setColumnView(3, 17);
sheet.addCell(new Label(4, row, StringUtil.isNull(obj[24]) ? ""
: obj[24].toString(), wcf_center));
sheet.setColumnView(4, 17);
sheet.addCell(new Label(5, row, StringUtil.isNull(obj[25]) ? ""
: obj[25].toString(), wcf_center));
sheet.setColumnView(5, 17);
sheet.addCell(new Label(6, row, StringUtil.isNull(obj[8]) ? ""
: obj[8].toString(), wcf_center));
sheet.setColumnView(6, 13);
sheet.addCell(new Label(7, row, StringUtil.isNull(obj[5]) ? ""
: obj[5].toString(), wcf_center));
sheet.setColumnView(7, 12);
sheet.addCell(new Label(8, row, StringUtil.isNull(obj[9]) ? ""
: obj[9].toString(), wcf_center));
sheet.setColumnView(8, 40);
row++;
}
} catch (Exception e) {
throw e;
}
}


// 设置header样式
public WritableCellFormat getHeader() throws Exception {
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 13,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.WHITE);
WritableCellFormat wcf_title = new WritableCellFormat(titleFont);
wcf_title.setBackground(Colour.GRAY_25, Pattern.SOLID);
wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_title.setAlignment(Alignment.CENTRE);
return wcf_title;
}

public WritableCellFormat getCellCls() throws Exception {
WritableFont NormalFont = new WritableFont(WritableFont.TAHOMA, 11);
WritableCellFormat wcf_center = new WritableCellFormat(NormalFont);
wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直对齐
wcf_center.setAlignment(Alignment.LEFT);
return wcf_center;
}

public WritableCellFormat getTitleCls() throws Exception {
WritableFont font = new WritableFont(WritableFont.TIMES, 14,
WritableFont.BOLD);// 定义字体
WritableCellFormat format = new WritableCellFormat(font);
format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中
format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 黑色边框
return format;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值