-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZZExcel2Word.cpp
362 lines (338 loc) · 9.73 KB
/
ZZExcel2Word.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "StdAfx.h"
#include "ZZExcel2Word.h"
#include "..\CExcelApplication.h"
#include "..\CExcelWorkbook.h"
#include "..\CExcelWorkbooks.h"
#include "..\CExcelWorksheet.h"
#include "..\CExcelWorksheets.h"
#include "..\CExcelRange.h"
#include <sstream>
CZZExcel2Word::CZZExcel2Word()
{
m_stringWordDocKey = _T("管道序号");
m_iValueNameRow = 2;
InitExportSettings();
m_ioutputOnlyoneFile = 0;
}
#define INI_DEBUG_APPNAME _T("调试规则")
#define INI_DEBUG_KEYNAME_ONLYOUTPUTONEFILE _T("仅输出一个文件")
#define INI_EXCEL_APPNAME _T("读取Excel规则")
#define INI_EXCEL_KEYNAME_VALUENAMEROWINDEX _T("表头行")
#define INI_REPORT_APPNAME _T("报告属性")
#define INI_REPORT_KEYNAME _T("报告关键字")
#define INI_DATAMAP_APPNAME _T("数据匹配")
#define MAX_LENGTH 1024
#define INI_FILE_NAME _T("ZZ.ini")
#define Template_FILE_NAME _T("ZZTemplate.dot")
std::wstring CZZExcel2Word::GetCurrentDir()
{
TCHAR szBuf[MAX_PATH];
ZeroMemory(szBuf,MAX_PATH);
std::wstring initFilePath;
if (GetCurrentDirectory(MAX_PATH,szBuf) > 0)
{
initFilePath = szBuf;
initFilePath += _T("\\");
}
else
{
initFilePath = _T("C:\\");
}
return initFilePath;
}
HRESULT CZZExcel2Word::InitExportSettings()
{
std::wstring curdir = GetCurrentDir();
std::wstring initFilePath;
m_stringWordTemplatePath = curdir + Template_FILE_NAME;
initFilePath = curdir+INI_FILE_NAME;
return InitExportSettings(initFilePath.c_str());
}
//遍历ini文件
HRESULT CZZExcel2Word::InitExportSettings(LPCTSTR pFilePath)
{
if (!PathFileExists(pFilePath))
{
WritePrivateProfileString(INI_EXCEL_APPNAME,INI_EXCEL_KEYNAME_VALUENAMEROWINDEX,_T("2"),pFilePath);
WritePrivateProfileString(INI_REPORT_APPNAME,INI_REPORT_KEYNAME,_T("管道序号"),pFilePath);
WritePrivateProfileString(INI_DATAMAP_APPNAME,_T("管道名称"),_T("管道名称"),pFilePath);
WritePrivateProfileString(INI_DEBUG_APPNAME,INI_DEBUG_KEYNAME_ONLYOUTPUTONEFILE,_T("1"),pFilePath);
}
// TODO: Add your control notification handler code here
TCHAR strAppNameTemp[1024];//所有AppName的返回值
TCHAR strKeyNameTemp[1024];//对应每个AppName的所有KeyName的返回值
TCHAR strReturnTemp[1024];//返回值
DWORD dwKeyNameSize;//对应每个AppName的所有KeyName的总长度
//所有AppName的总长度
DWORD dwAppNameSize = GetPrivateProfileString(NULL,NULL,NULL,strAppNameTemp,MAX_LENGTH,pFilePath);
if(dwAppNameSize>0)
{
TCHAR *pAppName = new TCHAR[dwAppNameSize];
int nAppNameLen=0; //每个AppName的长度
for(DWORD i = 0;i<dwAppNameSize;i++)
{
pAppName[nAppNameLen++]=strAppNameTemp[i];
if(strAppNameTemp[i]==0)
{
dwKeyNameSize = GetPrivateProfileString(pAppName,NULL,NULL,strKeyNameTemp,1024,pFilePath);
if(dwAppNameSize>0)
{
TCHAR *pKeyName = new TCHAR[dwKeyNameSize];
int nKeyNameLen=0; //每个KeyName的长度
for(DWORD j = 0;j<dwKeyNameSize;j++)
{
pKeyName[nKeyNameLen++]=strKeyNameTemp[j];
if(strKeyNameTemp[j]==0)
{
if(GetPrivateProfileString(pAppName,pKeyName,NULL,strReturnTemp,1024,pFilePath))
{
NULL;
}
//my code here. szg
std::wstring tempappstr = pAppName;
if (tempappstr == INI_REPORT_APPNAME)
{
m_stringWordDocKey = strReturnTemp;
}
if (tempappstr == INI_DATAMAP_APPNAME)
{
std::wstring tempbookmarkstr = pKeyName;
std::wstring tempReturnstr = strReturnTemp;
AddBookMarkDataPair(tempReturnstr,tempbookmarkstr);
}
if (tempappstr == INI_EXCEL_APPNAME)
{
std::wstring tempkeyname = pKeyName;
if (tempkeyname == INI_EXCEL_KEYNAME_VALUENAMEROWINDEX)
{
m_iValueNameRow = _ttoi(strReturnTemp);
}
}
if (tempappstr == INI_DEBUG_APPNAME)
{
std::wstring tempkeyname = pKeyName;
if (tempkeyname == INI_DEBUG_KEYNAME_ONLYOUTPUTONEFILE)
{
m_ioutputOnlyoneFile = _ttoi(strReturnTemp);
}
}
memset(pKeyName,0,dwKeyNameSize);
nKeyNameLen=0;
}
}
delete[]pKeyName;
}
memset(pAppName,0,dwAppNameSize);
nAppNameLen=0;
}
}
delete[]pAppName;
}
return S_OK;
}
CZZExcel2Word::~CZZExcel2Word(void)
{
ClearWordDoc();
}
HRESULT CZZExcel2Word::ExportDataToWordDoc(std::wstring LocationFolder)
{
std::vector<PZZWordDoc>::iterator it;
for (it = m_vecWordDoc.begin(); it != m_vecWordDoc.end(); ++it)
{
PZZWordDoc temp = *it;
temp->GenerateWordDoc(LocationFolder);
}
return S_OK;
}
HRESULT CZZExcel2Word::AddBookMarkDataPair(std::wstring DataName,std::wstring BookMarkName)
{
m_mapDataItem2BookMark.insert(std::pair<std::wstring, std::wstring>(BookMarkName, DataName));
return S_OK;
}
HRESULT CZZExcel2Word::TransferExcelFiles2Word(std::vector<std::wstring> vecExcelFiles)
{
std::vector<std::wstring>::iterator it;
for (it = vecExcelFiles.begin(); it != vecExcelFiles.end(); ++it)
{
std::wstring temp = *it;
BuildDataFromExcelFile(temp,m_stringWordDocKey);
}
return S_OK;
}
static std::wstring GetStringFromExcelCell(CExcelRange& useRange)
{
COleVariant keyValue = useRange.get_Value2();
std::wstring itemString;
if (keyValue.vt != VT_BSTR)
{
if (keyValue.vt == VT_R8)
{
std::wostringstream ostr;
ostr<<keyValue.dblVal;
itemString = ostr.str();
}
}
else
{
if (keyValue.bstrVal != NULL)
{
itemString = keyValue.bstrVal;
}
}
return itemString;
}
HRESULT CZZExcel2Word::BuildDataFromExcelFile(std::wstring ExcelFile,std::wstring stringDocKey)
{
CExcelApplication ExcelApp;
CExcelWorkbooks wbsMyBooks;
CExcelWorkbook wbMyBook;
CExcelWorksheets wssMysheets;
CExcelWorksheet wsMysheet;
//创建Excel 2000服务器(启动Excel)
// IID clsid;
// HRESULT hr = IIDFromString(_T("Excel.Application"), &clsid);
if (!ExcelApp.CreateDispatch(_T("Excel.Application"),NULL))
{
AfxMessageBox(_T("创建Excel服务失败!"));
exit(1);
}
//利用模板文件建立新文档
wbsMyBooks = ExcelApp.get_Workbooks();
COleVariant avar((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
wbMyBook = wbsMyBooks.Open(ExcelFile.c_str(),avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar,avar);
//得到Worksheets
wssMysheets = wbMyBook.get_Sheets();
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CExcelRange useRange;
for (int iSheetIdx = 1; iSheetIdx <= wssMysheets.get_Count() ; iSheetIdx++)
{
//得到sheet
wsMysheet=wssMysheets.get_Item(COleVariant((short)iSheetIdx));
#ifdef _DEBUG
CString sheetname = wsMysheet.get_Name();
#endif // _DEBUG
//首先分析表的第一行,得到数据关键字
int iKeyColumnIdx = -1;
CExcelRange useRange = wsMysheet.get_UsedRange();
long iRowNum = useRange.get_Count();
useRange = useRange.get_Columns();
long iStartRow = useRange.get_Row();
int nColumn = useRange.get_Count();
long iStartCol = useRange.get_Column();
long iValueNameRow = m_iValueNameRow;
//find keyName Column index
for (int iColIdx = iStartCol; iColIdx <= nColumn ; iColIdx++)
{
useRange = wsMysheet.get_Cells();
COleVariant keyValue=useRange.get_Item(_variant_t(iValueNameRow),_variant_t(iColIdx));
useRange = keyValue.pdispVal;
std::wstring itemString = GetStringFromExcelCell(useRange);
if (stringDocKey == itemString )
{
iKeyColumnIdx = iColIdx;
break;
}
}
if (iKeyColumnIdx < 0)
{
continue;//no valid data with doc key string
}
//根据关键字列创建doc
for (int iRodIdx = 3; iRodIdx <= iRowNum ; iRodIdx++)
{
useRange = wsMysheet.get_Cells();
COleVariant keyValue=useRange.get_Item(_variant_t(iRodIdx),_variant_t(iKeyColumnIdx));
useRange = keyValue.pdispVal;
std::wstring keyitemString = GetStringFromExcelCell(useRange);
if (keyitemString.empty())
{
continue;
}
PZZWordDoc pDoc = GetDocFromKeyString(keyitemString);
if (pDoc == NULL)
{
pDoc = CreateDoc(keyitemString);
if (pDoc == NULL)
{
continue;
}
SetExportReportSetting(pDoc);
m_vecWordDoc.push_back(pDoc);
}
//逐个读取相关doc的数据
for (int iColIdx = iStartCol; iColIdx <= nColumn ; iColIdx++)
{
if (iColIdx == iKeyColumnIdx)
{
continue;
}
useRange = wsMysheet.get_Cells();
keyValue=useRange.get_Item(_variant_t(iRodIdx),_variant_t(iColIdx));
useRange = keyValue.pdispVal;
std::wstring valueitemString = GetStringFromExcelCell(useRange);
if (valueitemString.empty())
{
//无效数据
continue;
}
//查找这个值对应的名字
useRange = wsMysheet.get_Cells();
keyValue=useRange.get_Item(_variant_t(iValueNameRow),_variant_t(iColIdx));
useRange = keyValue.pdispVal;
std::wstring valuenameitemString = GetStringFromExcelCell(useRange);
if (valuenameitemString.empty())
{
//无效数据名字
continue;
}
pDoc->AddDataItem(valuenameitemString,valueitemString);
}
}
if (m_ioutputOnlyoneFile ==1)
{
break;
}
wsMysheet.ReleaseDispatch();
}//sheet scan
wssMysheets.ReleaseDispatch();
wbsMyBooks.Close();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.Quit();
ExcelApp.ReleaseDispatch();
return S_OK;
}
PZZWordDoc CZZExcel2Word::CreateDoc(std::wstring key)
{
PZZWordDoc newDoc = new CZZWordDoc();
newDoc->SetName(key);
return newDoc;
}
PZZWordDoc CZZExcel2Word::GetDocFromKeyString(std::wstring key)
{
std::vector<PZZWordDoc>::iterator it;
for (it = m_vecWordDoc.begin(); it != m_vecWordDoc.end(); ++it)
{
PZZWordDoc temp = *it;
if (temp->GetName() == key)
{
return temp;
}
}
return NULL;
}
void CZZExcel2Word::ClearWordDoc()
{
std::vector<PZZWordDoc>::iterator it;
for (it = m_vecWordDoc.begin(); it != m_vecWordDoc.end(); ++it)
{
PZZWordDoc temp = *it;
delete temp;
}
m_vecWordDoc.clear();
}
void CZZExcel2Word::SetExportReportSetting( PZZWordDoc pDoc )
{
pDoc->SetMapDataItem2BookMark(m_mapDataItem2BookMark);
pDoc->SetStringWordTemplatePath(m_stringWordTemplatePath);
}