首页 > 编程语言 >【Java】轻量Excel读写框架学习

【Java】轻量Excel读写框架学习

时间:2024-04-05 17:33:40浏览次数:28  
标签:Java type Excel Label cell new sheet 轻量 Empty

 

参考黑马教程:

https://www.bilibili.com/video/BV1eA41157va?p=5

写着写着发现有更好的Demo案例:

https://cloud.tencent.com/developer/article/1966065

  

所需依赖:

<dependency>
    <groupId>org.apache.tika</groupId>
    <artifactId>tika-core</artifactId>
    <version>1.27</version> <!-- 请检查并使用最新版本 -->
</dependency>

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

 

Excel输出API常见方法:

@SneakyThrows
public void downloadXlsByJxl(HttpServletResponse response) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    ServletOutputStream outputStream = response.getOutputStream();


    /* 基于输出流创建一个新的工作簿 */
    WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
    WritableSheet sheet = workbook.createSheet("第一个工作表", 0);

    /* 创建标题行 */
    List<String> titleList = Arrays.asList("编号", "姓名", "手机号", "入职日期", "现在地址");

    /* 创建列宽配置 */
    int standardCharWidth = 1;
    int chineseCharWidth = 3;
    List<Integer> columnWidthSets = Arrays.asList(
            standardCharWidth * chineseCharWidth * 2,
            standardCharWidth * chineseCharWidth * 3,
            standardCharWidth * standardCharWidth * 12,
            standardCharWidth * 12,
            standardCharWidth * standardCharWidth * 20);

    Label eachLabel = null;
    for (int columnIdx = 0; columnIdx < titleList.size(); columnIdx++) {
        String title = titleList.get(columnIdx);
        eachLabel = new Label(columnIdx, 0, title);
        sheet.addCell(eachLabel);

        /* 列宽设置, 列下表, 一个标准字母的宽度 */
        sheet.setColumnView(columnIdx, columnWidthSets.get(columnIdx));
    }

    List<User> allUserList = userMapper.selectAll();
    for (int recordIdx = 0, rowIdx = 1; recordIdx < allUserList.size(); recordIdx ++, rowIdx ++) {
        User user = allUserList.get(recordIdx);
        eachLabel = new Label(0, rowIdx, String.valueOf(user.getId()));
        sheet.addCell(eachLabel);

        eachLabel = new Label(1, rowIdx, user.getUserName());
        sheet.addCell(eachLabel);

        eachLabel = new Label(2, rowIdx, user.getPhone());
        sheet.addCell(eachLabel);

        eachLabel = new Label(3, rowIdx, sdf.format(user.getHireDate()));
        sheet.addCell(eachLabel);

        eachLabel = new Label(4, rowIdx, user.getAddress());
        sheet.addCell(eachLabel);
    }


    /* 写入图片  非图片文件无法写入 Warning:  Image type doc not supported.  Supported types are png */
    File file = new File("C:\\Users\\Administrator\\Pictures\\AngelLegionPhoto\\Girl_20240303155025.png");
    WritableImage writableImage = new WritableImage(10, 10, 1, 1, file);
    sheet.addImage(writableImage);

    /* 写入链接 */
    WritableHyperlink writableHyperlink = new WritableHyperlink(10, 11, new URL("https://www.bilibili.com/video/BV1eA41157va"));
    sheet.addHyperlink(writableHyperlink);
    writableHyperlink = new WritableHyperlink(10, 12, new File("D:\\迅雷下载\\20121015120541355.doc"));
    sheet.addHyperlink(writableHyperlink);

    String fileName = "JXL-Demo.xls";
    response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), StandardCharsets.ISO_8859_1));
    response.setContentType("application/vnd.ms-excel");

    workbook.write();
    workbook.close();
    outputStream.close();
}

 

输出结果:

 

 

Excel读取常见API:

获取存在记录的单元格的边界列和行

能满足图片,公式,普通值的获取

