opencsv_5456:基于 OpenHarmony 的 CSV 文件处理库项目

用户可快速在 OpenHarmony 应用中实现 CSV 文件的读写操作。支持单条/多条数据写入、读取所有数据及指定编码读取,提供简洁接口,便于表格数据的纯文本形式处理与存储。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
2 years ago
9 months ago
2 years ago
9 months ago
2 years ago
9 months ago
4 years ago
2 years ago
2 years ago
9 months ago
9 months ago
11 months ago
2 years ago
2 years ago
2 years ago
9 months ago

OpenCSV

Introduction

Comma-Separated Values (CSV) files are commonly used in websites. OpenCSV provides APIs to access CSV files.

A CSV file stores table data in plain text. It is a character sequence that consists of records separated by a newline character. Each record consists of fields, which are separated by characters or strings. All records have the same sequence of fields, that is, a structured table in plain text. You can open a CSV file in text, Excel, or any other editor like text.

How to Install

ohpm install @ohos/opencsv

For details, see Installing an OpenHarmony HAR.

How to Use

Writing Data

import { getPath, openSync, CSVWriter } from '@ohos/opencsv';
getPath().then((path) => {
   let fd = openSync (path,'test.csv'/* csv filename */,0o2 | 0o100);
   let writer = new CSVWriter(fd.fd);
   writer.writeNext(["1", "Zhang San", "18"], true); // Write data.
   writer.writeNext(["2", "Li Si", "19"], true);
   writer.writeNext(["3", "Wang Wu", "20"], true);
   writer.writeNext(["4", "Zhao Liu", "21"], true);
   writer.close(); // Close writing.
})

Reading Data

import { getPath, openSync, CSVReaderBuilder, CSVParser } from '@ohos/opencsv';
getPath().then((path) => {
    let rd = openSync (path,'test.csv'/* csv filename */,0o2)
    let readerBuilder: CSVReaderBuilder = new CSVReaderBuilder(rd.fd)
    let readerbuildcsv = readerBuilder
          .withCSVParser(new CSVParser())
          .buildCSVReader()
    let lines: Array<Array<string>> = readerbuildcsv.readAll(); // Read all data in the CSV file. 
    console.log(JSON.stringify(lines))
    /* [
     *  ["1", "Zhang San", "18"]
     *  ["2", "Li Si", "19"]
     *  ["3", "Wang Wu", "20"]
     *  ["4", "Zhao Liu", "21"]
     *               ]
     */
    readerbuildcsv.close () // Close reading.
})

Reading Data with specified encoding, for the included encoding formats, see.

import { getPath, openSync, CSVReaderBuilder, CSVParser } from '@ohos/opencsv';
getPath().then((path) => {
    let rd = openSync (path,'test.csv'/* csv filename */,0o2)
    let readerBuilder: CSVReaderBuilder = new CSVReaderBuilder(rd.fd, 'gbk')
    let readerbuildcsv = readerBuilder
          .withCSVParser(new CSVParser())
          .buildCSVReader()
    let lines: Array<Array<string>> = readerbuildcsv.readAll(); // Read all data in the CSV file.
    console.log(JSON.stringify(lines))
    /* [
     *  ["1", "Zhang San", "18"]
     *  ["2", "Li Si", "19"]
     *  ["3", "Wang Wu", "20"]
     *  ["4", "Zhao Liu", "21"]
     *               ]
     */
    readerbuildcsv.close () // Close reading.
})

Available APIs

  1. Write a data record.

async writeNext(nextLine: Array<string>, appleQuotesToAll: boolean): Promise<void>;

Parameter Type Mandatory Description Return Value
nextLine Array< string > Yes Data record to write. This API returns no value.
  1. Write multiple data records.

writeAll(allLines: Array<Array<string>>, appleQuotesToAll: boolean): void;

Parameter Type Mandatory Description Return Value
allLines Array<Array< string >> Yes Data records to write. This API returns no value.
  1. Read the next data record.

readNext(): Array<string>

Parameter Type Mandatory Description Return Value
None None No Next data record read. Array< string >
  1. Read all data records.

readAll(): Array<Array<string>>

Parameter Type Mandatory Description Return Value
None None No All data records read. Array<Array< string >>

About obfuscation

  • Code obfuscation, please seeCode Obfuscation
  • If you want the OpenCSV library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/opencsv

Constraints

OpenCSV has been verified only in the following version:

DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release (5.0.0.66)

DevEco Studio (4.1.1.500) --SDK:API11 (4.1.7.3)

Directory Structure

|---- opencsv 
|     |---- entry  # Sample code
|     |---- library # OpenCSV library
|           |---- Index.ets  # External APIs
|     |---- README.md  # Brief introduction of the opencsv library                   

How to Contribute

If you find any problem during the use, submit an Issue or PR to us.

License

This project is licensed under Apache License 2.0.

Introduction

用户可快速在 OpenHarmony 应用中实现 CSV 文件的读写操作。支持单条/多条数据写入、读取所有数据及指定编码读取,提供简洁接口,便于表格数据的纯文本形式处理与存储。【此简介由AI生成】

Customize your domain