HSSFWorkbook导出数据到excel文件

HSSFWorkbook导出数据到excel文件

数据

/**
 * @describe: 数据提供
 * Create By Zheng
 */
public class DataProvider {

    public static String title = "手机报价表";

    public static List<String> headerList = new ArrayList<>();
    static {
        headerList.add("ID");
        headerList.add("品牌");
        headerList.add("型号");
        headerList.add("价格");
        headerList.add("更新日期");
        headerList.add("数量");
    }

    public static List<List<String>> dataList = new ArrayList<>();
    static {
        dataList.add(Arrays.asList(new String[]{"1","苹果","x","300","2020/10/10","5"}));
        dataList.add(Arrays.asList(new String[]{"2","苹果","xs","2000","2020/10/10","4"}));
        dataList.add(Arrays.asList(new String[]{"3","苹果","xs max","2000","2020/10/10","5"}));
        dataList.add(Arrays.asList(new String[]{"4","苹果","xr","2000","2020/10/10","53"}));
        dataList.add(Arrays.asList(new String[]{"5","苹果","11","3000","2020/10/10","51"}));
        dataList.add(Arrays.asList(new String[]{"6","苹果","11pro","3000","2020/10/10","51"}));
    }
}

代码

    @Test
    public void createExcelFile() throws IOException {
        HSSFWorkbook workbook = createWorkbook();
        // 创建本地文件
        FileOutputStream file = new FileOutputStream("C:\\Users\\huishe010\\Desktop\\excel导出.xlsx");
        BufferedOutputStream fileOutputStream = new BufferedOutputStream(file);
        workbook.write(fileOutputStream);
        if (fileOutputStream != null) {
         	fileOutputStream.close();
        }
    }


    /**
     * 创建工作簿,并在工作簿中添加内容
     * @return workbook 工作簿
     */
    public HSSFWorkbook createWorkbook() {
        // 1-声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();//工作簿
        HSSFSheet sheet = workbook.createSheet("手机报表");//excel表名
        sheet.setDefaultColumnWidth(20);//设置表格列宽度为20个字节
        // 2-数据准备
        String title = DataProvider.title;
        List<String> headerList = DataProvider.headerList;
        List<List<String>> dataList = DataProvider.dataList;
        int width = headerList.size();

        // 3-标题准备
        int row = 0;
        updateCellText(sheet, row ++, 0, title);
        CellRangeAddress region = new CellRangeAddress(0 , 0, 0, width - 1);// 合并标题单元格
        sheet.addMergedRegion(region);

        // 4-表头准备
        for (int i = 0; i < width; i++) {
            updateCellText(sheet, row, i, headerList.get(i));
        }
        row ++;

        // 6-表数据写入
        for (int i = 0; i < dataList.size(); i++) {
            for (int j = 0; j < dataList.get(i).size(); j++) {
                updateCellText(sheet, row + i, j, dataList.get(i).get(j));
            }
        }

        return workbook;
    }

    /**
     * 对工作簿对象-Excel-表格中进行内容的填充-仅展示文本Text内容
     * @param sheet 工作簿中的表格
     * @param rowIndex 行数 Starting from 0
     * @param cellIndex 列数 Starting from 0
     * @param content 单元格内容
     * Create By Zheng
     */
    private void updateCellText(HSSFSheet sheet, int rowIndex, int cellIndex, String content) {
        // 1-获取行对象
        HSSFRow row = sheet.getRow(rowIndex);
        if (row == null) {
            row = sheet.createRow(rowIndex);
        }
        // 2-创建单元格对象
        HSSFCell cell = row.createCell(cellIndex);
        // 3-单元格文本数据填充
        cell.setCellValue(new HSSFRichTextString(content));
    }

通过接口返回excel文件

	@GetMapping(value="/export")
    public void exportExcel(HttpServletResponse response) throws IOException, ServerSqlErrorException {

        HSSFWorkbook workbook = HSSFWorkbookDemo.createWorkbook(id);

        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");

        //这后面可以设置导出Excel的名称,此例中名为report.xls
        response.setHeader("Content-disposition", "attachment;filename=report.xlsx");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中,供页面下载
        workbook.write(response.getOutputStream());
    }

导出效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值