2017年10月31日16:40:32
做个记录。方便下次查看。

参考:java实现赋值Excel模板并写入后导出 ( ̄▽ ̄)~*

ExcelUtils

@Component
public class ExcelUtils {private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Resourceprivate SysUserMapper sysUserMapper;//声明一个该工具类的静态的内部对象private static ExcelUtils excelUtils;//工具类中需要注入service,dao等需要//使用注解@PostConstruct把需要使用的service,dao等加载到上面定义的静态内部对象中@PostConstructpublic void init() {excelUtils = this;excelUtils.sysUserMapper =  this.sysUserMapper;}/***tempPath 模板文件路径*path 文件路径*list 集合数据*/public void exportExcel(String tempPath, String path, HttpServletResponse response, List<OrderVO> list) {File newFile = createNewFile(tempPath, path);InputStream is = null;XSSFWorkbook workbook = null;XSSFSheet sheet = null;try {is = new FileInputStream(newFile);// 将excel文件转为输入流workbook = new XSSFWorkbook(is);// 创建个workbook,// 获取第一个sheetsheet = workbook.getSheetAt(0);} catch (Exception e1) {e1.printStackTrace();}if (sheet != null) {try {// 写数据FileOutputStream fos = new FileOutputStream(newFile);XSSFRow row = sheet.getRow(0);if (row == null) {row = sheet.createRow(0);}XSSFCell cell = row.getCell(0);if (cell == null) {cell = row.createCell(0);}cell.setCellValue("我是标题");for (int i = 0; i < list.size(); i++) {OrderVO vo = list.get(i);row = sheet.createRow(i+2); //从第三行开始//这里就可以使用sysUserMapper,做相应的操作//User user = excelUtils.sysUserMapper.selectByPrimaryKey(vo.getId());                  //根据excel模板格式写入数据....createRowAndCell(vo.getTaobaoOrderId(), row, cell, 0);createRowAndCell(vo.getOrderInfo(), row, cell, 1);createRowAndCell(vo.getLy(), row, cell, 2);createRowAndCell(format.format(DateFormat.getDateInstance().parse(vo.getCreateTime())), row, cell, 3);createRowAndCell(vo.getTotal(), row, cell, 4);createRowAndCell(getOrderSource(vo.getSourceId()), row, cell, 5);//.....}workbook.write(fos);fos.flush();fos.close();// 下载InputStream fis = new BufferedInputStream(new FileInputStream(newFile));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();response.reset();response.setContentType("text/html;charset=UTF-8");OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/x-msdownload");String newName = URLEncoder.encode("订单" + System.currentTimeMillis() + ".xlsx","UTF-8");response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");response.addHeader("Content-Length", "" + newFile.length());toClient.write(buffer);toClient.flush();} catch (Exception e) {e.printStackTrace();} finally {try {if (null != is) {is.close();}} catch (Exception e) {e.printStackTrace();}}}// 删除创建的新文件this.deleteFile(newFile);}/***根据当前row行,来创建index标记的列数,并赋值数据*/private void createRowAndCell(Object obj, XSSFRow row, XSSFCell cell, int index) {cell = row.getCell(index);if (cell == null) {cell = row.createCell(index);}if (obj != null)cell.setCellValue(obj.toString());else cell.setCellValue("");}/*** 复制文件* * @param s*            源文件* @param t*            复制到的新文件*/public void fileChannelCopy(File s, File t) {try {InputStream in = null;OutputStream out = null;try {in = new BufferedInputStream(new FileInputStream(s), 1024);out = new BufferedOutputStream(new FileOutputStream(t), 1024);byte[] buffer = new byte[1024];int len;while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);}} finally {if (null != in) {in.close();}if (null != out) {out.close();}}} catch (Exception e) {e.printStackTrace();}}/*** 读取excel模板,并复制到新文件中供写入和下载* * @return*/public File createNewFile(String tempPath, String rPath) {// 读取模板,并赋值到新文件************************************************************// 文件模板路径String path = (tempPath);File file = new File(path);// 保存文件的路径String realPath = rPath;// 新的文件名String newFileName = System.currentTimeMillis() + ".xlsx";// 判断路径是否存在File dir = new File(realPath);if (!dir.exists()) {dir.mkdirs();}// 写入到新的excelFile newFile = new File(realPath, newFileName);try {newFile.createNewFile();// 复制模板到新文件fileChannelCopy(file, newFile);} catch (Exception e) {e.printStackTrace();}return newFile;}/*** 下载成功后删除* * @param files*/private void deleteFile(File... files) {for (File file : files) {if (file.exists()) {file.delete();}}}
}

ExcelController

@Controller
@RequestMapping(value = "/export")
public class ExcelController {@RequestMapping(value = "/list")@ResponseBodypublic void excel(HttpServletRequest request, HttpServletResponse response) throws IOException {String tempPath = request.getSession().getServletContext().getRealPath("/") + "template/" + "order.xlsx";String path = request.getSession().getServletContext().getRealPath("/") + "template/";Map<String, Object> map = new HashMap<String, Object>();if (..) {//查询数据的条件map.put();}List<OrderVO> list = orderMapper.selectOrderByExcel(map);ExcelUtils ex = new ExcelUtils();ex.exportExcel(tempPath, path, response, list);}
}

java 根据excel模板格式导出指定格式的excel相关推荐

  1. java导出PDF、iText5导出漂亮表格PDF、导出指定格式水印PDF

    我们在项目当中经常要导出pdf文档,pdf文档还要按一定的格式导出,以下介绍导出pdf文档功能: 1)支持A4纸大小导出 2)指定文字显示的位置 3)支持表格展示数据 4)添加水印 开源下载 java ...

  2. Java 基于Excel模板实现导出Excel并插入数据

    [本案例基于spring boot架构] 一.需求介绍 给定一个Excel模板,在指定位置插入数据,然后将生成的excel导出. 二.实现方法介绍 1.准备Excel模板 模板中要插入数据的地方用&q ...

  3. POI读取Excel模板并导出大量数据

    POI读取Excel模板并导出大量数据 我在使用XSSFWorkbook读取Excel模板并导出大量数据(百万级)时,发现很长时间没有响应,debugger模式发现在读取第三四十万条数据时,程序直接停 ...

  4. springboot+poi导出指定格式Excel模板详解+Demo

    项目用的是springboot框架,非常的简单,只需导入项目即可运行,先看看效果: 资源下载地址: http://download.csdn.net/download/tjcyjd/9998721 具 ...

  5. Java导出Excel模板,导出数据到指定模板,通过模板导入数据(一)

    本文章主要是介绍阿里巴巴的easyexcel的使用 1. 首先需要我们导入easyexcel的依赖包 <!-- alibaba/easyexcel 使用高版本,低版本string接收数字丢小数位 ...

  6. java 导入excel表格(批量导入),下载excel模板,导出表格

    1.导入excel表格(批量导入) 如图,给id=83和id=84的老师导入工作时间. 导入的excel模板如下 注意导入excel表格时关于日期时间类的的数据要设置对应的格式. 后台接口 /*** ...

  7. 【开发随机】JAVA+POI+自定义注解+反射构建自定义工具类实现快捷简便的Excel模板化导出(附demo代码)

    220907更新 项目UAT期间,用户反映了一个问题,数据量稍大的情况下,会出现"从某一行开始,往下所有行设置的字体和字体大小不生效"的BUG. 经过排查,发现原因是:POI的XS ...

  8. Mongodb数据导出指定格式

    Mongo支持导出格式有csv.json.dat,其导入导出指定为mongoexport.mongoimport 数据库里数据如下: 导出步骤: 1.先进入命令行模式,cd到mongo安装的安装bin ...

  9. jxl实现写入excel模板及导出(带图片)

    在上一片博客中我用的是freemarker(操作简单),试了很多种方法就是没有把带图片的excel导出来,没办法就换jxl来实现.好了下面就开始进入正题: 第一步:pom.xml中添加jxl的依赖如下 ...

最新文章

  1. python 重复输出字符串
  2. TP5安装失败怎么办?
  3. java线程删除文件,线程“main”java.io.jgitinernalexception中的异常:无法删除临时文件c:\users\13 dec...
  4. python杂记-读取png图像文件头数据
  5. nginx 禁止通过IP,未绑定域名访问服务器
  6. vue项目接口地址暴露_vue组件暴露和.js文件暴露接口操作
  7. 他受他爸影响,他爸受数学家影响,最终造出了自动旋转的房子!
  8. linux从表中提取信息,如何用Linux命令提取表格文本中的某列
  9. selenium环境搭建,浏览器驱动安装
  10. Spring DeferredResult 实现长轮询
  11. 网站底部版权声明、备案编号、许可证信息应该怎么写?
  12. KrakenD网关V1.0.0文档初步翻译
  13. C#面向对象基础之String类的练习
  14. Windows 10 驱动开发 及 驱动签名
  15. OpenGL3D场景大作业制作
  16. Blender图解教程:高仿版超级马里奥(三)身体建模(5月7日更新 附模型下载)
  17. input框禁止输入空格
  18. 社交媒体2.0_社交媒体2.0:增强现实生活
  19. XigmaNAS(原 NAS4Free) 发布 11.2.0.4.6625 版本
  20. RabbitMQ消息队列工作原理及集成使用

热门文章

  1. table行的折叠和展开
  2. JS处理JSON字符串转数组,数字超17为处理方法
  3. 最新版本webrtc源代码在windows上的编译方法
  4. 基于图像识别的表格数据提取系统
  5. javascript--经典实例锦集
  6. 身份证校验和获取身份证信息的方法
  7. 戴尔linux改win7教程视频,戴尔笔记本win10改win7系统BIOS设置详细教程
  8. 3d可视化虚拟建模vr展示三维模型方案
  9. 短视频矩阵系统源码。抖音矩阵系统源码。抖音SEO源码。
  10. Dijkstra算法讲解(通过边实现松弛)