[C++]遍历文件夹所有文件(含所有子目录)输出文件大小至EXCEL

环境

Microsoft Windows 11.22621.674
Microsoft Excel 2019MSO (2209 Build 16.0.15629.20196) 32 位
Microsoft Visual Studio Professional 201916.11.20

libxl-win-4.0.4.zip

第一步 创建项目

创建C++空项目

第二步 配置环境

去这里下载LIBXL库:C++ Excel Library to read/write xls/xlsx files - LibXL

正版的LIBXL库使用时第一行有提示"Created by LibXL trial version 4.0.4. Please buy the LibXL full version for removing this message."

想去掉这个提示用盗版自己想办法

下载完比如说我用的版本是libxl-win-4.0.4.zip解压到VS的项目文件夹

然后去这里(也在LIBXL的官网里面)看怎么配环境:Setup LibXL in Visual Studio for C/C++ languages

①项目→属性

C/C++→常规→附加包含目录→添加include_cpp的位置

根据你的生成不同配置的文件不同

生成x86用红色

链接器→常规→附加库目录→添加红色的lib文件夹位置

链接器→输入→附加依赖项→输入libxl.lib

将红色的bin文件夹内的libxl.dll复制到Debug或Release文件夹

生成x64用绿色

链接器→常规→附加库目录→添加红色的lib文件夹位置

链接器→输入→附加依赖项→输入libxl.lib

将绿色的的bin64文件夹内的libxl.dll复制到Debug或Release文件夹

③添加代码

#include "libxl.h"
using namespace libxl;

第三步 代码

#include<io.h>
#include<fstream>
#include<iostream>
#include<vector>
#include<algorithm>
#include <string>
#include "libxl.h"
#include <windows.h>
#include <direct.h>

using namespace libxl;
using namespace std;

#define uint64_asa unsigned __int64

wchar_t* multiByteToWideChar(const string& pKey)
{
    char* pCStrKey = (char*)pKey.c_str();
    unsigned long pSize = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0);
    wchar_t* pWCStrKey = new wchar_t[pSize];
    MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pWCStrKey, pSize);
    return pWCStrKey;
}

string Scale(uint64_asa BYTE)
{
    vector<string> vec;
    vec.push_back("B");
    vec.push_back("KB");
    vec.push_back("MB");
    vec.push_back("GB");
    vec.push_back("TB");
    vec.push_back("PB");

    uint64_asa nRemainder = 0;
    double nKeyValue = 1024.0;
    uint64_asa nByteSize = BYTE;
    int i = 0;

    while (nByteSize >= nKeyValue && i < vec.size())
    {
        nRemainder = (uint64_asa)nByteSize % (uint64_asa)nKeyValue;
        nByteSize = nByteSize / nKeyValue;   //nByteSize /= nKeyValue
        i++;
    }

    nByteSize = floor(nByteSize);
    string strResult = to_string((uint64_asa)nByteSize);
    if (nRemainder > 0)
    {
        strResult += "." + to_string(nRemainder).substr(0, 2);
    }

    strResult += vec.at(i);
    //cout << strResult << endl;

    return strResult;
}

void GetFiles(string path, vector<string>& files)
{
    //文件句柄  
    intptr_t   hFile = 0;
    //文件信息,声明一个存储文件信息的结构体  
    struct _finddata_t fileinfo;
    string p;//字符串,存放路径

    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
    {
        do
        {
            //如果是目录,迭代之(即文件夹内还有文件夹)  
            if ((fileinfo.attrib & _A_SUBDIR))
            {
                //.表示当前目录;..表示当前目录的父目录;
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    //进行向里递归;
                    GetFiles(p.assign(path).append("\\").append(fileinfo.name), files);
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }

        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}

int main()
{
    vector<string> str_vecFileName;
    string strFilePath;
    cout << "请输入要查询的文件夹路径:" << endl;
    getline(cin, strFilePath);
    //cout << strFilePath << endl;
    cout << "正在生成表格..." << endl;
    GetFiles(strFilePath, str_vecFileName);
    Book * book = xlCreateBook();
    book->setKey(L"TommoT", L"windows-2421220b07c2e10a6eb96768a2p7r6gc");
    Sheet* sheet = book->addSheet(L"Sheet1");

    int intRow = 0;
    sheet->setCol(0, 0, 80);
    sheet->setCol(1, 1, 20);
    sheet->setCol(1, 2, 20);
    for (vector<string>::iterator it0 = str_vecFileName.begin(); it0 != str_vecFileName.end(); it0++)
    {
        //输出全路径
        sheet->writeStr(0, 0, L"当前文件夹内所有文件");
        sheet->writeStr(0, 1, L"字节(Byte)");
        sheet->writeStr(0, 2, L"等于");
        sheet->writeStr(intRow+1, 0, multiByteToWideChar (*it0));
        ifstream fin(*it0);
        if (fin.is_open())
        {
            fin.seekg(0, ios::end);
            uint64_asa int_64FileSize = fin.tellg();
            string strSize = to_string(int_64FileSize);
            fin.close();
            //输出文件大小
            sheet->writeStr(intRow+1, 1, multiByteToWideChar(strSize));
            sheet->writeStr(intRow+1, 2, multiByteToWideChar(Scale(int_64FileSize)));
        }
        intRow++;
    }

    book->save(L"当前文件夹内所有文件.xls");
    book->release();

    char* buffer = _getcwd(NULL, 0);
    printf("表格已生成成功!位于【%s】\n", buffer);
    free(buffer);
    system("pause");

    return 0;
}

 

注意事项

使用了getline但是任然读取不到带有空格的路径,试试把这里的C++语言标准改新一点

生成的表格样式

 

 

posted @ 2022-10-20 16:32  SairenjiHaruna  阅读(311)  评论(0编辑  收藏  举报