PHP和UniApp实现数据的Excel导入与导出

  1. 创建导入接口
    在PHP项目中,创建一个Excel导入接口,接收前端传递的Excel文件,并解析文件内容,将数据存入数据库或进行其他操作。
<?php
require 'vendor/autoload.php';
use PhpOfficePhpSpreadsheetIOFactory;

if ($_FILES['file']['error'] == 0) {
    $file = $_FILES['file']['tmp_name'];
    $reader = IOFactory::createReader('Xlsx');
    $spreadsheet = $reader->load($file);
    $worksheet = $spreadsheet->getActiveSheet();
    
    // 获取数据并处理
    $data = [];
    foreach ($worksheet->getRowIterator() as $row) {
        $rowData = [];
        foreach ($row->getCellIterator() as $cell) {
            $rowData[] = $cell->getValue();
        }
        $data[] = $rowData;
    }
    
    // 处理数据
    // ...
    
    // 返回处理结果
    echo json_encode([
        'status' => 1,
        'message' => '上传成功'
    ]);
} else {
    echo json_encode([
        'status' => 0,
        'message' => '上传失败'
    ]);
}
?>

2. 在UniApp中调用接口

在UniApp项目中,使用uni.request来调用上述接口,将Excel文件作为FormData上传到服务器。

export default {
    methods: {
        importExcel() {
            uni.chooseMessageFile({
                count: 1,
                success: (res) => {
                    const tempFilePath = res.tempFiles[0].path;
                    uni.uploadFile({
                        url: 'http://localhost/import.php',
                        filePath: tempFilePath,
                        name: 'file',
                        success: (res) => {
                            const data = JSON.parse(res.data);
                            if (data.status === 1) {
                                uni.showToast({
                                    title: '导入成功',
                                    icon: 'success'
                                });
                            } else {
                                uni.showToast({
                                    title: '导入失败',
                                    icon: 'none'
                                });
                            }
                        },
                        fail: () => {
                            uni.showToast({
                                title: '上传失败',
                                icon: 'none'
                            });
                        }
                    });
                }
            });
        }
    }
}

 通过UniApp实现数据的Excel导出

  1. 创建导出接口
    在PHP项目中,创建一个Excel导出接口,根据需要从数据库中获取数据,然后将数据导出为Excel文件供用户下载。
<?php
require 'vendor/autoload.php';
use PhpOfficePhpSpreadsheetSpreadsheet;
use PhpOfficePhpSpreadsheetWriterXlsx;

// 获取数据
$data = [
    ['name', 'age', 'gender'],
    ['Tom', 20, 'Male'],
    ['Lisa', 25, 'Female'],
    // ...
];

// 创建Excel文件
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray($data);

// 下载文件
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export.xlsx"');
$writer->save('php://output');
?>

2. 在UniApp中调用接口
在UniApp项目中,使用uni.downloadFile来下载导出的Excel文件。

export default {
    methods: {
        exportExcel() {
            uni.downloadFile({
                url: 'http://localhost/export.php',
                success: (res) => {
                    uni.saveFile({
                        tempFilePath: res.tempFilePath,
                        success: (res) => {
                            uni.showToast({
                                title: '导出成功',
                                icon: 'success'
                            });
                        }
                    });
                },
                fail: () => {
                    uni.showToast({
                        title: '导出失败',
                        icon: 'none'
                    });
                }
            });
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一些相关的参考信息。 1. 数据导出 首先需要引入jxl相关的jar包,然后创建一个Excel文件并设置列名和数据,最后将数据写入Excel文件中即可。 示例代码如下: ```java // 定义Excel文件名和工作表名 String fileName = "example.xls"; String sheetName = "Sheet1"; // 创建Excel文件 WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName)); WritableSheet sheet = workbook.createSheet(sheetName, 0); // 设置列名 Label label = new Label(0, 0, "姓名"); sheet.addCell(label); label = new Label(1, 0, "年龄"); sheet.addCell(label); // 设置数据 for (int i = 0; i < dataList.size(); i++) { Map<String, Object> data = dataList.get(i); label = new Label(0, i + 1, data.get("name").toString()); sheet.addCell(label); label = new Label(1, i + 1, data.get("age").toString()); sheet.addCell(label); } // 写入Excel文件 workbook.write(); workbook.close(); ``` 2. 数据导入 需要引入cos相关的jar包,然后读取Excel文件中的数据并进行处理。 示例代码如下: ```java // 定义Excel文件名和工作表名 String fileName = "example.xls"; String sheetName = "Sheet1"; // 读取Excel文件 Workbook workbook = Workbook.getWorkbook(new File(fileName)); Sheet sheet = workbook.getSheet(sheetName); // 获取数据 List<Map<String, Object>> dataList = new ArrayList<>(); for (int i = 1; i < sheet.getRows(); i++) { Map<String, Object> data = new HashMap<>(); data.put("name", sheet.getCell(0, i).getContents()); data.put("age", sheet.getCell(1, i).getContents()); dataList.add(data); } // 处理数据 for (int i = 0; i < dataList.size(); i++) { // TODO: 对数据进行处理或存储 } // 关闭Excel文件 workbook.close(); ``` 以上是一个基本的使用jxl实现Excel表格数据导入导出的示例代码,希望对您有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值