java jxl插件导出excel

public boolean exportFullGpsDataXls(PagesBean<FullGpsData> page)
throws WriteException {
String download = ServletActionContext.getServletContext().getRealPath(
"eeee.xls");
String logo = ServletActionContext.getServletContext().getRealPath(
"/file/logo.PNG");
File newFile = new File(download);
String titleName="";
if (null != page) {
 titleName = "FullGpsData" + page.getList().get(0).getDate();
}
try {
newFile.createNewFile();
WritableWorkbook workbook = Workbook.createWorkbook(newFile);
WritableSheet sheet = workbook.createSheet("xxxx", 0);
for (int i = 0; i < 12; i++) {
if (i == 10) {
sheet.setColumnView(i, 18);
} else {
sheet.setColumnView(i, 16);
}
}
WritableCellFormat titFormat = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 16,
WritableFont.BOLD));
titFormat.setAlignment(Alignment.CENTRE);
WritableCellFormat titFormat2 = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 20,
WritableFont.BOLD));
titFormat2.setAlignment(Alignment.CENTRE);


WritableCellFormat titFormat3 = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 10,
WritableFont.BOLD));
titFormat3.setAlignment(Alignment.CENTRE);
titFormat3.setVerticalAlignment(VerticalAlignment.CENTRE);
titFormat3.setWrap(true);


WritableCellFormat format = new WritableCellFormat(
new WritableFont(WritableFont.createFont("宋体"), 10));
format.setAlignment(Alignment.CENTRE);
format.setVerticalAlignment(VerticalAlignment.CENTRE);
format.setWrap(true);



File imageFile = new File(logo);
if (imageFile.exists()) {
WritableImage image = new WritableImage(4.0, 0.1, 0.42, 2.45,
imageFile);
sheet.addImage(image);
}


Label label = new Label(0, 0, "xxxx", titFormat);// 列,行
sheet.addCell(label);
sheet.mergeCells(0, 0, 11, 1);// 第几列第几行合并到第几列第几行 从0开始


Label label1 = new Label(0, 2, page.getList().get(0).getLineName()
+ "线路车辆定位信息", titFormat2);// 列,行
sheet.addCell(label1);
sheet.mergeCells(0, 2, 11, 3);// 第几列第几行合并到第几列第几行 从0开始


sheet.addCell(new Label(0, 4, "序号", titFormat3));
sheet.addCell(new Label(1, 4, "车号", titFormat3));
sheet.addCell(new Label(2, 4, "xx路", titFormat3));
sheet.addCell(new Label(3, 4, "日期", titFormat3));
sheet.addCell(new Label(4, 4, "时间", titFormat3));
sheet.addCell(new Label(5, 4, "xx 状态", titFormat3));
sheet.addCell(new Label(6, 4, "xx度", titFormat3));
sheet.addCell(new Label(7, 4, "xx度", titFormat3));
sheet.addCell(new Label(8, 4, "速度(km/h)", titFormat3));
sheet.addCell(new Label(9, 4, "xx运行方向", titFormat3));
sheet.addCell(new Label(10, 4, "当前站", titFormat3));
sheet.addCell(new Label(11, 4, "信息状态", titFormat3));


int i = 5;
int n = 1;
if (null != page) {
for (FullGpsData fullGpsData : page.getList()) {
sheet.addCell(new Label(0, i, String.valueOf(n++), format));
sheet.addCell(new Label(1, i, fullGpsData.getBusName(),
format));
sheet.addCell(new Label(2, i, fullGpsData.getLineName(),
format));
sheet
.addCell(new Label(3, i, fullGpsData.getDate(),
format));
sheet
.addCell(new Label(4, i, fullGpsData.getTime(),
format));
sheet.addCell(new Label(5, i, fullGpsData.getRunstatus(),
format));
sheet.addCell(new Label(6, i, fullGpsData.getLongitude(),
format));
sheet.addCell(new Label(7, i, fullGpsData.getLatitude(),
format));
sheet.addCell(new Label(8, i, fullGpsData.getSpeed(),
format));
sheet
.addCell(new Label(9, i, fullGpsData.getOup(),
format));
sheet.addCell(new Label(10, i,
fullGpsData.getStationname(), format));
sheet.addCell(new Label(11, i, "正常", format));
i++;
}
workbook.write();
workbook.close();
}


} catch (Exception e) {
e.printStackTrace();
return false;
}
return exportTo(newFile, download, titleName);

}




/**
* 导出excel,保存至...

* @param fullGpsDataList
* @return
*/
public static boolean exportTo(File newFile, String download,
String titleName) {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
if (newFile.exists()) {
try {
HttpServletResponse response = getResponse();
response.setContentType("application/octec-stream");
response.setHeader("Content-Disposition",
"attachment;filename="
+ new String(titleName.getBytes("iso-8859-1"),
"UTF-8") + ".xls");
bos = new BufferedOutputStream(response.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(download));
byte[] buffer = new byte[2 * 1024];
int i = -1;
while ((i = bis.read(buffer)) != -1) {
bos.write(buffer, 0, i);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
}
return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值