导出列表框中的数据到excel文件

wxj221217 2005-07-14 11:42:31
我首先查询出一些数据到ListControl,然后把数据导出到excel文件,用的是com组件,点击倒出按钮启动excel程序窗口,数据添加到窗口中
这时如果点任务栏下原来界面窗口,则好长时间才会弹出,又没有什么办法,不要出现
高手能给出一些例子吗
...全文
223 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qrlvls 2005-07-14
  • 打赏
  • 举报
回复
void CExportXLSDlg::__GetCellName(int nRow, int nCol, CString &strName)
{
int nSeed = nCol;
CString strRow;

strName = _T("");

while (nSeed > 0)
{
strName = (TCHAR)('A' + (nSeed-1)%26) + strName;
nSeed --;
nSeed /= 26;
}

strRow.Format(_T("%d"), nRow);
strName += strRow;
}
qrlvls 2005-07-14
  • 打赏
  • 举报
回复
void CExportXLSDlg::__SetExcelCellText(_Worksheet &objWorkSheet, int nRow, int nCol, CString &strText, int nHeight, int nWidth, int nAlignment)
{
Range objRange;
CString strCellFrom;
CString strCellTo;
COleVariant varOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);

int nColFrom, nRowFrom;
int nColTo, nRowTo;

nRowFrom = nRow;
nRowTo = nRowFrom + nHeight - 1;

nColFrom = nCol;
nColTo = nColFrom + nWidth - 1;

ASSERT(nRowFrom > 0 && nRowTo >= nRow);
ASSERT(nColFrom > 0 && nColTo >= nCol);

__GetCellName(nRowFrom, nColFrom, strCellFrom);
__GetCellName(nRowTo, nColTo, strCellTo);

objRange = objWorkSheet.GetRange(COleVariant(strCellFrom), COleVariant(strCellTo));
if (nWidth>1 || nHeight>1)
objRange.Merge(COleVariant((short)0));
objRange.SetValue(COleVariant(strText));
objRange.BorderAround(COleVariant((short)1), (long)2, (long)1, varOptional);
objRange.SetHorizontalAlignment(COleVariant((short)nAlignment));
objRange.SetVerticalAlignment(COleVariant((short)2));
objRange.SetColumnWidth(COleVariant((short)10));
}
qrlvls 2005-07-14
  • 打赏
  • 举报
回复
DWORD WINAPI CExportXLSDlg::_ExportThread(LPVOID pParameter)
{
CExportXLSDlg *pDlg = (CExportXLSDlg*)pParameter;

_Application *pExcelApp;
Workbooks objExcelBooks;
Sheets objExcelSheets;
_Workbook objWorkBook;
_Worksheet objWorkSheet;
Range objRange;

COleVariant varOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant varTrue((short)TRUE);
COleVariant varFalse((short)FALSE);
MSG msg;
CString strText;

int nMilliTime = 0;
int nSequence = 0, nLastSequence = -1;
int nRow = 1;
int i;
double dblValue[TOTAL_CHANNEL_NUM];

CoInitialize(NULL); // 因为对于COM操作必须对每个线程引用 COM 库

pDlg->m_staticProgress.SetWindowText(_T("正在创建Excel实例..."));

pExcelApp = new _Application;
pExcelApp->CreateDispatch(_T("Excel.Application")); // 创建 Excel 应用程序

objExcelBooks = pExcelApp->GetWorkbooks();
objExcelSheets = objExcelBooks.Add(varOptional);
objWorkBook.AttachDispatch(pExcelApp->GetApplication());
objExcelSheets = objWorkBook.GetSheets();


// 先导出历史数据相关信息
pDlg->m_staticProgress.SetWindowText(_T("正在导出历史数据信息..."));
objWorkSheet = objExcelSheets.GetItem(COleVariant((short)1));
objWorkSheet.SetName(_T("历史数据信息"));
objWorkSheet.Activate();

__ExportInfomation(objWorkSheet);

// 导出历史试验数据
pDlg->m_staticProgress.SetWindowText(_T("正在导出历史试验数据..."));
objWorkSheet = objExcelSheets.GetItem(COleVariant((short)2));
objWorkSheet.SetName(_T("试验数据"));
objWorkSheet.Activate();
__ExportDataHead(objWorkSheet);

nSequence = 0;
while (nSequence < g_nHistoryCount)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
}

if (nSequence > nLastSequence)
{
nRow ++;

__GetTimeString(nSequence, strText);
__SetExcelCellText(objWorkSheet, nRow, 1, strText, 1, 2);

__GetHistoryRecord(nSequence, dblValue);
strText.Format(_T("%.3f"), dblValue[BASEID_COOL]);
__SetExcelCellText(objWorkSheet, nRow, 3, strText);
for (i=0; i<T_CHANNEL_NUM; i++)
{
strText.Format(_T("%.3f"), dblValue[BASEID_T + i]);
__SetExcelCellText(objWorkSheet, nRow, 4+i, strText);
}
for (i=0; i<K_CHANNEL_NUM; i++)
{
strText.Format(_T("%.3f"), dblValue[BASEID_K + i]);
__SetExcelCellText(objWorkSheet, nRow, 50+i, strText);
}
for (i=0; i<A_CHANNEL_NUM; i++)
{
strText.Format(_T("%.3f"), dblValue[BASEID_A + i]);
__SetExcelCellText(objWorkSheet, nRow, 58+i, strText);
}
strText.Format(_T("%d / %d"), nSequence, g_nHistoryCount);
pDlg->m_staticProgress.SetWindowText(strText);
pDlg->m_slidProgress.Value = nSequence * 100.0 / g_nHistoryCount;

nLastSequence = nSequence;
}

nMilliTime += pDlg->m_nMilliSecond;
nSequence = nMilliTime * (g_sHistoryConfig.dblHighSampleRate / PCI9114_CHANNELS / 1000.0);
}

// 切换回到历史信息页
objWorkSheet = objExcelSheets.GetItem(COleVariant((short)1));
objWorkSheet.Activate();

pExcelApp->SetVisible(TRUE); // 显示 Excel 应用程序
delete pExcelApp;

CoUninitialize(); // 因为对于COM操作必须对每个线程引用 COM 库

pDlg->m_hThread = NULL;
::PostMessage(pDlg->m_hWnd, WM_CLOSE, 0, 0);

return 0;
}

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