• VS将数据保存在excel表格中


    参考链接转载自:https://blog.csdn.net/qq_28093585/article/details/78836821

    我使用的第三方库是

    ExcelFormat Library:       https://www.codeproject.com/Articles/42504/ExcelFormat-Library

    软件版本:VS2013

     使用方法:

    将源文件中的BasicExcel.cpp、BasicExcel.hpp、ExcelFormat.cpp、ExcelFormat.h直接加入到工程中。

    此时编译项目会报一个错误

    解决办法:

    第一步:

    第二步:

    然后就可以正常调用这个第三方库的源文件了。

    下面的代码是简单的创建一个Excel表格,将数据保存到一个名为Test.xls文件。

    我是将代码分为了四个部分,分别放在不同的位置。不可直接copy编译,可能会有未知的问题。

    // Part1
    #include "ExcelFormat.h"
    
    using namespace ExcelFormat;
    
    // save data into excel file at selected position
    // 将数据保存到选定位置的excel文件中
    BasicExcel xls;
    
    BasicExcelWorksheet* sheet = NULL;
    BasicExcelCell* cell = NULL;
    
    int RowNum = 1;    // 标题在第一行,数据从第二行开始保存,所以这里value为1而不是0
    
    // Part2
        // create sheet 1 and get the associated BasicExcelWorksheet pointer
        // 创建工作表1并获取关联的BasicExcelWorksheet指针
        xls.New(1);
        sheet = xls.GetWorksheet(0);
        XLSFormatManager fmt_mgr(xls);
    
        // 设置Excel表格中的字体
        ExcelFont font_bold;
        font_bold._weight = FW_BOLD; // 700
    
        CellFormat fmt_bold(fmt_mgr);
        fmt_bold.set_font(font_bold);
    
        int col = 0, row = 0;
        //写入第一行,也就是表头
        cell = sheet->Cell(row, col); cell->Set("序号");      cell->SetFormat(fmt_bold); ++col;
        cell = sheet->Cell(row, col); cell->Set("altitude");  cell->SetFormat(fmt_bold); ++col;
        cell = sheet->Cell(row, col); cell->Set("longitude"); cell->SetFormat(fmt_bold); ++col;
        cell = sheet->Cell(row, col); cell->Set("latitude");  cell->SetFormat(fmt_bold); col = 0;
    
    // Part3
    int col = 0;
    sheet->Cell(RowNum, col)->Set(RowNum);         ++col;
    sheet->Cell(RowNum, col)->Set(p->altitude);    ++col;
    sheet->Cell(RowNum, col)->Set(p->longitude);   ++col;
    sheet->Cell(RowNum, col)->Set(p->latitude);
    RowNum++;
    
    // Part4
    xls.SaveAs("Test.xls");

    另外第三方库的链接中有提供样例和源码,样例很明了。建议代码上有什么语法问题可以参考一下。

  • 相关阅读:
    CF1343E-Weights Distributing (最短路)
    科大讯飞杯-日期小助手(补)
    网络15软工个人作业5——软件工程总结
    软工网络15个人作业4——alpha阶段个人总结
    软件工程网络15个人作业3——案例分析
    软工网络15结对编程练习
    软工网络15个人阅读作业2——提问题
    软件工程网络15个人阅读作业1(201521123052 蓝锦明)
    Java课程设计 购物车系统(个人博客) 201521123052 蓝锦明
    201521123052 《Java程序设计》 第14周学习总结
  • 原文地址:https://www.cnblogs.com/KeepThreeMunites/p/13941368.html
Copyright © 2020-2023  润新知