springmvc 导出excel
  TEZNKK3IfmPf 2023年11月13日 37 0
@RequestMapping("/dataLogToExcel")  
@ResponseBody
public void OperationLogToExcel(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {


// 生成提示信息,
response.setContentType("application/vnd.ms-excel");
String codedFileName = null;
OutputStream fOut = null;
try
{
// 进行转码,使其支持中文文件名
codedFileName = java.net.URLEncoder.encode("数据操作日志", "UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
// response.addHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".xls");
// 产生工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
//产生工作表对象
HSSFSheet sheet = workbook.createSheet();

//设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth(15);

//设置自动换行
sheet.setFitToPage(true);
// 设置标题
HSSFCellStyle titleStyle = workbook.createCellStyle();
// 居中显示
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 标题字体
HSSFFont titleFont = workbook.createFont();
// 字体大小
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
titleStyle.setFont(titleFont);

HSSFCellStyle contentStyle = workbook.createCellStyle();
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont contentFont = workbook.createFont();
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
contentStyle.setFont(contentFont);

// 产生表格标题行
HSSFRow row = sheet.createRow(0);
String[] headers = new String[] { "序号", "操作时间", "同步类型", "同步条数", "同步内容",
"同步结果", };
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//生成数据
List<DataLog> dataLogList = dataLogService.selectAllDataLog();
int rowCount = 1;
for (int i = 0; i < dataLogList.size(); i++, rowCount++) {
HSSFRow dataRow = sheet.createRow(rowCount);
DataLog dataLog=dataLogList.get(i);

//序号
HSSFCell cell0 = dataRow.createCell(0);
cell0.setCellValue((i + 1));
cell0.setCellStyle(contentStyle);



//操作时间
HSSFCell cell1 = dataRow.createCell(1);
Date dataTime=dataLog.getDataTime();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dataTime);
cell1.setCellValue(dateString);
cell1.setCellStyle(contentStyle);

//同步类型
HSSFCell cell2 = dataRow.createCell(2);
cell2.setCellValue(dataLog.getDataType());
cell2.setCellStyle(contentStyle);
cell2.setAsActiveCell();

//同步条数
HSSFCell cell3 = dataRow.createCell(3);
cell3.setCellValue(dataLog.getDataCount());
cell3.setCellStyle(contentStyle);
cell3.setAsActiveCell();

//同步内容
HSSFCell cell4 = dataRow.createCell(4);
cell4.setCellValue(dataLog.getDataContent());
cell4.setCellStyle(contentStyle);

//同步结果
HSSFCell cell5 = dataRow.createCell(5);
cell5.setCellValue(dataLog.getDataContent());
cell5.setCellStyle(contentStyle);


}


fOut = response.getOutputStream();
workbook.write(fOut);
}
catch (UnsupportedEncodingException e1)
{}
catch (Exception e)
{}
finally
{
try
{
fOut.flush();
fOut.close();
}
catch (IOException e)
{}
//session.setAttribute("state", "open");
}
System.out.println("文件生成...");
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月13日   24   0   0 springMVC上传
  TEZNKK3IfmPf   2023年11月13日   37   0   0 springMVC
  TEZNKK3IfmPf   2023年11月13日   31   0   0 springMVC
  TEZNKK3IfmPf   2023年11月13日   51   0   0 springMVC
  TEZNKK3IfmPf   2023年11月14日   24   0   0 springMVC
  TEZNKK3IfmPf   2023年11月14日   29   0   0 springMVCspring
  TEZNKK3IfmPf   2023年11月14日   30   0   0 springMVC
TEZNKK3IfmPf