@SneakyThrows
public void readXlsByJxl(MultipartFile multipartFile) {
    InputStream inputStream = multipartFile.getInputStream();
    Workbook workbook = Workbook.getWorkbook(inputStream);
    Sheet sheet = workbook.getSheet(0);
    int[] columnPageBreaks = sheet.getColumnPageBreaks();
    int[] rowPageBreaks = sheet.getRowPageBreaks();
    int columns = sheet.getColumns();
    int rows = sheet.getRows();
    int numberOfImages = sheet.getNumberOfImages();
    Hyperlink[] hyperlinks = sheet.getHyperlinks();
    log.info("columnPageBreaks {}", Arrays.toString(columnPageBreaks));
    log.info("rowPageBreaks {}", Arrays.toString(rowPageBreaks));
    log.info("columns {}", columns);
    log.info("rows {}", rows);
    log.info("numberOfImages {}", numberOfImages);
    for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
        StringBuilder currentRow = new StringBuilder();
        for (int colIdx = 0; colIdx < columns; colIdx++) {
            Cell cell = sheet.getCell(colIdx, rowIdx);
            CellType type = cell.getType();
            String contents = cell.getContents();
            currentRow.append("type ").append(type.toString()).append(" ,").append(contents).append(" |");
        }
        log.info("cell -> {}", currentRow);
    }
    /* 读取图片 */
    if (0 != numberOfImages) {
        for (int i = 0; i < numberOfImages; i++) {
            Image image = sheet.getDrawing(i);
            File imageFile = image.getImageFile();
            String name = imageFile.getName();
            double column = image.getColumn();
            double row = image.getRow();
            byte[] imageData = image.getImageData();
            Tika tika = new Tika();
            /* 图片是准确的mimeType, 非图片只能识别为octStream */
            String detect = tika.detect(imageData);
            log.info("image -> idx {}, {} - {}, {}, {}", i, (int) column, (int) row, name, detect);
        }
    }
    for (Hyperlink hyperlink : hyperlinks) {
        boolean isFile = hyperlink.isFile();
        boolean isLocation = hyperlink.isLocation();
        boolean isUrl = hyperlink.isURL();
        log.info("isFile {}, isLocation {}, isUrl {}", isFile, isLocation, isUrl);
        String path = isFile ? hyperlink.getFile().getPath() : isUrl ? hyperlink.getURL().getPath() : "internalLocation";
        int column = hyperlink.getColumn();
        int row = hyperlink.getRow();
        log.info("hyperlink -> {}, {}", column + "," + row, path);
    }
}

 

样例输出

columnPageBreaks null
rowPageBreaks null
columns 11
rows 16
numberOfImages 1
cell -> type Label ,编号 |type Label ,姓名 |type Label ,手机号 |type Label ,入职日期 |type Label ,现在地址 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,1 |type Label ,大一 |type Label ,13800000001 |type Label ,2001-01-01 |type Label ,北京市西城区宣武大街1号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,2 |type Label ,不二 |type Label ,13800000002 |type Label ,2002-01-02 |type Label ,北京市西城区宣武大街2号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,3 |type Label ,张三 |type Label ,13800000003 |type Label ,2003-03-03 |type Label ,北京市西城区宣武大街3号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,4 |type Label ,李四 |type Label ,13800000004 |type Label ,2004-02-04 |type Label ,北京市西城区宣武大街4号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,5 |type Label ,王五 |type Label ,13800000005 |type Label ,2005-03-05 |type Label ,北京市西城区宣武大街5号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,6 |type Label ,赵六 |type Label ,13800000006 |type Label ,2006-04-06 |type Label ,北京市西城区宣武大街6号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,7 |type Label ,沈七 |type Label ,13800000007 |type Label ,2007-06-07 |type Label ,北京市西城区宣武大街7号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,8 |type Label ,酒八 |type Label ,13800000008 |type Label ,2008-07-08 |type Label ,北京市西城区宣武大街8号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,9 |type Label ,第九 |type Label ,13800000009 |type Label ,2009-03-09 |type Label ,北京市西城区宣武大街9号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,10 |type Label ,石十 |type Label ,13800000010 |type Label ,2010-07-10 |type Label ,北京市西城区宣武大街10号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,11 |type Label ,肖十一 |type Label ,13800000011 |type Label ,2011-12-11 |type Label ,北京市西城区宣武大街11号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Label ,https://www.bilibili.com/video/BV1eA41157va |
cell -> type Label ,12 |type Label ,星十二 |type Label ,13800000012 |type Label ,2012-05-12 |type Label ,北京市西城区宣武大街12号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Label ,D:\迅雷下载\20121015120541355.doc |
cell -> type Label ,13 |type Label ,钗十三 |type Label ,13800000013 |type Label ,2013-06-13 |type Label ,北京市西城区宣武大街13号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,14 |type Label ,贾十四 |type Label ,13800000014 |type Label ,2014-06-14 |type Label ,北京市西城区宣武大街14号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
cell -> type Label ,15 |type Label ,甄世武 |type Label ,13800000015 |type Label ,2015-06-15 |type Label ,北京市西城区宣武大街15号院 |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |type Empty , |
image -> idx 0, 10 - 9, Girl_20240303155025.png, image/png
isFile false, isLocation false, isUrl true
hyperlink -> 10,11, /video/BV1eA41157va
isFile true, isLocation false, isUrl false
hyperlink -> 10,12, D:\迅雷下载\2012

  

 

