vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

接前面所寫文章,繼續編寫程序。

按照此方法實現的時候,我們需要使用GDIPlus開源庫實現圖片的轉化功能。因此我們需要在vc開發環境中設置庫路徑。

GDIPlus下載地址:https://download.csdn.net/download/u013970791/12200574

下載完成後解壓到本機電腦的一個路徑下面即可

接下來在vc6.0中導入GDIPlus開源庫

選擇vc6.0的菜單欄“Tool”-》“Options”,在彈出的對話框中找到Directories標籤頁,在Show directories for:下拉框選項中選擇“Include files”,在“Directories”的右側點擊第一個小圖標

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

會在列表中出現一行空白行,此時點擊空白行後面的“...”按鈕,找到解壓之後的GDIPlus文件夾中的“Include”文件夾,選中之後雙擊即可完成“Include”文件夾的添加。

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

切換“Show Directories for:”下拉框選項選擇“Library files”選項,找到GDIPlus文件夾中的“Lib”文件夾雙擊添加進去即可。

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

選擇vc6.0的菜單欄“Project”-》“Setting”,在彈出的對話框界面找到“Link”標籤頁,在Object/library modules:輸入框中輸入:GDIPlus.lib

打開“FileView”視圖,雙擊打開StdAfx.h 文件,添加如下代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

打開“FileView”視圖,雙擊打開ExcelDemo1.h 文件,添加如下代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

打開“FileView”視圖,雙擊打開ExcelDemo1.cpp 文件,添加如下代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現


vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

完成上述的操作後,點擊鍵盤“F7”快捷鍵編譯程序,此時會發現編譯的時候報了好多的錯誤,這個錯誤的原因是因為Excel中的類型名與GDIPlus中的類型名衝突導致的,解決方法如下:

1、在“FileView”視圖中雙擊《excel.h》文件打開,在文件的開頭位置添加代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

2、拖動滑塊置文件的末尾處,在文件的末尾添加}符號:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

3、在“FileView”視圖中雙擊《excel.cpp》文件打開,在文件中添加如下代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

4、拖動滑塊置文件的末尾處,在文件的末尾添加}符號:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

5、在“FileView”視圖中雙擊《XExcel.h》文件打開,在文件中添加如下代碼:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

完成上述步驟之後,重新點擊鍵盤快捷鍵“F7”重新編譯程序,類型名衝突的報錯信息將會消失。

打開“FileView”視圖,雙擊打開ExcelDemo1Dlg.h 文件,在文件中導入XExcel.h頭文件,如下所示:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

打開“ResourceView”視圖,找到IDD_EXCELDEMO1_DIALOG對話框,雙擊打開該對話框,效果圖如下所示:

vc操作Excel實現截取指定區域生成jpg圖片7-利用剪貼板實現

雙擊對話框中的“確定”按鈕,為“確定”按鈕增加響應函數。操作完成後進入“確定”按鈕實現函數處進行代碼的編輯工作。

在這裡我們首先為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 文件,實現成員函數的功能:

