我是靠谱客的博主 儒雅金针菇,最近开发中收集的这篇文章主要介绍C++操作EXCEL类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

头文件

#pragma once
#include <vector>
class CReadExcelClass
{
public:
CReadExcelClass(void);
virtual ~CReadExcelClass(void);
BOOL GetExcelPath(const CString strPath);
BOOL ReadAndGetExcel(int iColumn, long iRow, long iCol, CStringList &strList);
BOOL ReadAllFloatGetExcel(int iColumn, long iRow, long iCol, CStringList &strList);
long GetExcelTotalRow();
long GetExcelTotalClo();
BOOL ReadRandExcel(std::vector<int> vec, long iCol, CStringList &strList);
public:
CString m_strPath;
};

cpp实现

#include "StdAfx.h"
#include "ReadExcelClass.h"
CReadExcelClass::CReadExcelClass(void)
{
m_strPath = "";
}
CReadExcelClass::~CReadExcelClass(void)
{
m_strPath = "";
}
BOOL CReadExcelClass::GetExcelPath(const CString strPath)
{
if (strPath.IsEmpty())
{
return FALSE;
}
m_strPath = strPath;
return TRUE;
}
BOOL CReadExcelClass::ReadAndGetExcel(int iColumn, long iRow, long iCol, CStringList &strList)
{
CStringList strNull;
if (iColumn < 0)
{
return FALSE;
}
long iLine = iRow;
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
return FALSE;
}
CApplication ExcelApp;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if (!ExcelApp.CreateDispatch(L"Excel.Application"))
{
return FALSE;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lp;
books.AttachDispatch(ExcelApp.get_Workbooks());
lp = books.Open(m_strPath, covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional);
book.AttachDispatch(lp);
sheets.AttachDispatch(book.get_Worksheets());
lp = book.get_ActiveSheet();
sheet.AttachDispatch(lp);
range.AttachDispatch(sheet.get_Cells());
for (int i = 0; i < iColumn; i++)
{
for (int j = 0; j < iCol; j++)
{
COleVariant rValue;
CRange rag;
rag.AttachDispatch(range.get_Item(COleVariant((long)iLine),COleVariant((long)j+1)).pdispVal, TRUE);
rValue = rag.get_Value2();
CString str;
/*if (str.Find('/') != -1)
{
str.
}*/
if (rValue.vt == VT_BSTR)
{
str = rValue.bstrVal;
}
else if (rValue.vt == VT_INT)
{
str.Format(_T("%d"), rValue.dblVal);
}
else if (rValue.vt == VT_R8)
{
str.Format(_T("%0.2f"), rValue.dblVal);
}
else if (rValue.vt == VT_EMPTY)
{
str = "";
}
else if (rValue.vt == VT_DATE)
{
SYSTEMTIME st;
VariantTimeToSystemTime(rValue.date, &st);
}
strList.AddTail(str);
}
iLine++;
}
books.Close();
ExcelApp.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
return TRUE;
}
BOOL CReadExcelClass::ReadAllFloatGetExcel(int iColumn, long iRow, long iCol, CStringList &strList)
{
if (iColumn < 0)
{
return FALSE;
}
long iLine = iRow;
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
return FALSE;
}
CApplication ExcelApp;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if (!ExcelApp.CreateDispatch(L"Excel.Application"))
{
return FALSE;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lp;
books.AttachDispatch(ExcelApp.get_Workbooks());
lp = books.Open(m_strPath, covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional);
book.AttachDispatch(lp);
sheets.AttachDispatch(book.get_Worksheets());
lp = book.get_ActiveSheet();
sheet.AttachDispatch(lp);
range.AttachDispatch(sheet.get_Cells());
for (int i = 0; i < iColumn; i++)
{
for (int j = 0; j < iCol; j++)
{
COleVariant rValue;
CRange rag;
rag.AttachDispatch(range.get_Item(COleVariant((long)iLine),COleVariant((long)j+1)).pdispVal, TRUE);
rValue = rag.get_Value2();
CString str;
if (rValue.vt == VT_BSTR)
{
str = rValue.bstrVal;
}
else if (rValue.vt == VT_INT)
{
str.Format(_T("%d"), rValue.dblVal);
}
else if (rValue.vt == VT_R8)
{
str.Format(_T("%f"), rValue.dblVal);
}
else if (rValue.vt == VT_EMPTY)
{
str = "";
}
else if (rValue.vt == VT_DATE)
{
SYSTEMTIME st;
VariantTimeToSystemTime(rValue.date, &st);
}
strList.AddTail(str);
}
iLine++;
}
books.Close();
ExcelApp.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
return TRUE;
}
long CReadExcelClass::GetExcelTotalRow()
{
long row;
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
return FALSE;
}
CApplication ExcelApp;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if (!ExcelApp.CreateDispatch(L"Excel.Application"))
{
return FALSE;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lp;
books.AttachDispatch(ExcelApp.get_Workbooks());
lp = books.Open(m_strPath, covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional);
book.AttachDispatch(lp);
sheets.AttachDispatch(book.get_Worksheets());
lp = book.get_ActiveSheet();
sheet.AttachDispatch(lp);
range.AttachDispatch(sheet.get_UsedRange(),TRUE);
CRange rg;
rg.AttachDispatch(range.get_Rows(),TRUE);
row = rg.get_Count();
books.Close();
ExcelApp.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
return row;
}
long CReadExcelClass::GetExcelTotalClo()
{
long row;
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
return FALSE;
}
CApplication ExcelApp;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if (!ExcelApp.CreateDispatch(L"Excel.Application"))
{
return FALSE;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lp;
books.AttachDispatch(ExcelApp.get_Workbooks());
lp = books.Open(m_strPath, covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional);
book.AttachDispatch(lp);
sheets.AttachDispatch(book.get_Worksheets());
lp = book.get_ActiveSheet();
sheet.AttachDispatch(lp);
range.AttachDispatch(sheet.get_UsedRange(),TRUE);
CRange rg;
rg.AttachDispatch(range.get_Columns(),TRUE);
row = rg.get_Count();
books.Close();
ExcelApp.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
return row;
}
BOOL CReadExcelClass::ReadRandExcel(std::vector<int> vec, long iCol, CStringList &strList)
{
if (iCol < 0)
{
return FALSE;
}
HRESULT hr;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
return FALSE;
}
CApplication ExcelApp;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if (!ExcelApp.CreateDispatch(L"Excel.Application"))
{
return FALSE;
}
CWorkbooks books;
CWorkbook book;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange iCell;
LPDISPATCH lp;
books.AttachDispatch(ExcelApp.get_Workbooks());
lp = books.Open(m_strPath, covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,
covOptional,covOptional,covOptional,covOptional);
book.AttachDispatch(lp);
sheets.AttachDispatch(book.get_Worksheets());
lp = book.get_ActiveSheet();
sheet.AttachDispatch(lp);
range.AttachDispatch(sheet.get_Cells());
for (unsigned int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < iCol; j++)
{
COleVariant rValue;
CRange rag;
rag.AttachDispatch(range.get_Item(COleVariant((long)vec[i]),COleVariant((long)j+1)).pdispVal, TRUE);
rValue = rag.get_Value2();
CString str;
if (rValue.vt == VT_BSTR)
{
str = rValue.bstrVal;
}
else if (rValue.vt == VT_INT)
{
str.Format(_T("%d"), rValue.dblVal);
}
else if (rValue.vt == VT_R8)
{
str.Format(_T("%0.2f"), rValue.dblVal);
}
else if (rValue.vt == VT_EMPTY)
{
str = "";
}
else if (rValue.vt == VT_DATE)
{
SYSTEMTIME st;
VariantTimeToSystemTime(rValue.date, &st);
}
strList.AddTail(str);
}
}
books.Close();
ExcelApp.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
return TRUE;
}

文中的CString可换成std::string

最后

以上就是儒雅金针菇为你收集整理的C++操作EXCEL类的全部内容,希望文章能够帮你解决C++操作EXCEL类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部