view类在Spring控制器配置文件中的配置
<!-- 配置Excel文件解析器 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <!-- 配置优先级 假如存在一个excelView.jsp页面,如果这个页面的优先级大于Excel 视图类,则直接跳转到jsp页面,而不执行到处Excel文件操作 --> <property name="order" value="10"/> </bean> <bean id="excelView" class="com.lovo.view.UserListViewResolver"/> <bean id="pdfView" class="com.lovo.view.WordListPdfView"/>
导出Excel表格类
package com.lovo.view; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView; import com.lovo.entity.User; /** * 使用静态类AbstractExcelView生成excel文件 * @author tian */ public class UserListViewResolver extends AbstractExcelView { @Override protected void buildExcelDocument(Map<String, Object> map, HSSFWorkbook book, HttpServletRequest req, HttpServletResponse resp) throws Exception { resp.setContentType("application/vnd.ms-excel"); //由于Tomcat内部编码被改成了utf-8 因此需要将告诉浏览器的中文文件名再转回成iso-8859-1 // 浏览器才能够识别出该中文文件名 resp.setHeader("Content-Disposition", "inline;filename="+new String("用户列表".getBytes(), "iso-8859-1")); HSSFSheet sheet = book.createSheet("用户列表"); HSSFCell tempCell; tempCell = getCell(sheet, 0, 0); setText(tempCell, "用户名"); tempCell = getCell(sheet, 0, 1); setText(tempCell, "密码"); List<User> list = (List<User>) map.get("userList"); for(int i=0;i<list.size();++i){ tempCell = getCell(sheet, i+1, 0); setText(tempCell, list.get(i).getUsername()); tempCell = getCell(sheet, i+1, 1); setText(tempCell, list.get(i).getPassword()); } } }
导出PDF类
package com.lovo.view; import java.awt.Color; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.view.document.AbstractPdfView; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class WordListPdfView extends AbstractPdfView { @Override protected void buildPdfDocument(Map<String, Object> map, Document doc, PdfWriter pw, HttpServletRequest req, HttpServletResponse resp) throws Exception { resp.setHeader("Content-Disposition", "inline;filename="+new String("用户列表".getBytes(), "iso-8859-1")); resp.setContentType("application/pdf"); List<String> words = (List<String>) map.get("words"); BaseFont bfont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); Font font = new Font(bfont, 14, Font.BOLD, Color.BLUE); for(int i=0,len=words.size();i<len;++i){ doc.add(new Paragraph(words.get(i), font)); } } }
控制器中,如何实现导出功能
/** * 使用AbstractExcelView生成Excel文件 * 为什么返回excelView后就能跳转到UserListViewResolver??? * @param map * @return */ @RequestMapping("/getExcel") public String getExcel(ModelMap map){ List<User> list = new ArrayList<>(); User u1 = new User("jack", "123"); User u2 = new User("bob", "234"); User u3 = new User("marry", "567"); list.add(u1); list.add(u2); list.add(u3); map.addAttribute("userList", list); return "excelView"; } @RequestMapping("/getPdf") public String getPdf(ModelMap map){ List<String> words = new ArrayList<>(); words.add("shit!"); words.add("fuck!"); words.add("foolish!"); map.addAttribute("words", words); return "pdfView"; }
相关推荐
导出 Excel 用的是 easypoi, 导出 PDF 是用 spire.xls.free 将 Excel 流转化为 PDF 流导出.
这是一款非常强大实用的jQuery将网页表格数据导出excel表格文件、pdf文件和CSV文件代码,点击按钮一键导出,还支持网页打印、查询数据等。
代码片段: <body>[removed][removed] 日程事件导出 事件ID 员工ID 标题 <th>URL 开始时间 事件ID 员工ID 标题 <th>URL 开始时间 </table>
jQuery表格导出excel文件代码是一款日程事件表格点击按钮一键导出excel表格文件,pdf文件,CSV表格效果,支持网页打印代码。
Javascript导出excel为xlsx格式,兼容IE6+和主流浏览器,下载下来直接可以使用。 其他javascript导出excel插件可看作者文章:https://blog.csdn.net/qq_21693027/article/details/80459677
TIA博途V17中DB块如何导出成源文件或XML文件或EXCEL中? 博途中导出DB块的3种方法介绍。
EPLAN表格导出为EXCEL2.pdf
echarts图表导出到excel中的解决方法,做个备忘录,以防忘记
java示例代码,导出pdf文件,加签;可以实现导出pdf的图片签名;导出图片,文件,内容.纯后端代码,代码示例 在项目开发中,产品的需求越来越奇葩啦,开始文件下载都是下载为excel的,做着做着需求竟然变了,要求能导出pdf...
layui表格导出Excel,js对象属性循环
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,csv大数据量导出(千万级别,不会内存溢出),多线程导出 ,生产环境已经很稳定的使用着
NULL 博文链接:https://keep-going.iteye.com/blog/1907664
本篇文章主要介绍了Spring 实现excel及pdf导出表格示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
运行依赖于fr-server-6.5.jar、fr-third-6.5.jar两个jar文件,以及datasource.xml数据库连接定义文件。
bootstrap table导出表格支持中文csv、pdf、txt等
jQuery表格导出excel文件代码是一款日程事件表格点击按钮一键导出excel表格文件,pdf文件,CSV表格效果,支持网页打印代码。
jQuery表格导出excel文件代码是一款日程事件表格点击按钮一键导出excel表格文件,pdf文件,CSV表格效果,支持网页打印代码。
西门子_如何将 WinCC 归档的历史数据导 出到 EXCEL CSV 文件pdf,
web前端表格导出excel、pdf、png等格式的文件。使用插件为tableExport。