关于开发HarmonyOS 鸿蒙Next应用下载xlsx文件

关于开发HarmonyOS 鸿蒙Next应用下载xlsx文件 大佬们好,我想请问下鸿蒙开发中,我需要将TextField中的内容存入xlsx,并且下载到本地文件

我使用的开发语言是Java

我现在有以下情况:

  1. 官方示例中的simple》Download示例的是public Builder(Context context, Uri uri),我的并没有网页uri

  2. 当我使用FileOutputStream fos = new FileOutputStream(excelPath);

    生成默认地址excelPath/data/data/com.example.try6_two/MainAbility/preferences/test/test.xlsx

    (即使用Context context = getContext();)时,

    可以成功应用FileOutputStream fos = new FileOutputStream(excelPath)并通过代码打印excel中数据,但无法在鸿蒙系统平板文件管理中找到文件

  3. 当我使用FileOutputStream fos = new FileOutputStream(excelPath);

    地址excelPath我的平板/me/test.xlsx(即平板中文件储存的格式)

    地址excelPath/entry/resources/rawfile/test.xlsx

    均无法执行FileOutputStream fos = new FileOutputStream(excelPath) 且无法在鸿蒙系统平板文件管理中找到文件

希望得到各位大佬指点!!!!


更多关于关于开发HarmonyOS 鸿蒙Next应用下载xlsx文件的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复
这是我用来保存错误日志的代码,我保存的是txt文档,你可以参考下

```java
public static void saveErrorLog(Throwable throwable) {
    File externalFilesDirs = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
    if (externalFilesDirs.isDirectory()) {
        File[] listFiles = externalFilesDirs.listFiles();
        if (listFiles.length > 10) {
            int length = listFiles.length - 10;
            for (int i = 0; i < length; i++) {
                boolean delete = listFiles[i].delete();
            }
        }
    }
    File file = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
            TimeConversionUtils.GetCurrentTime() + ".txt");
    try {
        PrintWriter printWriter = new PrintWriter(file);
        printWriter.print("品牌:" + ReflexesUtil.getBrand() + "\n");
        printWriter.print("系统版本:" + ReflexesUtil.getVersion() + "\n");
        printWriter.print("API版本:" + SystemVersion.getApiVersion() + "\n");
        printWriter.print("芯片型号:" + ReflexesUtil.getHardware() + "\n\r");
        throwable.printStackTrace(printWriter);
        printWriter.flush();
        printWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

更多关于关于开发HarmonyOS 鸿蒙Next应用下载xlsx文件的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


大佬,我把我的代码放在下面,请您看一下是哪里的问题,

大佬,我把我的代码放在下面,请您看一下是哪里的问题,

姓名: 张三
职业: 软件工程师
简介: 拥有超过十年的软件开发经验,熟悉多种编程语言和技术。

建议你使用这个路径getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)

/data/data/com.example.try6_two/MainAbility/preferences/test这个路径我们好像不能操作
使用

```perl
#String mSDCardFolderPath = "我的平板";
#String mSDCardFolderPath ="/entry/resources/rawfile";
#String mSDCardFolderPath = "/data/data/com.example.try6_two/MainAbility/preferences/test";

#String excelPath = "我的平板/test.xlsx";
#String excelPath = "/entry/resources/rawfile/test.xlsx";
#String excelPath = "/data/data/com.example.try6_two/MainAbility/preferences/test/test.xlsx";

这三种配对打印的结果都是dir.mkdir();chuang’jian’shi’bai

09-21 19:37:47.601 9930-9930/com.example.try6_two I System.out: ----------------1

09-21 19:37:47.869 9930-9930/com.example.try6_two I System.out: ----------------2

09-21 19:37:47.937 9930-9930/com.example.try6_two I System.out: ----------------3

09-21 19:37:47.937 9930-9930/com.example.try6_two I System.out: ----------------excelPath1>>> /data/data/com.example.try6_two/MainAbility/preferences/test/test.txt

09-21 19:37:47.937 9930-9930/com.example.try6_two I System.out: ----------------不存在则创建false

