Excel-lib的使用

第一步:在代码文件夹放下面文件:

(1)enum.h

(2)IBookT.h

(3)IFontT.h

(4)IFormatT.h

(5)ISheetT.h

(6)libxl.h

(7)setup.h

(8)main

(9)libxl.dll

(10)lib/libxl.lib

第二步:

1.在项目的【属性页】->【配置属性】->【C/C++】->【常规】->【附加包含目录】填写相对路径

如:..\项目文件夹\代码文件夹

 2.在项目的【属性页】->【配置属性】->【链接器】->【常规】->【附加库目录】填写相对路径

如:..\代码文件夹\lib库文件夹

3.在项目的【属性页】->【配置属性】->【链接器】->【输入】->【附加依赖项】填写lib库的名字

如:libxl.lib

 

  1 #include <iostream>
  2 #include <fstream>
  3 #include <sstream>
  4 #include <Windows.h>
  5 #include "libxl.h"
  6 #include <vector>
  7 #include <io.h>
  8 #include <string>
  9 
 10 using namespace std;
 11 using namespace libxl;
 12 
 13 void GetDataPath(string strRootPath,string &strDataPath);
 14 void GetFilePath(string strRoot,vector<string> &path);
 15 void PutFileToExcel(string strDataPath,vector<string> path);
 16 
 17 
 18 void main()
 19 {
 20     char szAppName[MAX_PATH]={0};
 21     string strRootPath;
 22     ::GetModuleFileName(NULL, szAppName, MAX_PATH);
 23     strRootPath = szAppName;
 24     strRootPath = strRootPath.substr(0,strRootPath.find_last_of('\\'));
 25 
 26     string strDataPath;
 27     vector<string>path;
 28     GetDataPath(strRootPath,strDataPath);
 29     GetFilePath(strDataPath,path);
 30     PutFileToExcel(strDataPath,path);
 31 }
 32 
 33 void GetDataPath(string strRootPath,string &strDataPath)
 34 {
 35     string filepath=strRootPath+"\\temp.txt";
 36     fstream file;
 37     setlocale(LC_ALL,"");
 38     file.open(filepath.c_str(),ios::in);
 39     if (file)
 40     {
 41         while (!file.eof())
 42         {
 43             char buffer[256]={0};
 44             file.getline(buffer,256,'\n');
 45             strDataPath=buffer;            
 46         }
 47         file.close();
 48     }
 49     else
 50     {
 51         printf("temp文件不存在\n");
 52     }
 53 }
 54 void GetFilePath(string strDataPath,vector<string> &path)
 55 {
 56     string strPath=strDataPath+"\\*.txt";
 57     _finddata_t fileDir;
 58     long lfDir;
 59     if((lfDir = _findfirst(strPath.c_str(),&fileDir))==-1l)
 60     {
 61         printf("No file is found\n");
 62     }        
 63     else
 64     {
 65         do 
 66         {
 67             string strfileName=fileDir.name;
 68             if (strfileName!="."&&strfileName!="..")
 69             {
 70                 path.push_back(strfileName);
 71             }
 72             
 73         } while (_findnext( lfDir, &fileDir ) == 0);
 74     }
 75     _findclose(lfDir);
 76 }
 77 void PutFileToExcel(string strDataPath,vector<string> path)
 78 {
 79     //sheet->setCol(0, 0, 5);
 80     Book *book=xlCreateBook();
 81     Sheet *sheetCPU = book->addSheet("CPU");
 82     Sheet *sheetMemory = book->addSheet("Memory");
 83     Sheet *sheetLoad=book->addSheet("Load");
 84     string strExcelpath=strDataPath+"\\ServerData.xls";
 85     int fileColomn=1;
 86     if (book)
 87     {
 88         for (UINT i=0;i<path.size();i++)
 89         {
 90             string strFileName=path.at(i);
 91             string strFilePath=strDataPath+"\\"+strFileName;
 92             printf("%d. %s\n",i+1,strFileName.c_str());
 93             string name;
 94             int ipos=strFileName.find_last_of('.');
 95             name=strFileName.substr(0,ipos);
 96 
 97             if (book)
 98             {
 99                 setlocale(LC_ALL,"");
100                 char buffer[512]={0};
101                 ifstream file;
102                 file.open(strFilePath.c_str(),ios::in);
103                 if (file.fail())
104                 {
105                     printf("Open file failed\n");
106                 }
107                 else
108                 {
109                     if (sheetCPU&&sheetMemory&&sheetLoad)
110                     {
111                         if (name=="time")
112                         {
113                             sheetCPU->writeStr(1,0,"Time");
114                             sheetMemory->writeStr(1,0,"Time");
115                             sheetLoad->writeStr(1,0,"Time");
116 
117                             int iRow=2;
118                             while (file.getline(buffer,256,'\n'))
119                             {
120                                 istringstream istr(buffer);
121                                 string str1;
122                                 istr>>str1;
123                                 sheetCPU->writeStr(iRow, 0, str1.c_str());
124                                 sheetMemory->writeStr(iRow, 0, str1.c_str());
125                                 sheetLoad->writeStr(iRow, 0, str1.c_str());
126                                 iRow++;
127                             }
128                         }
129                         else if(name=="load")
130                         {
131                             sheetLoad->writeStr(1,1,name.c_str());
132                             int iRow=2;
133                             while (file.getline(buffer,256,'\n'))
134                             {
135                                 istringstream istr(buffer);
136                                 string str1;
137                                 int strPos=0;
138                                 while (true)
139                                 {
140                                     istr>>str1;
141                                     strPos=str1.find("average");
142                                     if (strPos>=0)
143                                     {
144                                         istr>>str1;
145                                         break;
146                                     }
147                                 }
148                                 strPos=str1.find(",");
149                                 str1=str1.substr(0,strPos);
150                                 sheetLoad->writeStr(iRow,1, str1.c_str());
151 
152                                 istr>>str1;
153                                 strPos=str1.find(",");
154                                 str1=str1.substr(0,strPos);
155                                 sheetLoad->writeStr(iRow, 2, str1.c_str());
156 
157                                 istr>>str1;
158                                 sheetLoad->writeStr(iRow, 3, str1.c_str());
159                                 iRow++;
160                             }
161                         }
162                         else
163                         {
164                             string strServerCPU;
165                             string strServerMemory;
166                             strServerCPU=name+" CPU";
167                             strServerMemory=name+" Memory";
168 
169                             sheetCPU->writeStr(1,fileColomn,strServerCPU.c_str());
170                             sheetMemory->writeStr(1,fileColomn,strServerMemory.c_str());
171 
172                             int iRow=1;
173                             while (file.getline(buffer,256,'\n'))
174                             {
175                                 istringstream istr(buffer);
176                                 string str1;
177                                 istr>>str1>>str1>>str1>>str1>>str1>>str1>>str1>>str1>>str1;
178                                 sheetCPU->writeStr(iRow+1, fileColomn, str1.c_str());
179                                 istr>>str1;
180                                 sheetMemory->writeStr(iRow+1, fileColomn, str1.c_str());
181                                 iRow++;
182                             }
183                             fileColomn++;
184                         }
185                     }
186                     file.close();
187                 }
188             }
189         }
190         book->save(strExcelpath.c_str());
191         book->release();
192     }
193 }

 

转载于:https://www.cnblogs.com/Yao1991/p/6740853.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值