标签:Java,type,Excel,Label,cell,new,sheet,轻量,Empty
From: https://www.cnblogs.com/mindzone/p/18115970

相关文章

  • JAVA学习-类型信息.空对象
        空对象(NullObject)是一种特殊的对象,它表示一个不引用任何实际对象的对象。在Java中,空对象通常用null表示,它可以赋值给引用类型的变量。空对象在编程中具有一些特点和使用方法,与其他对象进行比较和高级应用。一、特点:1.空对象没有实际的内容或状态,它不指向任何......
  • JAVA学习-类型信息.instanceof与Class的等价性
    Java中的类型信息主要通过instanceof和Class来实现。1.instanceof:instanceof是Java关键字,用于判断某个对象是否是指定类型或其子类型的实例。它的语法如下:objectinstanceoftype其中object表示要判断的对象,type表示要判断的类型。如果object是type类型或其子类型的实例,则......
  • 【附源码】计算机毕业设计招投标管理系统(java+springboot+mysql+mybatis+论文)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义在建筑、工程及众多行业领域,招投标活动是获取项目和签订合同的关键环节。一个高效的招投标管理系统能够帮助企业规范招投标流程,提高文档处理效率,确保信息透明公正,......
  • 【附源码】计算机毕业设计在线音乐播放平台(java+springboot+mysql+mybatis+论文)
    本系统(程序+源码)带文档lw万字以上  文末可领取本课题的JAVA源码参考系统程序文件列表系统的选题背景和意义在线音乐播放平台随着互联网技术的发展和数字媒体的普及逐渐成为人们获取音乐的主要途径。这类平台不仅为用户提供了便捷的音乐收听体验,还推动了音乐产业的新商业......
  • java程序写法展示
    importjava.util.Scanner;publicclasstest2{  publicstaticvoidmain(String[]args){   Scannersc=newScanner(System.in);  System.out.println("请输入一个整数:");  intn=sc.nextInt();  System.out.println("n="+n);     ......
  • 基于java斗车交易系统设计与实现—文档
    论文主要是对斗车交易系统进行了介绍,包括研究的现状,还有涉及的开发背景,然后还对系统的设计目标进行了论述,还有系统的需求,以及整个的设计方案,对系统的设计以及实现,也都论述的比较细致,最后对斗车交易系统进行了一些具体测试。正是针对上述问题,本论文对线上汽车信息管理系统的开......
  • 《架构风清扬-Java面试系列第13讲》说一说Java对象在内存中的生命周期
    大家好,加个餐!像线程的生命周期,Servlet的生命周期,相信这类问题大家都非常熟悉了Java对象在内存中的生命周期,这个题目倒是有些新鲜来,思考片刻,说出你的答案(PS:上图缓冲)Java对象在其内存中的生命周期可以被划分为多个阶段,下面钊哥逐个给大家说一说1,创建阶段(Creation......
  • 【Java后端开发】教程及案例
    Java后端开发是软件开发中的一个重要领域,它涉及到服务器端的程序设计、数据库交互、API开发、安全性处理等多个方面。以下是关于Java后端开发的教程和案例的详细介绍:###Java后端开发基础####1.Java基础知识-掌握Java基础语法、面向对象编程、集合框架、异常处理等。-......
  • 黑马程序员Java从入门到起飞(上) P103 字符串-08-较难练习练习-金额转换
    文章目录标题:黑马程序员Java从入门到起飞(上)P103字符串-08-较难练习练习-金额转换前言一、案例的使用场景是什么?二、输入输出情况三、思路四、什么是查表法?五、代码实现六、完整代码总结标题:黑马程序员Java从入门到起飞(上)P103字符串-08-较难练习练习-金额转......
  • JavaScript不建议直接声明undefined的原因,极致的严谨和仔细
    前言:很多人写JavaScript代码时习惯用vara=undefined这种写法,其实这种写在某种情况下会产生意想不到的bug,又或者说undefined本身就是JavaScript的一个bug??为什么这么说呢,因为undefined它应该是关健字才合理的,但undefined在JavaScript里面压根就不是关健字,它是window......