《C++读写EXCEL文件方式比较》。
我的代码参考的地方是这儿,再次感谢原作者
http://blog.csdn.net/gyssoft/archive/2007/04/29/1592104.aspx
我根据自己的需要做了整理,干净了一点,而后根据发现的速度问题做了一些优化。
预加载的思路来自这个帖子
http://topic.csdn.net/t/20030626/21/1962211.html
其实思路很简单,不再一个CELL一个CELL的伛数据,而是一次把表格里面所有的数据读取出来处理。
.h文件的源码代码如下:
其中的头文件都是OLE的头文件。如何导出可以参考
http://blog.csdn.net/wyz365889/article/details/7599924
我自己这儿一直保存了一套别人生成的这几个文件,也可以用。大家可以找找有没有下载的,不过我不太确认跨版本是否可行。
还有既然是OLE,你一定要安装EXCEL的。
- #pragma once
-
- #include <CRange.h>
- #include <CWorkbook.h>
- #include <CWorkbooks.h>
- #include <CWorksheet.h>
- #include <CWorksheets.h>
- #include <CApplication.h>
-
- class IllusionExcelFile
- {
-
- public:
-
-
- IllusionExcelFile();
- virtual ~IllusionExcelFile();
-
- protected:
-
- CString open_excel_file_;
-
-
- CWorkbooks excel_books_;
-
- CWorkbook excel_work_book_;
-
- CWorksheets excel_sheets_;
-
- CWorksheet excel_work_sheet_;
-
- CRange excel_current_range_;
-
-
-
- BOOL already_preload_;
-
- COleSafeArray ole_safe_array_;
-
- protected:
-
-
- static CApplication excel_application_;
- public:
-
-
- void ShowInExcel(BOOL bShow);
-
-
- BOOL IsCellString(long iRow, long iColumn);
-
- BOOL IsCellInt(long iRow, long iColumn);
-
-
- CString GetCellString(long iRow, long iColumn);
-
- int GetCellInt(long iRow, long iColumn);
-
- double GetCellDouble(long iRow, long iColumn);
-
-
- int GetRowCount();
-
- int GetColumnCount();
-
-
- BOOL LoadSheet(long table_index,BOOL pre_load = FALSE);
-
- BOOL LoadSheet(const char* sheet,BOOL pre_load = FALSE);
-
- CString GetSheetName(long table_index);
-
-
- int GetSheetCount();
-
-
- BOOL OpenExcelFile(const char * file_name);
-
- void CloseExcelFile(BOOL if_save = FALSE);
-
- void SaveasXSLFile(const CString &xls_file);
-
- CString GetOpenFileName();
-
- CString GetLoadSheetName();
-
-
- void SetCellInt(long irow, long icolumn,int new_int);
-
- void SetCellString(long irow, long icolumn,CString new_string);
-
- public:
-
- static BOOL InitExcel();
-
- static void ReleaseExcel();
-
- static char *GetColumnName(long iColumn);
-
- protected:
-
-
- void PreLoadSheet();
- };
CPP文件的与代码如下:
-
-
-
-
-
- #include "StdAfx.h"
- #include "illusion_excel_file.h"
-
-
-
- COleVariant
- covTrue((short)TRUE),
- covFalse((short)FALSE),
- covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
-
- CApplication IllusionExcelFile::excel_application_;
-
-
- IllusionExcelFile::IllusionExcelFile():
- already_preload_(FALSE)
- {
- }
-
- IllusionExcelFile::~IllusionExcelFile()
- {
-
- CloseExcelFile();
- }
-
-
- BOOL IllusionExcelFile::InitExcel()
- {
-
-
- if (!excel_application_.CreateDispatch("Excel.Application",NULL))
- {
- AfxMessageBox("创建Excel服务失败,你可能没有安装EXCEL,请检查!");
- return FALSE;
- }
-
- excel_application_.put_DisplayAlerts(FALSE);
- return TRUE;
- }
-
- void IllusionExcelFile::ReleaseExcel()
- {
- excel_application_.Quit();
- excel_application_.ReleaseDispatch();
- excel_application_=NULL;
- }
-
- BOOL IllusionExcelFile::OpenExcelFile(const char *file_name)
- {
-
- CloseExcelFile();
-
-
- excel_books_.AttachDispatch(excel_application_.get_Workbooks(),true);
-
- LPDISPATCH lpDis = NULL;
- lpDis = excel_books_.Add(COleVariant(file_name));
- if (lpDis)
- {
- excel_work_book_.AttachDispatch(lpDis);
-
- excel_sheets_.AttachDispatch(excel_work_book_.get_Worksheets(),true);
-
-
- open_excel_file_ = file_name;
-
- return TRUE;
- }
-
- return FALSE;
- }
-
- void IllusionExcelFile::CloseExcelFile(BOOL if_save)
- {
-
- if (open_excel_file_.IsEmpty() == FALSE)
- {
-
- if (if_save)
- {
- ShowInExcel(TRUE);
- }
- else
- {
-
- excel_work_book_.Close(COleVariant(short(FALSE)),COleVariant(open_excel_file_),covOptional);
- excel_books_.Close();
- }
-
-
- open_excel_file_.Empty();
- }
-
-
-
- excel_sheets_.ReleaseDispatch();
- excel_work_sheet_.ReleaseDispatch();
- excel_current_range_.ReleaseDispatch();
- excel_work_book_.ReleaseDispatch();
- excel_books_.ReleaseDispatch();
- }
-
- void IllusionExcelFile::SaveasXSLFile(const CString &xls_file)
- {
- excel_work_book_.SaveAs(COleVariant(xls_file),
- covOptional,
- covOptional,
- covOptional,
- covOptional,
- covOptional,
- 0,
- covOptional,
- covOptional,
- covOptional,
- covOptional,
- covOptional);
- return;
- }
-
-
- int IllusionExcelFile::GetSheetCount()
- {
- return excel_sheets_.get_Count();
- }
-
-
- CString IllusionExcelFile::GetSheetName(long table_index)
- {
- CWorksheet sheet;
- sheet.AttachDispatch(excel_sheets_.get_Item(COleVariant((long)table_index)),true);
- CString name = sheet.get_Name();
- sheet.ReleaseDispatch();
- return name;
- }
-
- BOOL IllusionExcelFile::LoadSheet(long table_index,BOOL pre_load)
- {
- LPDISPATCH lpDis = NULL;
- excel_current_range_.ReleaseDispatch();
- excel_work_sheet_.ReleaseDispatch();
- lpDis = excel_sheets_.get_Item(COleVariant((long)table_index));
- if (lpDis)
- {
- excel_work_sheet_.AttachDispatch(lpDis,true);
- excel_current_range_.AttachDispatch(excel_work_sheet_.get_Cells(), true);
- }
- else
- {
- return FALSE;
- }
-
- already_preload_ = FALSE;
-
- if (pre_load)
- {
- PreLoadSheet();
- already_preload_ = TRUE;
- }
-
- return TRUE;
- }
-
- BOOL IllusionExcelFile::LoadSheet(const char* sheet,BOOL pre_load)
- {
- LPDISPATCH lpDis = NULL;
- excel_current_range_.ReleaseDispatch();
- excel_work_sheet_.ReleaseDispatch();
- lpDis = excel_sheets_.get_Item(COleVariant(sheet));
- if (lpDis)
- {
- excel_work_sheet_.AttachDispatch(lpDis,true);
- excel_current_range_.AttachDispatch(excel_work_sheet_.get_Cells(), true);
-
- }
- else
- {
- return FALSE;
- }
-
- already_preload_ = FALSE;
-
- if (pre_load)
- {
- already_preload_ = TRUE;
- PreLoadSheet();
- }
-
- return TRUE;
- }
-
- int IllusionExcelFile::GetColumnCount()
- {
- CRange range;
- CRange usedRange;
- usedRange.AttachDispatch(excel_work_sheet_.get_UsedRange(), true);
- range.AttachDispatch(usedRange.get_Columns(), true);
- int count = range.get_Count();
- usedRange.ReleaseDispatch();
- range.ReleaseDispatch();
- return count;
- }
-
- int IllusionExcelFile::GetRowCount()
- {
- CRange range;
- CRange usedRange;
- usedRange.AttachDispatch(excel_work_sheet_.get_UsedRange(), true);
- range.AttachDispatch(usedRange.get_Rows(), true);
- int count = range.get_Count();
- usedRange.ReleaseDispatch();
- range.ReleaseDispatch();
- return count;
- }
-
- BOOL IllusionExcelFile::IsCellString(long irow, long icolumn)
- {
- CRange range;
- range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true);
- COleVariant vResult =range.get_Value2();
-
- if(vResult.vt == VT_BSTR)
- {
- return TRUE;
- }
- return FALSE;
- }
-
- BOOL IllusionExcelFile::IsCellInt(long irow, long icolumn)
- {
- CRange range;
- range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true);
- COleVariant vResult =range.get_Value2();
-
- if(vResult.vt == VT_INT || vResult.vt == VT_R8)
- {
- return TRUE;
- }
- return FALSE;
- }
-
- CString IllusionExcelFile::GetCellString(long irow, long icolumn)
- {
-
- COleVariant vResult ;
- CString str;
-
- if (already_preload_ == FALSE)
- {
- CRange range;
- range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true);
- vResult =range.get_Value2();
- range.ReleaseDispatch();
- }
-
- else
- {
- long read_address[2];
- VARIANT val;
- read_address[0] = irow;
- read_address[1] = icolumn;
- ole_safe_array_.GetElement(read_address, &val);
- vResult = val;
- }
-
- if(vResult.vt == VT_BSTR)
- {
- str=vResult.bstrVal;
- }
-
- else if (vResult.vt==VT_INT)
- {
- str.Format("%d",vResult.pintVal);
- }
-
- else if (vResult.vt==VT_R8)
- {
- str.Format("%0.0f",vResult.dblVal);
- }
-
- else if(vResult.vt==VT_DATE)
- {
- SYSTEMTIME st;
- VariantTimeToSystemTime(vResult.date, &st);
- CTime tm(st);
- str=tm.Format("%Y-%m-%d");
-
- }
-
- else if(vResult.vt==VT_EMPTY)
- {
- str="";
- }
-
- return str;
- }
-
- double IllusionExcelFile::GetCellDouble(long irow, long icolumn)
- {
- double rtn_value = 0;
- COleVariant vresult;
-
- if (already_preload_ == FALSE)
- {
- CRange range;
- range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true);
- vresult =range.get_Value2();
- range.ReleaseDispatch();
- }
-
- else
- {
- long read_address[2];
- VARIANT val;
- read_address[0] = irow;
- read_address[1] = icolumn;
- ole_safe_array_.GetElement(read_address, &val);
- vresult = val;
- }
-
- if (vresult.vt==VT_R8)
- {
- rtn_value = vresult.dblVal;
- }
-
- return rtn_value;
- }
-
- int IllusionExcelFile::GetCellInt(long irow, long icolumn)
- {
- int num;
- COleVariant vresult;
-
- if (already_preload_ == FALSE)
- {
- CRange range;
- range.AttachDispatch(excel_current_range_.get_Item (COleVariant((long)irow),COleVariant((long)icolumn)).pdispVal, true);
- vresult = range.get_Value2();
- range.ReleaseDispatch();
- }
- else
- {
- long read_address[2];
- VARIANT val;
- read_address[0] = irow;
- read_address[1] = icolumn;
- ole_safe_array_.GetElement(read_address, &val);
- vresult = val;
- }
-
- num = static_cast<int>(vresult.dblVal);
-
- return num;
- }
-
- void IllusionExcelFile::SetCellString(long irow, long icolumn,CString new_string)
- {
- COleVariant new_value(new_string);
- CRange start_range = excel_work_sheet_.get_Range(COleVariant("A1"),covOptional);
- CRange write_range = start_range.get_Offset(COleVariant((long)irow -1),COleVariant((long)icolumn -1) );
- write_range.put_Value2(new_value);
- start_range.ReleaseDispatch();
- write_range.ReleaseDispatch();
-
- }
-
- void IllusionExcelFile::SetCellInt(long irow, long icolumn,int new_int)
- {
- COleVariant new_value((long)new_int);
-
- CRange start_range = excel_work_sheet_.get_Range(COleVariant("A1"),covOptional);
- CRange write_range = start_range.get_Offset(COleVariant((long)irow -1),COleVariant((long)icolumn -1) );
- write_range.put_Value2(new_value);
- start_range.ReleaseDispatch();
- write_range.ReleaseDispatch();
- }
-
-
- void IllusionExcelFile::ShowInExcel(BOOL bShow)
- {
- excel_application_.put_Visible(bShow);
- excel_application_.put_UserControl(bShow);
- }
-
- CString IllusionExcelFile::GetOpenFileName()
- {
- return open_excel_file_;
- }
-
- CString IllusionExcelFile::GetLoadSheetName()
- {
- return excel_work_sheet_.get_Name();
- }
-
- char *IllusionExcelFile::GetColumnName(long icolumn)
- {
- static char column_name[64];
- size_t str_len = 0;
-
- while(icolumn > 0)
- {
- int num_data = icolumn % 26;
- icolumn /= 26;
- if (num_data == 0)
- {
- num_data = 26;
- icolumn--;
- }
- column_name[str_len] = (char)((num_data-1) + ‘A‘ );
- str_len ++;
- }
- column_name[str_len] = ‘\0‘;
-
- _strrev(column_name);
-
- return column_name;
- }
-
- void IllusionExcelFile::PreLoadSheet()
- {
-
- CRange used_range;
-
- used_range = excel_work_sheet_.get_UsedRange();
-
-
- VARIANT ret_ary = used_range.get_Value2();
- if (!(ret_ary.vt & VT_ARRAY))
- {
- return;
- }
-
- ole_safe_array_.Clear();
- ole_safe_array_.Attach(ret_ary);
- }