android实战项目开发七---完美解决excel文件导出功能,并分享到微信

因项目需求,需在Android中导出Excel文件并分享给微信好友。查询网上诸多方案,调试时均存在问题,文中给出关键代码。

由于近期项目的需求,需要导出excel文件并分享给微信好友。基本上查询网上好多方案,单都没解决到位,调试的时候都有问题。

关键代码

private void exportExcel() {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        //设置列宽
        sheet.setColumnWidth(0,2000);
        sheet.setColumnWidth(1,3000);
        sheet.setColumnWidth(2,3000);
        sheet.setColumnWidth(3,3000);
        sheet.setColumnWidth(4,5000);
        sheet.setColumnWidth(5,7000);
        //=================================定义表头属性===============================================
        Font font = workbook.createFont(); // 生成字体格式设置对象
        font.setFontName("黑体"); // 设置字体黑体
        font.setBold(true); // 字体加粗
        font.setFontHeightInPoints(( short ) 16 ); // 设置字体大小
        font.setColor(Font.COLOR_NORMAL);//字体颜色

        CellStyle cellStyle = workbook.createCellStyle(); // 生成行格式设置对象
        cellStyle.setBorderBottom(BorderStyle.THIN);// 下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyle.setBorderRight(BorderStyle.THIN);// 右边框
        cellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyle.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
        cellStyle.setFont(font);
        //=================================定义内容属性===============================================
        Font txtContent = workbook.createFont(); // 生成字体格式设置对象
        txtContent.setFontName("黑体"); // 设置字体黑体
        txtContent.setBold(false); // 字体加粗
        txtContent.setFontHeightInPoints(( short ) 12 ); // 设置字体大小
        txtContent.setColor(Font.COLOR_NORMAL);//字体颜色

        CellStyle cellStyleContent = workbook.createCellStyle(); // 生成行格式设置对象
        cellStyleContent.setBorderBottom(BorderStyle.THIN);// 下边框
        cellStyleContent.setBorderLeft(BorderStyle.THIN);// 左边框
        cellStyleContent.setBorderRight(BorderStyle.THIN);// 右边框
        cellStyleContent.setBorderTop(BorderStyle.THIN);// 上边框
        cellStyleContent.setAlignment(HorizontalAlignment.CENTER); // 横向居中对齐
        cellStyleContent.setVerticalAlignment(VerticalAlignment.CENTER); // 纵向居中对齐
        cellStyleContent.setFont(txtContent);

        //Font font = workbook.createFont();
        font.setBold(true);

        // 创建CellStyle并设置字体和对齐方式
        CellStyle style = workbook.createCellStyle();
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        for (int k=0;k<list.size()+1;k++) {
            Row row = sheet.createRow(k);
            if (k==0){
                Cell cell0=row.createCell(0);
                Cell cell1=row.createCell(1);
                Cell cell2=row.createCell(2);
                Cell cell3=row.createCell(3);
                Cell cell4=row.createCell(4);

                row.setHeight((short) 500);
                cell0.setCellValue("姓名");
                cell1.setCellValue("类型");
                cell2.setCellValue("事由");
                cell3.setCellValue("金额");
                cell4.setCellValue("日期");

                cell0.setCellStyle(cellStyle);
                cell1.setCellStyle(cellStyle);
                cell2.setCellStyle(cellStyle);
                cell3.setCellStyle(cellStyle);
                cell4.setCellStyle(cellStyle);

            }else{
                Cell cell0=row.createCell(0);
                Cell cell1=row.createCell(1);
                Cell cell2=row.createCell(2);
                Cell cell3=row.createCell(3);
                Cell cell4=row.createCell(4);


                row.setHeight((short) 500);
                cell0.setCellValue(list.get(k-1).getName());
                cell1.setCellValue(list.get(k-1).getType());
                cell2.setCellValue(list.get(k-1).getEvent());
                cell3.setCellValue(list.get(k-1).getAmount());
                cell4.setCellValue(list.get(k-1).getRemembertime());

                cell0.setCellStyle(cellStyleContent);
                cell1.setCellStyle(cellStyleContent);
                cell2.setCellStyle(cellStyleContent);
                cell3.setCellStyle(cellStyleContent);
                cell4.setCellStyle(cellStyleContent);

            }
        }

        try {
            String fileName = Excel_File_Name;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
                values.put(MediaStore.Downloads.MIME_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                values.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);

                Uri uri = getActivity().getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
                if (uri != null) {
                    OutputStream outputStream = getActivity().getContentResolver().openOutputStream(uri);
                    if (outputStream != null) {
                        workbook.write(outputStream);
                        outputStream.close();
                        //Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
                        ToastUtils.getInstance().showToast("导出成功");

                        // 分享Excel文件到微信
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SEND);
                        intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
                        intent.putExtra(Intent.EXTRA_STREAM, uri); // 设置文件路径为Uri
                        startActivity(intent); // 启动分享意图
                    }
                }
            } else {
                String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/" + fileName;
                OutputStream outputStream = new FileOutputStream(filePath);
                workbook.write(outputStream);
                outputStream.close();
                //Toast.makeText(getActivity(), "导出成功", Toast.LENGTH_SHORT).show();
                ToastUtils.getInstance().showToast("导出成功");

                // 分享Excel文件到微信
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("application/vnd.ms-excel"); // 设置文件类型为Excel
                intent.putExtra(Intent.EXTRA_STREAM, filePath); // 设置文件路径为Uri
                startActivity(intent); // 启动分享意图
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值