jxl导出xls后缀的excel文件

4 篇文章 0 订阅
2 篇文章 0 订阅

基于ssm+jsp的导出方法,使用的是jxl

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


@RequiresPermissions("sys:messageSendLog:view")

@RequestMapping(value = "export")
public void exportFile(MessageSendLog messageSendLog, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) throws Exception {
try {
// 获取要导出的数据
List<MessageSendLog> dataList = messageSendLogService.findList(messageSendLog);
if (dataList != null && !dataList.isEmpty()) {
// 如果有数据,则获取导出模板
String destPath = request.getSession().getServletContext().getRealPath("");
File excelFile = new File(destPath + "/template/xls/导出模板.xls");
String fileName = "数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xls";// .xlsx
File tempXls = new File(SystemPath.getSystempPath() + File.separator + fileName);
FileUtils.copyFile(excelFile, tempXls);
if (tempXls.exists()) {// 获取模板后,开始导出数据
String filePath = writeExcel(dataList, tempXls.getPath());
if (StringUtils.isNotBlank(filePath)) {
File file = new File(filePath);
/*ServerFileExportUtils.output(response, filePath, "application/vnd.ms-excel", false);*/
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8")+";filename*=UTF-8''"+URLEncoder.encode(fileName,"UTF-8"));
response.setContentLength((int) file.length());
response.setHeader("Connection", "close");
ServletOutputStream op = response.getOutputStream();
int bufferSize = 131072;
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(786432);
byte[] barray = new byte[bufferSize];
int nRead, nGet;
try {
while ((nRead = fileChannel.read(bb)) != -1) {
if (nRead == 0)
continue;
bb.position(0);
bb.limit(nRead);
while (bb.hasRemaining()) {
nGet = Math.min(bb.remaining(), bufferSize);
bb.get(barray, 0, nGet);
op.write(barray);
}
bb.clear();
}
op.flush();
} catch (IOException e) {
throw e;
} finally {
bb.clear();
fileChannel.close();
fileInputStream.close();
op.close();
}
}
}
}
} catch (Exception e) {
logger.error("导出短信数据失败,信息为: " + e.getMessage());
}
}

=========================下面的方法供上面调用=========================================================

/**
* @Title: writeExcel
* @Description: 写Excel文件
* @param sysLogList
* @return
*/
private String writeExcel(List<MessageSendLog> sysLogList,String desctFilePath) {
try {
File destFile = new File(desctFilePath);
if (destFile.exists()) {
if (sysLogList != null && !sysLogList.isEmpty()) {
Workbook wb = Workbook.getWorkbook(destFile);
WritableWorkbook book = Workbook.createWorkbook(destFile,wb);
WritableSheet sheet = book.getSheet(0);
// 导入数据到Excel中
int i = 0;
jxl.write.WritableCellFormat wcsB = new jxl.write.WritableCellFormat(); 
wcsB.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.MEDIUM);
for (MessageSendLog msg : sysLogList) {
sheet.addCell(new Label(0,i+2,String.valueOf((i+1)),wcsB));
sheet.addCell(new Label(1,i+2,msg.getMessageContent(),wcsB));
String sts = msg.getStatus();
String statusValue = DictUtils.getDictLabel(sts, "send_status", "");
sheet.addCell(new Label(2,i+2,statusValue,wcsB));
Integer message = msg.getMessageType();
String messageTypeValue = String.valueOf(message); 
String messageType = DictUtils.getDictLabel(messageTypeValue, "message_type", "");
sheet.addCell(new Label(3,i+2,messageType,wcsB));
String sendTime = msg.getSendTime() == null?"":msg.getSendTime().toString();
if (StringUtils.isNotBlank(sendTime)) {
Date sendDate =new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(sendTime);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newSendTime=sdf.format(sendDate);
sheet.addCell(new Label(4,i+2,newSendTime,wcsB));
}else {
sheet.addCell(new Label(4,i+2,null,wcsB));
}
String times = msg.getResendNum() == null?"":msg.getResendNum().toString();
sheet.addCell(new Label(5,i+2,times,wcsB));
String sendBy = msg.getSendBy() == null?"":msg.getSendBy().toString();
sheet.addCell(new Label(6,i+2,sendBy,wcsB));
String sendTelephone = msg.getSendTelephone() == null?"":msg.getSendTelephone().toString();
sheet.addCell(new Label(7,i+2,sendTelephone,wcsB));
String sendUnit = msg.getSendUnit() == null?"":msg.getSendUnit().toString();
sheet.addCell(new Label(8,i+2,sendUnit,wcsB));
String principalBy = msg.getPrincipalBy() == null?"":msg.getPrincipalBy().toString();
sheet.addCell(new Label(9,i+2,principalBy,wcsB));
String principalTelephone = msg.getPrincipalTelephone() == null?"":msg.getPrincipalTelephone().toString();
sheet.addCell(new Label(10,i+2,principalTelephone,wcsB));
String principalUnit = msg.getPrincipalUnit() == null?"":msg.getPrincipalUnit().toString();
sheet.addCell(new Label(11,i+2,principalUnit,wcsB));
String createTime = msg.getCreateTime() == null?"":msg.getCreateTime().toString();
           if (createTime !="") {
                Date createDate =new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK).parse(createTime);
                    SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String newCreateTime=sdf1.format(createDate);
                    sheet.addCell(new Label(12,i+2,newCreateTime,wcsB));
}else {
sheet.addCell(new Label(12,i+2,null,wcsB));
}
String remindType = msg.getRemindType() == null?"":msg.getRemindType().toString();
String remindTypeValue = DictUtils.getDictLabel(remindType, "remind_type", ""); 
sheet.addCell(new Label(13,i+2,remindTypeValue,wcsB));
String remindMode = msg.getRemindMode() == null?"":msg.getRemindMode().toString();
String remindModeValue = DictUtils.getDictLabel(remindMode, "remind_mode", ""); 
sheet.addCell(new Label(14,i+2,remindModeValue,wcsB));
i++;
}
book.write();
book.close();
wb.close();
return destFile.getPath();
}
}
} catch (Exception e) {
logger.error("文件下载->写文件时异常,信息为: " + e.getMessage());
}
return null;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值