java 实现execl导出 我应该怎么实现。希望是细致的说法,有完整的实例更好

查看:87
本文介绍了java 实现execl导出 我应该怎么实现。希望是细致的说法,有完整的实例更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

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

解决方案

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

这篇关于java 实现execl导出 我应该怎么实现。希望是细致的说法,有完整的实例更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