• 大小: 106KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C/C++
  • 标签: MFC  Excel  

资源简介

Visual Studio 2013开发MFC程序,逐行读取TXT文件的内容,重新写入Excel文件。

资源截图

代码片段和文件信息

#include “stdafx.h“
#include “Excel2010Helper.h“


CExcel2010Helper::CExcel2010Helper()
{
// 初始化COM库
CoInitialize(NULL);
}


CExcel2010Helper::~CExcel2010Helper()
{
// 关闭COM库
CoUninitialize();
}

BOOL CExcel2010Helper::CreateExcelApplication()
{
//创建Excel服务
if (!m_oExcelApp.CreateDispatch(TEXT(“Excel.Application“) NULL))
{
return FALSE;
}

COleVariant covOptional(DISP_E_PARAMNOTFOUND VT_ERROR);

// 打开工作薄
m_oWorkBooks.AttachDispatch(m_oExcelApp.get_Workbooks() TRUE);
m_oWorkBook.AttachDispatch(m_oWorkBooks.Add(covOptional) TRUE);

// 打开第一个工作表
covOptional = (short)1;
m_oWorkSheets.AttachDispatch(m_oWorkBook.get_Worksheets() TRUE);
m_oWorkSheet.AttachDispatch(m_oWorkSheets.get_Item(covOptional) TRUE);

return TRUE;
}

void CExcel2010Helper::DestroyExcelApplication()
{
// 释放对象
m_oCurrRange.ReleaseDispatch();
m_oWorkSheet.ReleaseDispatch();
m_oWorkSheets.ReleaseDispatch();
m_oWorkBook.ReleaseDispatch();
m_oWorkBooks.ReleaseDispatch();

// Quit必须在m_ExlApp释放之前,否则程序结束后还会有一个Excel进程驻留在内存中,而且程序重复运行的时候会出错
m_oExcelApp.Quit();
m_oExcelApp.ReleaseDispatch();
}


void CExcel2010Helper::SetCellText(_tstring cellPos _tstring cellText)
{
//获取单元格区域
m_oCurrRange.AttachDispatch(m_oWorkSheet.get_Range(COleVariant(cellPos.c_str())
COleVariant(cellPos.c_str())));

m_oCurrRange.put_Value2(COleVariant(cellText.c_str()));
}


void CExcel2010Helper::SetCellBackgroundColor(_tstring cellStart _tstring cellEnd long color)
{
Cnterior oInterior;

//获取单元格区域
m_oCurrRange.AttachDispatch(m_oWorkSheet.get_Range(COleVariant(cellStart.c_str())
COleVariant(cellEnd.c_str())));

//设置单元格背景色
oInterior.AttachDispatch(m_oCurrRange.get_Interior() TRUE);
oInterior.put_Color(COleVariant(color));

oInterior.ReleaseDispatch();
}

void CExcel2010Helper::SetCellFontFormat(_tstring cellStart _tstring cellEnd
 long size bool bold long color)
{
CFont0 oFont;

//获取单元格区域
m_oCurrRange.AttachDispatch(m_oWorkSheet.get_Range(COleVariant(cellStart.c_str())
COleVariant(cellEnd.c_str())));

oFont.AttachDispatch(m_oCurrRange.get_Font());
// 设置字体是否粗体
oFont.put_Bold(COleVariant((short)bold));
// 设置字体大小
oFont.put_Size(COleVariant(size));
// 设置字体颜色
oFont.put_Color(COleVariant(color));

oFont.ReleaseDispatch();
}

void CExcel2010Helper::SetCellBorderAround(_tstring cellStart _tstring cellEnd XlLinestyle linestyle
 XlBorderWeight borderWeight long color)
{
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND VT_ERROR);

//获取单元格区域
m_oCurrRange.AttachDispatch(m_oWorkSheet.get_Range(COleVariant(cellStart.c_str())
COleVariant(cellEnd.c_str())));

//设置外边框
m_oCurrRange.BorderAround(COleVariant((long)linestyle) borderWeight xlColorIndexAutomatic
 COleVariant(color) covOptional);
}

void CExcel2010Helper::SetColumnAutoFit()
{
// 获得使用的区域Range
m_oCurrRange.AttachDispatch(m_oWorkSheet.get_UsedRange() TRUE);

m_oCurrRange

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-10-17 17:51  LogTxtToExcel\
     目录           0  2015-10-17 17:51  LogTxtToExcel\Debug\
     文件         985  2015-10-14 09:16  LogTxtToExcel\Debug\AlarmTransmit_receive_20150930.log
     文件         985  2015-10-14 09:16  LogTxtToExcel\Debug\AlarmTransmit_receive_20150931.log
     文件         165  2015-10-16 18:27  LogTxtToExcel\Debug\~$AlarmTransmit_receive_20150930.xlsx
     目录           0  2015-10-17 17:51  LogTxtToExcel\LogTxtToExcel\
     文件       79655  2015-10-15 17:09  LogTxtToExcel\LogTxtToExcel\CApplication.h
     文件        3806  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\CBorders.h
     文件        6208  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\CFont0.h
     文件       45487  2015-10-15 17:05  LogTxtToExcel\LogTxtToExcel\CRange.h
     文件       45364  2015-10-15 17:09  LogTxtToExcel\LogTxtToExcel\CWorkbook.h
     文件        7346  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\CWorkbooks.h
     文件       29258  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\CWorksheet.h
     文件        4907  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\CWorksheets.h
     文件        4295  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\Cnterior.h
     文件        4234  2015-10-17 16:48  LogTxtToExcel\LogTxtToExcel\Excel2010Helper.cpp
     文件        3423  2015-10-17 17:50  LogTxtToExcel\LogTxtToExcel\Excel2010Helper.h
     文件        2465  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\LogTxtToExcel.cpp
     文件         485  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\LogTxtToExcel.h
     文件       11058  2015-10-16 17:22  LogTxtToExcel\LogTxtToExcel\LogTxtToExcel.rc
     文件        6657  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\LogTxtToExcel.vcxproj
     文件        3111  2015-10-15 16:55  LogTxtToExcel\LogTxtToExcel\LogTxtToExcel.vcxproj.filters
     文件        9178  2015-10-17 16:36  LogTxtToExcel\LogTxtToExcel\LogTxtToExcelDlg.cpp
     文件        1463  2015-10-16 17:18  LogTxtToExcel\LogTxtToExcel\LogTxtToExcelDlg.h
     文件        3975  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\ReadMe.txt
     目录           0  2015-10-16 23:17  LogTxtToExcel\LogTxtToExcel\res\
     文件       67777  2013-07-22 01:18  LogTxtToExcel\LogTxtToExcel\res\LogTxtToExcel.ico
     文件         682  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\res\LogTxtToExcel.rc2
     文件        1612  2015-10-15 13:51  LogTxtToExcel\LogTxtToExcel\resource.h
     文件         146  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\stdafx.cpp
     文件        1578  2015-10-15 13:47  LogTxtToExcel\LogTxtToExcel\stdafx.h
............此处省略3个文件信息

评论

共有 条评论