but1_2.setClickedListener(component -> { HiLog.info(label, “clicked”); System.out.println("----------------1");

Workbook book = new XSSFWorkbook(); //创建工作簿

System.out.println("----------------2");
Sheet sheet = book.createSheet(); //创建工作表
String[] title = {"列1","列2","列3","列4","列5"};

Row row = sheet.createRow(0); //创建行

int colNum = title.length;
for (int i = 0; i < colNum; i++) {
    sheet.setColumnWidth(i, 20 * 256);
    Cell cell1 = row.createCell(i);
    cell1.setCellValue(title[i]);
}
row = sheet.createRow(1);
row.setHeightInPoints(28f);

//创建单元格
Cell cell1 = row.createCell(0);
cell1.setCellValue("my1");
Cell cell2 = row.createCell(1);
cell2.setCellValue("my2");
Cell cell3 = row.createCell(2);
cell3.setCellValue("my3");
Cell cell4 = row.createCell(3);
cell4.setCellValue("my4");
Cell cell5 = row.createCell(4);
cell5.setCellValue("my5");
System.out.println("----------------3");

Context context = getContext(); // 数据文件存储路径:/data/data/{PackageName}/{AbilityName}/preferences。

//System.out.println("----------------4>>>"+context);

String mSDCardFolderPath = context.getPreferencesDir() + "/test";
//String mSDCardFolderPath = "我的平板";
//String mSDCardFolderPath ="/entry/resources/rawfile";
//String mSDCardFolderPath = "/data/data/com.example.try6_two/MainAbility/preferences/test";
File dir = new File(mSDCardFolderPath);

String excelPath = mSDCardFolderPath+"/test.xlsx";
//String excelPath = "我的平板/test.xlsx";
//String excelPath = "/entry/resources/rawfile/test.xlsx";
//String excelPath = "/data/data/com.example.try6_two/MainAbility/preferences/test/test.xlsx";

System.out.println("----------------excelPath1>>>"+excelPath);
//   /data/data/com.example.try6_two/MainAbility/preferences/test/test.xlsx
//判断文件是否存在
if (!dir.isFile()) {
    //不存在则创建
    dir.mkdir();
    System.out.println("----------------不存在则创建"+dir.isFile());
}
try {
    // 打开excel文件
    FileOutputStream fos = new FileOutputStream(excelPath);
    System.out.println("----------------one");
    book.write(fos);
    System.out.println("----------------two");
    fos.flush();
    System.out.println("----------------three");
    fos.close();
    System.out.println("----------------four");
    book.close();
    System.out.println("------------写入成功");
}
catch(Exception ex){
    ex.printStackTrace();
}

try {
    // 打开excel文件
    Workbook wb = new XSSFWorkbook(excelPath);
    System.out.println("----------------excelPath>>>"+excelPath);
    int sheetSize = wb.getNumberOfSheets();
    if (sheetSize == 0) {
        HiLog.info(label, "Excel中没有工作表");
        System.out.println("----------------Excel中没有工作表");
    }
    // 获取excel的第一个sheet
    Sheet readSheet = wb.getSheetAt(0);
    int rowTotal = readSheet.getLastRowNum();
    // 遍历单元格获取数据
    for (int i = 0; i < rowTotal; i++) {
        Row tempRow = readSheet.getRow(i);
        for (int j = 0; j < tempRow.getLastCellNum(); j++) {
            String content = tempRow.getCell(j).getStringCellValue();
            HiLog.info(label, content);
            System.out.println("----------------Excel中>>>" + content);
        }
    }
} catch (Exception e) {
    HiLog.error(label, "Exception" + e);
}

});

在开发HarmonyOS(鸿蒙)Next应用时,若需要实现下载xlsx文件的功能,可以通过以下方式实现:

HarmonyOS提供了丰富的API用于网络请求和文件操作。首先,你可以使用HttpURLConnectionOkHttp等网络库来发起HTTP GET请求,从服务器下载xlsx文件。在请求成功后,将响应流中的数据保存到本地文件中。

具体步骤如下:

  1. 发起网络请求:使用HttpURLConnection或第三方网络库(如OkHttp)来发送HTTP GET请求到xlsx文件的URL。

  2. 读取响应数据:在请求成功后,从响应流中读取xlsx文件的二进制数据。

  3. 保存文件:将读取到的二进制数据写入到本地文件中,注意指定文件路径和文件名为.xlsx后缀。

  4. 处理文件权限:确保你的应用有权限在指定的存储位置写入文件。在HarmonyOS中,你可能需要在config.json中声明相关权限,并在运行时请求用户授权。

示例代码(伪代码,具体实现需根据HarmonyOS SDK文档调整):

// 伪代码,具体实现需根据HarmonyOS SDK调整,此处不展示具体代码实现
// 1. 发起网络请求获取xlsx文件
// 2. 读取响应数据
// 3. 将数据写入本地文件,指定.xlsx后缀
// 4. 处理文件存储权限
``

如果问题依旧没法解决请联系官网客服,官网地址是:[https://www.itying.com/category-93-b0.html](https://www.itying.com/category-93-b0.html)
回到顶部