Excel 각 필드에 연속되는 값을 빠르게 삽입하기 위한 함수 형태.

 

CStringArray에 입력할 정보들을 추가하여 넘겨주고 내부 for문을 돌며 정보를 삽입한다.

->하나씩 접근하는 방식에 3배 정도 빨라지게 된다.

CRange range;
range.AttachDispatch(sheet.get_Cells(), true);

...

range.Select();

를 반복하면서 시간이 많이 걸린다.

 

해당 함수를 활용하여 원하는 정보 List를 빠르게 Excel에 입력하자.

 



void SetValueListData2(bool *pbRun, int nSheet,
		CString strCharCol1, int nStartRow1, CStringArray *pstrNewValueList1);



void CExcelCtrl::SetValueListData2(bool *pbRun, int nSheet,
	CString strCharCol1, int nStartRow1, CStringArray *pstrNewValueList1)
{
	if (nSheet < 1 || m_app == nullptr || 
		pstrNewValueList1 == nullptr)
		return;

	try
	{

		int nCnt1 = pstrNewValueList1->GetCount();
		if (nCnt1 > 0)
		{
			// sheet 생성, 연결 (1번 시트)
			CWorksheet sheet;
			sheet = m_worksheets.get_Item(COleVariant((short)nSheet));
			sheet.Activate();
			// range 생성, 연결
			CRange range;
			range.AttachDispatch(sheet.get_Cells(), true);

			CString strCellPos;

			for (int i = 0; i < nCnt1; i++)
			{
				if (*pbRun == false)//쓰기 중지.
					break;

				strCellPos.Format(L"%s%d", strCharCol1, nStartRow1 + i);
				range = sheet.get_Range(COleVariant(strCellPos.GetBuffer(strCellPos.GetAllocLength())), m_covOptional);
				//Data 쓰기.
				range.put_Value2(COleVariant(pstrNewValueList1->GetAt(i)));
			}
			//------------------------------------------------------
			range.ReleaseDispatch();
			sheet.ReleaseDispatch();
		}

	}
	catch (CMemoryException* e)
	{
		CMemoryException* ep = e;
		AfxMessageBox(L"CMemoryException Could not clean up workbook.");
	}
	catch (CFileException* e)
	{
		CFileException* pe = e;
		AfxMessageBox(L"CFileException Could not clean up workbook.");
	}
	catch (CException* e)
	{
		CException* pe = e;
		AfxMessageBox(L"CException Could not clean up workbook.");
	}
}

 

+ Recent posts