#import "C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE11\\MSO.DLL" rename( "RGB", "MSORGB" )
using namespace Office;
#pragma warning(disable : 4192)
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
#import "D:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" )
void CRecordView::OnFileExport()
{
TCHAR szFilter[] = { _T("Excel文件 (*.xls)|*.xls|所有文件 (*.*)|*.*||") };
CFileDialog SaveDialog(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
if (SaveDialog.DoModal() == IDOK)
{
_variant_t varOption((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CWaitCursor wait;
try
{
Excel::_ApplicationPtr pExcelApp(_T("Excel.Application"));
Excel::_WorkbookPtr pBook = pExcelApp->Workbooks->Add(varOption);
Excel::_WorksheetPtr pSheet = pBook->ActiveSheet;
//Excel::RangePtr pRange = pSheet->GetRange(_T("A1"), _T("D1"));
Excel::RangePtr pRange = pSheet->Cells;
CHeaderCtrl* pHeader = m_wndList.GetHeaderCtrl();
int nColumn = pHeader->GetItemCount();
{
TCHAR szText[40] = { 0 };
HDITEM hdi = { 0 };
hdi.mask = HDI_TEXT;
hdi.pszText = szText;
hdi.cchTextMax = 40;
for (int i = 1; i < nColumn; i++)
{
pHeader->GetItem(i, &hdi);
pRange->Item[1][i] = hdi.pszText;
}
Excel::RangePtr pHeaderRange = pRange->GetRange(variant_t(_T("A1")), variant_t(_T("F1")));
pHeaderRange->Font->Bold = true;
}
for (int i = 0; i < m_wndList.GetItemCount(); i++)
{
for (int j = 1; j < nColumn; j++)
{
pRange->Item[i+2][j] = (LPCTSTR)m_wndList.GetItemText(i, j);
}
}
pRange = pRange->EntireColumn;
pRange->AutoFit();
pBook->Close(true, (LPCTSTR)SaveDialog.GetPathName(), varOption);
pExcelApp->Quit();
AfxMessageBox(_T("数据已成功导出到Excel表格中!"), MB_OK | MB_ICONINFORMATION);
}
catch (...)
{
AfxMessageBox(_T("导出数据错误!"), MB_OK | MB_ICONERROR);
return;
}
}
}
posted on 2007-09-10 12:25
披星戴月 阅读(1057)
评论(0) 编辑 收藏 引用 所属分类:
Win32