<code> int CExcelDemo1Dlg::SaveFile(Bitmap* pImage, const wchar_t* pFileName)
{
\tEncoderParameters encoderParameters;
\tCLSID jpgClsid;
\tint nRet = GetEncoderClsid(L"image/jpeg", &jpgClsid);
\tif (nRet < 0)
\t{
\t\treturn -1;
\t}
\tencoderParameters.Count = 1;
\tencoderParameters.Parameter[0].Guid = EncoderQuality;
\tencoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
\tencoderParameters.Parameter[0].NumberOfValues = 1;
\t
\tULONG quality;
\tquality = 100;
\tencoderParameters.Parameter[0].Value = &quality;
\tStatus status = pImage->Save(pFileName, &jpgClsid, &encoderParameters);

\tif (status != Ok)
\t{
\t\treturn -1;
\t}
\t
\treturn 0;
}/<code>
<code>int CExcelDemo1Dlg::SelectDirPath(CString& strDirPath)
{
\tCWnd* pMainCWnd = NULL;
\tpMainCWnd = GetActiveWindow();
\tHWND hWnd = pMainCWnd->GetSafeHwnd();
\t
strDirPath.Empty();
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
\treturn 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))
{
\tif (szBuffer[0] == '\\0')
\t\treturn 0;
\tstrDirPath = szBuffer;
\treturn 1;
}
\telse
\t\treturn 1;
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return 1;
}/<code>
<code>int CExcelDemo1Dlg::CreateJPGFile(const CString& strFilepath)
{
\tCString strExcelPath = "";
\tCString strDirPath = "";

\tCString strSavePath = "";
\tCString strFileName = "";

\tint nPos = strFilepath.Find(".");
\tif (nPos > 0)
\t{
\t\tstrFileName = strFilepath.Mid(0, nPos);
\t}

\tstrSavePath.Format("%s%s", m_strSaveJPGDirPath, strFileName);
\tm_edtPath.GetWindowText(strDirPath);
\tstrExcelPath.Format("%s%s", strDirPath, strFilepath);

\tCXExcel xExcel;
\tif(xExcel.InitApp()<0) return -1;

\tint nRet = xExcel.Open(strExcelPath);
\tif (nRet < 0)
\t{
\t\tCString strMsg = "";
\t\tstrMsg.Format("打開文件失敗:%s", strExcelPath);
\t\tAfxMessageBox(strMsg);
\t\txExcel.ExitApp();
\t}

\tCString strBeginArea = "", strEndArea = "";
\tm_edtBegin.GetWindowText(strBeginArea);
\tm_edtEnd.GetWindowText(strEndArea);
\t
\tif (strBeginArea.IsEmpty() || strEndArea.IsEmpty())
\t{
\t\tAfxMessageBox("請錄入完整的截圖區域!");
\t\txExcel.ExitApp();
\t\treturn -1;
\t}

\tBOOL bRet = xExcel.SaveExcelCellToPic(strBeginArea, strEndArea, strSavePath + ".bmp");
\tif (!bRet)
\t{
\t\tAfxMessageBox("生成圖片失敗!");
\t\txExcel.ExitApp();
\t\treturn -1;
\t}

\tunsigned short szBmpPath[MAX_PATH] = {0};
\tunsigned short szJpgPath[MAX_PATH] = {0};
\tMultiByteToWideChar(CP_ACP, 0, strSavePath + ".bmp", -1, szBmpPath, MAX_PATH);
\tMultiByteToWideChar(CP_ACP, 0, strSavePath + ".jpg", -1, szJpgPath, MAX_PATH);

\tnRet = BmpToJpg(szBmpPath, szJpgPath);
\tif (nRet == 0)
\t{
\t\t::DeleteFile(strSavePath + ".bmp");
\t}
\telse
\t{
\t\tAfxMessageBox("圖片轉化失敗!");
\t\txExcel.ExitApp();
\t\treturn -1;
\t}

\txExcel.ExitApp();
\treturn 0;
}/<code>
<code>int CExcelDemo1Dlg::CreateJPGByCliopBoard()
{
\tWIN32_FIND_DATA FindFileData;
HANDLE hFindFile;
\tCString strPath;
\tm_edtPath.GetWindowText(strPath);

if(strPath.GetLength()==0)\t\treturn -1;
\t
\tCString strPathEx = strPath;
\tCString strFileName = "";//文件名
\tCString strFile = "";
\tif(strPath.Right(1)!="\\\")
strPathEx = strPath+ "\\\";
\tstrFile= strPathEx + "*.*";
\t
strFile.Replace('\\\\', '/');
hFindFile = FindFirstFile(strFile, &FindFileData);
if(hFindFile!=INVALID_HANDLE_VALUE)
{
do
{
\tif ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
\t{
\t\tcontinue;
\t}
\t
\tstrFileName = FindFileData.cFileName;
\tif (strcmp(strFileName.Right(4), ".xls") == 0 ||
\t\tstrcmp(strFileName.Right(5), ".xlsx") == 0)
\t{
\t\tCreateJPGFile(strFileName);
\t}\t\t
}

while(FindNextFile(hFindFile, &FindFileData));
}
FindClose(hFindFile);

return 0;
}/<code>
<code>int CExcelDemo1Dlg::BmpToJpg(const unsigned short* szBmpPath, const unsigned short* szJPGPath)
{
\tint nRet = 0;
\t
\tBitmap newbitmap(szBmpPath);
\tnRet = SaveFile(&newbitmap,szJPGPath);
\t
\treturn nRet;
}/<code>
<code>int CExcelDemo1Dlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
\tUINT num = 0;
\tUINT size = 0;
\tImageCodecInfo* pImageCodecInfo = NULL;
\tGetImageEncodersSize(&num, &size);
\tif(size == 0)
\t\treturn -1;
\tpImageCodecInfo = (ImageCodecInfo*)(malloc(size));
\tif(pImageCodecInfo == NULL)
\t\treturn -1;
\tGetImageEncoders(num, size, pImageCodecInfo);
\tfor(UINT j = 0; j < num; ++j)
\t{
\t\tif( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
\t\t{
\t\t\t*pClsid = pImageCodecInfo[j].Clsid;
\t\t\tfree(pImageCodecInfo);
\t\t\treturn j;
\t\t}
\t}
\tfree(pImageCodecInfo);
return -1;
}/<code>

問題:

1、在程序編譯完成後執行程序,會存在報錯“Excel服務器出現異常”,具體原因暫時還不知道,我使用的Excel是2013版本的;

2、使用此種方式會在Excel07中出現截取不到圖片的情況,2013版本的正常。原因是在剪貼板中取不到數據。

備註:

1、項目完整代碼會整理完成後添加鏈接,鏈接地址會在本次Demo完結章節中給出。

2、本人所有文章皆為本人個人工作中遇到的問題進行彙總的學習筆記,如存在侵權行為,請及時聯繫刪除。


分享到:


相關文章: