Java 简单导出数据列表到excel中
发布日期:2021-10-12 20:07:59
浏览次数:13
分类:技术文章
本文共 6522 字,大约阅读时间需要 21 分钟。
现在很多网页啊,app啊,为了提高用户体验度,方便用户清晰可以随时能够看到想要的数据都会添加一个导出数据功能,把想要的数据统一导出到一个excel中。我也是听一个朋友偶然说的,昨天就刷了刷各种微博,基本都说的差不多,主要靠自己动手,这不,终于搞出来了。先上图,瞅瞅。
进入正题
一,做这个导出数据必要的依赖(我这里是ssm的maven项目)
org.apache.poi poi 3.10-FINAL org.apache.poi poi-ooxml 3.10-FINAL org.apache.poi poi-ooxml-schemas 3.10-FINAL
二,需要的工具类
public class POIUtil { public static void Excel2003Operate(String filePath) throws Exception { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(new File(filePath))); HSSFSheet sheet = hssfWorkbook.getSheetAt(0); for (int i = 0; i < 10000; i++) { HSSFRow hssfRow = sheet.createRow(i); for (int j = 0; j < 10; j++) { HSSFCellUtil.createCell(hssfRow, j, String.valueOf(Math.random())); } } FileOutputStream out = new FileOutputStream("workbook.xlsx"); hssfWorkbook.write(out); out.close(); } public static void ExcelOperate(String filePath) throws Exception { Workbook workbook = WorkbookFactory.create(new FileInputStream(new File(filePath))); Sheet first = workbook.getSheetAt(0); for (int i = 0; i < 100000; i++) { Row row = first.createRow(i); for (int j = 0; j < 11; j++) { if(i == 0) { row.createCell(j).setCellValue("column" + j); } else { if (j == 0) { row.createCell(j).setCellValue(i); } else row.createCell(j).setCellValue(Math.random()); } } } FileOutputStream out = new FileOutputStream("workbook.xlsx"); workbook.write(out); out.close(); } public static void Excel2007AboveOperateOld(String filePath) throws IOException { XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filePath))); // 获取第一个表单 Sheet first = workbook.getSheetAt(0); for (int i = 0; i < 100000; i++) { Row row = first.createRow(i); for (int j = 0; j < 11; j++) { if(i == 0) { // 首行 row.createCell(j).setCellValue("column" + j); } else { // 数据 if (j == 0) { CellUtil.createCell(row, j, String.valueOf(i)); } else CellUtil.createCell(row, j, String.valueOf(Math.random())); } } } // 写入文件 FileOutputStream out = new FileOutputStream("workbook.xlsx"); workbook.write(out); out.close(); } /** * 测试写入百万条数据 * Cast time : 87782 * @param filePath 文件路径 * @throws IOException */ public static void Excel2007AboveOperate(String filePath) throws IOException { XSSFWorkbook workbook1 = new XSSFWorkbook(new FileInputStream(new File(filePath))); SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100); Sheet first = sxssfWorkbook.getSheetAt(0); for (int i = 0; i < 10000; i++) { Row row = first.createRow(i); for (int j = 0; j < 11; j++) { if(i == 0) { // 首行 row.createCell(j).setCellValue("column" + j); } else { // 数据 if (j == 0) { CellUtil.createCell(row, j, String.valueOf(i)); } else CellUtil.createCell(row, j, String.valueOf(Math.random())); } } } FileOutputStream out = new FileOutputStream(filePath); sxssfWorkbook.write(out); out.close(); } public static void MathRandomCastTime() { long beginTime = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { Math.random(); } long endTime = System.currentTimeMillis(); }}
我在这里只用了其中一个方法,没引用这个类,贴出来看谁能用就用吧。(这个工具类也是别人的,不知道是谁的了,首先谢谢了,很实用)
三,通过js一个按钮导出数据
function exportList() { $.ajax({ url: "/user/system/exportList", dataType: "text", type: "post", cache: false, success: function(obj) { if (obj == "success") { layer.alert('导出成功!', function(index) { window.location.reload(); }); } else { layer.alert("导出失败!"); } }, error: function(textStatus, e) { layer.alert("系统ajax交互错误: "); } });}
后台处理
@RequestMapping( value = "exportList", method = RequestMethod.POST )@ResponseBodypublic String exportList( String cpage ) throws IOException{ long beginTime = System.currentTimeMillis(); String filePath = "d://test.xlsx"; //excel存放的位置 XSSFWorkbook workbook = new XSSFWorkbook( new FileInputStream( new File( filePath ) ) ); SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook( workbook, 100 ); Sheet first = sxssfWorkbook.getSheetAt( 0 ); try { ListdList = drugService.queryDruglist(); for ( int i = 0; i < dList.size(); i++ ) { Row row = first.createRow( i ); for ( int j = 0; j < 10; j++ ) { if ( i == 0 ) { /* 首行 */ row.createCell( j ).setCellValue( "标题" + j ); } else { /* 数据 */ if ( j == 0 ) //第一列 { CellUtil.createCell( row, j, String.valueOf( i ) ); }else if ( j == 1 )//第二列 { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getDrugs_id() ) ); }else if ( j == 2 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getPro_num() ) ); }else if ( j == 3 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getDrugs_name() ) ); }else if ( j == 4 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getFactory() ) ); }else if ( j == 5 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getHa_level() ) ); }else if ( j == 6 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getPack() ) ); }else if ( j == 7 ) { CellUtil.createCell( row, j, String.valueOf( dList.get( i ).getFactory() ) ); } } } } FileOutputStream out = new FileOutputStream( filePath ); sxssfWorkbook.write( out ); out.close(); long endTime = System.currentTimeMillis(); return("success"); } catch ( Exception e ) { (); } return("fail");}
可以尝试一下。
转载地址:https://blog.csdn.net/qq_39772439/article/details/90766939 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月25日 06时40分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java 的Swing 之JFrame快速入门
2021-06-29
Android Studio创建shapeDrawable的方法
2021-06-29
Mybatis快速入门(4)Mybatis与Spring整合(增删改查)以及逆向工程
2021-06-29
计算机二级C语言:大题程序修改题
2021-06-29
Android Studio 安卓手机上实现火柴人动画(Java源代码—Python)
2021-06-29
SpringMVC快速入门(2)商品列表的加载
2021-06-29
SpringMVC快速入门(3)默认组件加载
2021-06-29
Java 解决SpringMVC的post请求乱码的问题
2021-06-29
Java SSM 项目实战 day03 功能介绍,订单的操作,订单的增删改查
2021-06-29
Android Studio 实现登录注册-源代码 (连接MySql数据库)
2021-06-29
C/C++语言数据结构快速入门(一)(代码解析+内容解析)数据结构基本内容和线性表
2021-06-29
Java SSM 项目实战 day05 用户操作
2021-06-29