用C++管理Excel文件的数据

如何向单元格添加数据

Aspose.Cells提供了一个表示Microsoft Excel文件的 Workbook 类。 Workbook 类包含一个 Worksheets 集合,允许访问Excel文件中的每个工作表。工作表由 Worksheet 类表示。 Worksheet 类提供一个 Cells 集合。 Cells 集合中的每个项目都表示 Cell 类的对象。

Aspose.Cells允许开发人员通过调用 Cell 类的 PutValue 方法向工作表中的单元格添加数据。Aspose.Cells提供 PutValue 方法的重载版本,使开发人员可以向单元格添加不同类型的数据。使用 PutValue 方法的这些重载版本,可以将布尔值、字符串、双精度、整数或日期/时间等值添加到单元格。

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    Workbook workbook;

    WorksheetCollection worksheets = workbook.GetWorksheets();
    Worksheet worksheet = worksheets.Get(0);


    worksheet.GetCells().Get(U16String(u"A1")).PutValue(U16String(u"Hello World"));
    worksheet.GetCells().Get(U16String(u"A2")).PutValue(20.5);
    worksheet.GetCells().Get(U16String(u"A3")).PutValue((int32_t)15);
    worksheet.GetCells().Get(U16String(u"A4")).PutValue(true);

    Cell cellA1 = worksheet.GetCells().Get(U16String(u"A4"));
    Style style = cellA1.GetStyle();
    style.SetNumber(15);
    cellA1.SetStyle(style);

    workbook.Save(outDir + u"output.out.xls");

    std::cout << "Data inserted and workbook saved successfully." << std::endl;

    Aspose::Cells::Cleanup();
}

如何提高效率

如果您使用PutValue方法向工作表添加大量数据,应先按行然后按列添加值到单元格。这种方法极大地提高了应用程序的效率。

如何从单元格中检索数据

Aspose.Cells提供一个代表Microsoft Excel文件的类WorkbookWorkbook类包含一个Worksheets集合,允许访问文件中的工作表。工作表由Worksheet类表示。Worksheet类提供Cells集合。Cells集合中的每个项代表Cell类的对象。

Cell类提供了几个属性,允许开发人员根据其数据类型从单元格中检索值。这些属性包括:

当字段未填写时,具有DoubleValue 或FloatValue 的单元格会引发异常。

还可以通过使用Cell 类的Type 属性来检查单元格中包含的数据类型。 实际上,Cell 类的Type 属性基于CellValueType 枚举,其预定义值如下:

单元格值类型 描述
IsBool 指定单元格值为布尔类型。
IsDateTime 指定单元格值为日期/时间类型。
IsNull 表示空白单元格。
IsNumeric 指定单元格值为数值类型。
IsString 指定单元格值为字符串类型。
IsUnknown 指定单元格值为未知类型。

您还可以使用上述预定义的单元格值类型与每个单元格中存在的数据类型进行比较。

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
    U16String inputFilePath = srcDir + u"book1.xls";

    Workbook workbook(inputFilePath);
    Worksheet worksheet = workbook.GetWorksheets().Get(0);

    int maxRow = worksheet.GetCells().GetMaxDataRow();
    int maxCol = worksheet.GetCells().GetMaxDataColumn();

    for (int row = 0; row <= maxRow; row++)
    {
        for (int col = 0; col <= maxCol; col++)
        {
            Cell cell = worksheet.GetCells().Get(row, col);

            U16String stringValue;
            double doubleValue = 0.0;
            bool boolValue = false;

            switch (cell.GetType())
            {
                case CellValueType::IsString:
                    stringValue = cell.GetStringValue();
                    std::cout << "String Value: " << stringValue.ToUtf8() << std::endl;
                    break;

                case CellValueType::IsNumeric:
                    doubleValue = cell.GetDoubleValue();
                    std::cout << "Double Value: " << doubleValue << std::endl;
                    break;

                case CellValueType::IsBool:
                    boolValue = cell.GetBoolValue();
                    std::cout << "Bool Value: " << boolValue << std::endl;
                    break;

                case CellValueType::IsDateTime:
                    stringValue = cell.GetStringValue();
                    std::cout << "DateTime Value: " << stringValue.ToUtf8() << std::endl;
                    break;

                case CellValueType::IsUnknown:
                    stringValue = cell.GetStringValue();
                    std::cout << "Unknown Value: " << stringValue.ToUtf8() << std::endl;
                    break;

                case CellValueType::IsNull:
                    break;
            }
        }
    }

    Aspose::Cells::Cleanup();
    return 0;
}

高级主题