java 导出excel

public boolean exportExcel(LogBean[] beanlist,
HttpServletResponse response) throws IOException, WriteException {
OutputStream os = null;
response.reset(); // 清空输出流
os = response.getOutputStream(); // 取得输出流
String fileName = "日志管理.xls";// 导出文件名
response.setHeader("Content-disposition", "attachment; filename="
+ new String(fileName.getBytes(), "ISO-8859-1")); // 设定输出文件头

response.setContentType("application/msexcel"); // 定义输出类型

// 生成一个 Exc 的工作空间
// 创建新的Excel 工作簿
WritableWorkbook workbook = Workbook.createWorkbook(os);
// 生成 Exc 的Sheet,Sheet名为通讯录导出
WritableSheet sheet = workbook.createSheet("日志", 0);
// 这里是对Cell里面的字体进行格式化
// WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
// WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
// jxl.format.Colour.BLACK);
// WritableCellFormat format = new WritableCellFormat(wfc);
WritableCellFormat wcfFC = new jxl.write.WritableCellFormat();
// 行居中
wcfFC.setAlignment(Alignment.CENTRE);
// 列居中
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);

Label labelCk = null;

// 编号 用户 IP 类型 信息 记录时间
int colindex = 0;
labelCk = new Label(colindex, 0, "编号", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 10);
colindex++;

labelCk = new Label(colindex, 0, "用户 ", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 20);
colindex++;

labelCk = new Label(colindex, 0, "IP", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 15);
colindex++;

labelCk = new Label(colindex, 0, "类型", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 20);
colindex++;

labelCk = new Label(colindex, 0, "信息", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 40);
colindex++;

labelCk = new Label(colindex, 0, "记录时间", wcfFC);
sheet.addCell(labelCk);
sheet.setColumnView(colindex, 40);
colindex++;

if (beanlist != null) {
int rowIndex = 1;
for (LogBean bean : beanlist) {
if (bean == null)
continue;
int row_colindex = 0;
// 编号 用户 IP 类型 信息 记录时间
String order = bean.getId() == null ? "" : bean.getId();
labelCk = new Label(row_colindex, rowIndex, order, wcfFC);
sheet.addCell(labelCk);
row_colindex++;

String userName = bean.getUserName() == null ? ""
: bean.getUserName();
labelCk = new Label(row_colindex, rowIndex, userName,
wcfFC);
sheet.addCell(labelCk);
row_colindex++;

String ip = bean.getIp()== null ? ""
: bean.getIp();
labelCk = new Label(row_colindex, rowIndex,
ip, wcfFC);
sheet.addCell(labelCk);
row_colindex++;

String type_t = bean.getType_t()== null ? ""
: bean.getType_t();
labelCk = new Label(row_colindex, rowIndex,
type_t, wcfFC);
sheet.addCell(labelCk);
row_colindex++;

String info = bean.getInfo() == null ? "" : bean.getInfo();
labelCk = new Label(row_colindex, rowIndex, info, wcfFC);
sheet.addCell(labelCk);
row_colindex++;


String createTime =bean.getCreateTime();
labelCk = new Label(row_colindex, rowIndex, createTime, wcfFC);
sheet.addCell(labelCk);
row_colindex++;

rowIndex++;
}
}
workbook.write();
workbook.close();
return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值