接前面所写文章,继续编写程序。
按照此方法实现的时候,我们需要使用GDIPlus开源库实现图片的转化功能。因此我们需要在vc开发环境中设置库路径。
GDIPlus下载地址:
https://download.csdn.net/download/u013970791/12200574
下载完成后解压到本机电脑的一个路径下面即可
接下来在vc6.0中导入GDIPlus开源库
选择vc6.0的菜单栏“Tool”-》“Options”,在弹出的对话框中找到Directories标签页,在Show directories for:下拉框选项中选择“Include files”,在“Directories”的右侧点击第一个小图标
会在列表中出现一行空白行,此时点击空白行后面的“...”按钮,找到解压之后的GDIPlus文件夹中的“Include”文件夹,选中之后双击即可完成“Include”文件夹的添加。
切换“Show Directories for:”下拉框选项选择“Library files”选项,找到GDIPlus文件夹中的“Lib”文件夹双击添加进去即可。
选择vc6.0的菜单栏“Project”-》“Setting”,在弹出的对话框界面找到“Link”标签页,在Object/library modules:输入框中输入:GDIPlus.lib
打开“FileView”视图,双击打开StdAfx.h 文件,添加如下代码:
打开“FileView”视图,双击打开ExcelDemo1.h 文件,添加如下代码:
打开“FileView”视图,双击打开ExcelDemo1.cpp 文件,添加如下代码:
完成上述的操作后,点击键盘“F7”快捷键编译程序,此时会发现编译的时候报了好多的错误,这个错误的原因是因为Excel中的类型名与GDIPlus中的类型名冲突导致的,解决方法如下:
1、在“FileView”视图中双击《excel.h》文件打开,在文件的开头位置添加代码:
2、拖动滑块置文件的末尾处,在文件的末尾添加}符号:
3、在“FileView”视图中双击《excel.cpp》文件打开,在文件中添加如下代码:
4、拖动滑块置文件的末尾处,在文件的末尾添加}符号:
5、在“FileView”视图中双击《XExcel.h》文件打开,在文件中添加如下代码:
完成上述步骤之后,重新点击键盘快捷键“F7”重新编译程序,类型名冲突的报错信息将会消失。
打开“FileView”视图,双击打开ExcelDemo1Dlg.h 文件,在文件中导入XExcel.h头文件,如下所示:
打开“ResourceView”视图,找到IDD_EXCELDEMO1_DIALOG对话框,双击打开该对话框,效果图如下所示:
双击对话框中的“确定”按钮,为“确定”按钮增加响应函数。操作完成后进入“确定”按钮实现函数处进行代码的编辑工作。
在这里我们首先为CExcelDemo1Dlg类添加一个成员函数,作为生成JPG图片方法的实现。
打开“FileView”视图,双击打开ExcelDemo1Dlg.h 文件,在类中添加
成员函数:
int SaveFile(Bitmap* pImage, const wchar_t* pFileName);
int SelectDirPath(CString& strDirPath);
int CreateJPGFile(const CString& strFilepath);
int CreateJPGByCliopBoard();
int BmpToJpg(const unsigned short* szBmpPath, const unsigned short* szJPGPath);
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
成员变量:
CString m_strSaveJPGDirPath;
打开“FileView”视图,双击打开ExcelDemo1Dlg.cpp 文件,实现成员函数的功能:
int CExcelDemo1Dlg::SaveFile(Bitmap* pImage, const wchar_t* pFileName)
{
EncoderParameters encoderParameters;
CLSID jpgClsid;
int nRet = GetEncoderClsid(L"image/jpeg", &jpgClsid);
if (nRet < 0 return -1 encoderparameters.count='1;' encoderparameters.parameter0.guid='EncoderQuality;' encoderparameters.parameter0.type='EncoderParameterValueTypeLong;' encoderparameters.parameter0.numberofvalues='1;' ulong quality quality='100;' encoderparameters.parameter0.value='&quality;' status status='pImage-'>Save(pFileName, &jpgClsid, &encoderParameters);
if (status != Ok)
{
return -1;
}
return 0;
}
int CExcelDemo1Dlg::SelectDirPath(CString& strDirPath)
{
CWnd* pMainCWnd = NULL;
pMainCWnd = GetActiveWindow();
HWND hWnd = pMainCWnd->GetSafeHwnd();
strDirPath.Empty();
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
return 0;
char szDisplayName[_MAX_PATH];
char szBuffer[_MAX_PATH];
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hWnd;
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = "选择一个文件夹";
browseInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
browseInfo.lpfn = NULL;
browseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
{
if (szBuffer[0] == '\0')
return 0;
strDirPath = szBuffer;
return 1;
}
else
return 1;
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return 1;
}
int CExcelDemo1Dlg::CreateJPGFile(const CString& strFilepath)
{
CString strExcelPath = "";
CString strDirPath = "";
CString strSavePath = "";
CString strFileName = "";
int nPos = strFilepath.Find(".");
if (nPos > 0)
{
strFileName = strFilepath.Mid(0, nPos);
}
strSavePath.Format("%s%s", m_strSaveJPGDirPath, strFileName);
m_edtPath.GetWindowText(strDirPath);
strExcelPath.Format("%s%s", strDirPath, strFilepath);
CXExcel xExcel;
if(xExcel.InitApp()<0) return -1;
int nRet = xExcel.Open(strExcelPath);
if (nRet < 0)
{
CString strMsg = "";
strMsg.Format("打开文件失败:%s", strExcelPath);
AfxMessageBox(strMsg);
xExcel.ExitApp();
}
CString strBeginArea = "", strEndArea = "";
m_edtBegin.GetWindowText(strBeginArea);
m_edtEnd.GetWindowText(strEndArea);
if (strBeginArea.IsEmpty() || strEndArea.IsEmpty())
{
AfxMessageBox("请录入完整的截图区域!");
xExcel.ExitApp();
return -1;
}
BOOL bRet = xExcel.SaveExcelCellToPic(strBeginArea, strEndArea, strSavePath + ".bmp");
if (!bRet)
{
AfxMessageBox("生成图片失败!");
xExcel.ExitApp();
return -1;
}
unsigned short szBmpPath[MAX_PATH] = {0};
unsigned short szJpgPath[MAX_PATH] = {0};
MultiByteToWideChar(CP_ACP, 0, strSavePath + ".bmp", -1, szBmpPath, MAX_PATH);
MultiByteToWideChar(CP_ACP, 0, strSavePath + ".jpg", -1, szJpgPath, MAX_PATH);
nRet = BmpToJpg(szBmpPath, szJpgPath);
if (nRet == 0)
{
::DeleteFile(strSavePath + ".bmp");
}
else
{
AfxMessageBox("图片转化失败!");
xExcel.ExitApp();
return -1;
}
xExcel.ExitApp();
return 0;
}
int CExcelDemo1Dlg::CreateJPGByCliopBoard()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFindFile;
CString strPath;
m_edtPath.GetWindowText(strPath);
if(strPath.GetLength()==0) return -1;
CString strPathEx = strPath;
CString strFileName = "";//文件名
CString strFile = "";
if(strPath.Right(1)!="\\")
strPathEx = strPath+ "\\";
strFile= strPathEx + "*.*";
strFile.Replace('\\', '/');
hFindFile = FindFirstFile(strFile, &FindFileData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
continue;
}
strFileName = FindFileData.cFileName;
if (strcmp(strFileName.Right(4), ".xls") == 0 ||
strcmp(strFileName.Right(5), ".xlsx") == 0)
{
CreateJPGFile(strFileName);
}
}
while(FindNextFile(hFindFile, &FindFileData));
}
FindClose(hFindFile);
return 0;
}
int CExcelDemo1Dlg::BmpToJpg(const unsigned short* szBmpPath, const unsigned short* szJPGPath)
{
int nRet = 0;
Bitmap newbitmap(szBmpPath);
nRet = SaveFile(&newbitmap,szJPGPath);
return nRet;
}
int CExcelDemo1Dlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1;
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1;
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
问题:
1、在程序编译完成后执行程序,会存在报错“Excel服务器出现异常”,具体原因暂时还不知道,我使用的Excel是2013版本的;
2、使用此种方式会在Excel07中出现截取不到图片的情况,2013版本的正常。原因是在剪贴板中取不到数据。
备注:
1、项目完整代码会整理完成后添加链接,链接地址会在本次Demo完结章节中给出。
2、本人所有文章皆为本人个人工作中遇到的问题进行汇总的学习笔记,如存在侵权行为,请及时联系删除。
本文暂时没有评论,来添加一个吧(●'◡'●)