当前位置:软件学习 > Excel >>

一个vc的clistctrl的数据写到excel的方法

首先包含头文件和初始化COM接口,一定记得初始化com啊,调用CoInitialize(NULL);
 
#include <odbcinst.h>
#include <afxdb.h>
#include <comdef.h>
void ExportListToExcel(CListCtrl *pList, CString sExcelFile, CString sSheetName)
{
_Worksheet m_wsExcelSingle;
Worksheets m_wsExcels;
_Workbook m_wbExcelSingle;
Workbooks m_wbExcels;
_Application m_appExcel;
    Range m_rangeExcel;
 
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
 
if (!m_appExcel.CreateDispatch("Excel.Application",NULL))
{
MessageBox("创建Excel服务失败!","提示",MB_OK|MB_ICONWARNING);
return;
}
 
m_appExcel.SetVisible(false);
m_wbExcels.AttachDispatch(m_appExcel.GetWorkbooks(),true);
 
m_wbExcelSingle.AttachDispatch(m_wbExcels.Add(covOptional));
 
//得到Worksheets
m_wsExcels.AttachDispatch(m_wbExcelSingle.GetWorksheets(),true);
//删除多余的表
m_wsExcelSingle.AttachDispatch(m_wsExcels.GetItem(COleVariant((short)3)));
m_wsExcelSingle.Delete();
m_wsExcelSingle.AttachDispatch(m_wsExcels.GetItem(COleVariant((short)2)));
m_wsExcelSingle.Delete();
//表改名
m_wsExcelSingle.AttachDispatch(m_wsExcels.GetItem(COleVariant((short)1)));
m_wsExcelSingle.SetName(sSheetName);
m_rangeExcel.AttachDispatch(m_wsExcelSingle.GetUsedRange(), TRUE);
 
if (pList->GetItemCount ()>0)  
{
int nItemCount,nColumnCount;
 
nColumnCount = pList->GetHeaderCtrl()->GetItemCount();
nItemCount = pList->GetItemCount();
int i;
LVCOLUMN columnData;
CString columnName;
int columnNum = 0;
CString strH;
CString strV;
 
columnData.mask = LVCF_TEXT;
columnData.cchTextMax =100;
columnData.pszText = columnName.GetBuffer (100);
//写列表头
for(i=0;pList->GetColumn(i,&columnData);i++)
{
   m_rangeExcel.SetItem( _variant_t( (long)(1) ), _variant_t( (long)(i+1) ),COleVariant(columnData.pszText) );
}
columnName.ReleaseBuffer ();
 
// 向Excel中写数据
for (i = 0; i < nItemCount; i++ )            
{
   for ( int j = 0; j < nColumnCount; j++ )
   {
    m_rangeExcel.SetItem( _variant_t( (long)(i+2) ),
     _variant_t( (long)(j+1) ),
     COleVariant(pList->GetItemText(i,j)) );
   }
}
 
//此方法对于不同的Excel版本参数个数可能不能,具体参看MSDN
m_wbExcelSingle.SaveAs( COleVariant( sExcelFile ),
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   0,
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   _variant_t(vtMissing),
   _variant_t(vtMissing));
 
}
else
{
//没有数据
MessageBox("没有数据,不能导出!","提示",MB_OK|MB_ICONWARNING|MB_TOPMOST);
 
}
 
 
m_wbExcelSingle.Close( covOptional, COleVariant( sExcelFile ), covOptional );
m_wbExcels.Close();
m_rangeExcel.ReleaseDispatch();
m_wsExcelSingle.ReleaseDispatch();
m_wsExcels.ReleaseDispatch();
m_wbExcelSingle.ReleaseDispatch();
m_wbExcels.ReleaseDispatch();
m_appExcel.ReleaseDispatch();
m_appExcel.Quit();
 
}
 
 
再提供一个类
 
 
 
#pragma once
 
 
#include "StdAfx.h"
#include <iostream>
 
 
using namespace std ;
 
 
 
 
#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\mso.dll" rename("RGB", "MSRGB")
 
 
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" raw_inte易做图ces_only, \
rename("Reference", "ignorethis"), rename("VBE", "JOEVBE")
 
 
#import "C:\Program Files\Microsoft Office\OFFICE11\excel.exe" exclude("IFont", "IPicture") \
rename("RGB", "ignorethis"), rename("DialogBox", "ignorethis"), rename("VBE", "JOEVBE"), \
rename("ReplaceText", "JOEReplaceText"), rename("CopyFile","JOECopyFile"), \
rename("FindText", "JOEFindText"), rename("NoPrompt", "JOENoPrompt")
 
 
using namespace Office;
using namespace VBIDE;
using namespace Excel ;
 
 
 
 
#define NULLSTR TEXT("")
 
 
class CEclOp
{
public:
CEclOp(void);
~CEclOp(void);
public:
 
 
BOOL InsertAChart(short nSheet);
BOOL DeleteASheetByNO(short nSheet);
BOOL DeleteASheetByName(CString strSheet);
CString GetCellText(short nSheet, int Row, int Col);
void DeleteCells(short nSheet, CString strRange);
void DeleteCells(short nSheet, unsigned int nFRow, unsigned int nFCol, unsigned int nTRow, unsigned int nTCol);
BOOL Initialization(CString strFilePath = NULLSTR);
void SetCellText(short nsheet,int row, int col, _variant_t content);
void SetCellText(short nSheet,CString strCell, _variant_t Content);
void InsertPicsFromFile(LPCSTR path, short nsheet, double left, double top);
void InsertSheetsAfter(short nsheet, int num);
void SetColumnWidth(int nColF, int nColT, double dWidth);
void SaveQuit(CString strPath);
void ReleaseAllInte易做图ces();
void SetCellColor(short nSheet, CString Range, COLORREF rgb);
void SetCellColor(short nSheet, int row, int col, COLORREF rgb);
 
 
private:
int m_nTotalSheets;
int m_nCurSheet;
public:
_ApplicationPtr pApplication;
_WorkbookPtr pThisWorkbook;
_WorksheetPtr pThisWorksheet;
RangePtr pThisRange;
_variant_t vt ;
Excel::XlFileFormat vFileFormat ;
Excel::XlSaveAsAccessMode vSaveAsAccessMode ;
Excel::XlSaveConflictResolution vSaveConflictResolution ;
public:
SheetsPtr pThisSheets;
 
 
};
 
//cpp
 
/*EclOp.cpp*/
 
 
#include "stdafx.h"
#include "ExcelOprator.h"
 
 
 
 
CEclOp::CEclOp(void)
{
 
 
}
 
 
CEclOp::~CEclOp(void)
{
 
 
pThisRange.Release();
pThisRange.Release();                                       
pThisWorksheet.Release();                                      
pThisSheets.Release();               &nbs
补充:软件开发 , Vc ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,