java 实现execl导出 我应该怎么实现。希望是细致的说法,有完整的实例更好
阿神
阿神 2017-04-18 10:23:33
[Java讨论组]

我现在要做一个execl的导出,所用的.jar我已经弄好了,现在我不知道要怎么实现这个功能,还请细致回答。不要凑数的评论。谢谢大家。
网上的例子都是 半截拉快的 实在不知从哪开始。

我点击按钮之后,到后台,然后怎么弄。。。excel里面的列名字都是自己根据需要写死的吗,还是怎么弄,需要展现的数据,是在先查 还是已经存在一个集合里面直接循环出来了,,,

阿神
阿神

闭关修行中......

全部回复(3)
高洛峰

poi版本。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>

Hello World 示例

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellUtil;

/**
 * @author Kevin Zou (kevinz@weghst.com)
 */
public class HelloWorld {

    public static void main(String[] args) throws Exception {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("HELLO");
        Map<String, Object> properties = new HashMap<>();

        // border around a cell
        properties.put(CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM);
        properties.put(CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM);
        properties.put(CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM);
        properties.put(CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM);
        // Give it a color (RED)
        properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex());
        properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
        properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex());
        properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());

        // Apply the borders to the cell at B2
        Row row = sheet.createRow(1);
        Cell cell = row.createCell(1);

        for (Map.Entry<String, Object> e : properties.entrySet()) {
            CellUtil.setCellStyleProperty(cell, workbook, e.getKey(), e.getValue());
        }
        cell.setCellValue("First"); // 单元格值

        // Apply the borders to a 3x3 region starting at D4
        for (int ix = 3; ix <= 5; ix++) {
            row = sheet.createRow(ix);
            for (int iy = 3; iy <= 5; iy++) {
                cell = row.createCell(iy);
                for (Map.Entry<String, Object> e : properties.entrySet()) {
                    CellUtil.setCellStyleProperty(cell, workbook, e.getKey(), e.getValue());
                }

                cell.setCellValue(ix + " * " + iy); // 单元格值
            }
        }

        FileOutputStream fileOut = new FileOutputStream("C:/helloworld.xls");
        workbook.write(fileOut);
        fileOut.close();
        System.out.println("The end.");
    }
}

poi Quick Guide

巴扎黑

直接课程,够细致了吧

PHP中文网

我现在都是JS导出(Chrome),不过有一个利用Java反射导出Excel的工具类,分享给题主~

Java POI 导出EXCEL经典实现 Java导出ExceL

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号