用MFC怎么将Excel中的数据导入到列表控件中,再通过一系列操作,最后还能挑选想要的数据导出到新的EXCEL表中

小白学习学习 2018-05-10 11:26:59
需要将Excel表中的数据(由于表是不规则的,所以要从某一行开始才导入数据)存储到List Control中,然后挑选出自己想要的数据,再存储到新的Excel表格当中。以前只做过Access的,对Excel这一块不太熟悉,在网上也找了许多方法,但似乎没有自己想要的功能,求大神指教
...全文
979 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2018-05-10
  • 打赏
  • 举报
回复
直接使用COM

// Excel12.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>

#import "C:/Program Files/Common Files/Microsoft Shared/OFFICE14/mso.dll" \
  rename("RGB", "RBGMSO") rename("SearchPath", "SearchPathMSO") \
  rename("DocumentProperties", "DocumentPropertiesMSO") no_auto_exclude
#import "C:/Program Files/Common Files/Microsoft Shared/VBA/VBA6/VBE6EXT.OLB" no_namespace
#import "C:/Program Files/Microsoft Office/OFFICE14/excel.exe" \
  rename("IFont", "IFontXL") \
  rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB") \
  rename("CopyFile", "ExcelCopyFile") rename("ReplaceText", "ExcelReplaceText") \

void dump_com_error(_com_error &e)
{
  _tprintf(_T("Oops - hit an error!\n"));
  _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
  _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
  _bstr_t bstrSource(e.Source());
  _bstr_t bstrDescription(e.Description());
  _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
  _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
}
struct StartOle {
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;


int main(int argc, char* argv[])
{ 
  using namespace Office;
  using namespace Excel;
  
  _ApplicationPtr pXL;
  
  try 
  {
    pXL.CreateInstance(L"Excel.Application");
    
    pXL->PutVisible(0, VARIANT_TRUE);
    
    WorkbooksPtr pBooks = pXL->Workbooks;
    _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);
    
    _WorksheetPtr pSheet = pXL->ActiveSheet;
    
    //修改标名
    try
    {
      // This one will fail
      pSheet->Name = "Market Share?";
    }
    catch (_com_error &e) 
    {
      dump_com_error(e);
    }    
    pSheet->Name = "Market Share!";

    //读写数据
    {
      pSheet->Range["A2"]->Value2 = "Company A";
      _bstr_t bStr = pSheet->Range["A2"]->Value2;
      _tprintf(_T("\"A2\" = %s\n"), (LPCTSTR)bStr);
    }
       
    //插入超链接
    try
    {
      //Range("I4").Select
      //ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
      //  "http://bbs.csdn.net/topics/392326155", TextToDisplay:="测试超链接"
      
      RangePtr pRang = pSheet->Range["I4"];
      HyperlinksPtr pLinks = pSheet->Hyperlinks;
      pLinks->Add(pRang, 
        "http://bbs.csdn.net/topics/392375991", 
        vtMissing, 
        vtMissing,
        "测试超链接");
    }
    catch(_com_error &e) 
    {
      dump_com_error(e);
    }
    
    //插入图片
    try
    {
      Excel::ShapesPtr pShapes = pSheet->Shapes;
      RangePtr range = pSheet->Range["H1:O10"];
      float l = range->Left, t = range->Top;
      float w = range->Width, h = range->Height;
      Excel::ShapePtr pShape = pShapes->AddPicture( _bstr_t("C:\\tt.jpg"), 
        msoFalse, msoTrue, l, t, w, h);
    }    
    catch(_com_error &e) 
    {
      dump_com_error(e);
    }

    //插入饼状图
    try
    {
      pSheet->Range["A2"]->Value2 = "Company A";
      pSheet->Range["B2"]->Value2 = "Company B";
      pSheet->Range["C2"]->Value2 = "Company C";
      pSheet->Range["D2"]->Value2 = "Company D";    
      pSheet->Range["A3"]->Value2 = 75.0;
      pSheet->Range["B3"]->Value2 = 14.0;
      pSheet->Range["C3"]->Value2 = 7.0;
      pSheet->Range["D3"]->Value2 = 4.0;
      pSheet->Range["A4"]->Value2 = 75.0;
      pSheet->Range["B4"]->Value2 = 14.0;
      pSheet->Range["C4"]->Value2 = 7.0;
      pSheet->Range["D4"]->Value2 = 4.0;
      
      RangePtr  pRange  = pSheet->Range["A2:D3"];
      _ChartPtr  pChart  = pBook->Charts->Add();
      
      pChart->ChartWizard((Range*) pRange, (long) Office::xl3DPie, 7L, (long) Office::xlRows,
        1L, 0L, 2L, "Market Share");
    } 
    catch( _com_error &e)
    {
      dump_com_error(e);
    }

    //设置页面格式
    try
    {
      PageSetupPtr pPageSet = pSheet->PageSetup;
      pPageSet->PutPaperSize( xlPaperA4 ); //A4
      pPageSet->CenterHorizontally = VARIANT_TRUE;  //水平居中
      pPageSet->PutOrientation( xlLandscape ); //横向打印
      pPageSet->PrintArea = "$A$1:$J$22"; //打印区域
    }
    catch(_com_error &e) 
    {
      dump_com_error(e);
    }

    Sleep(6000);

    
    pBook->PutSaved(0, VARIANT_TRUE);
    pXL->Quit();
  } 
  catch(_com_error &e) 
  {
    dump_com_error(e);
  }

  return 0;
}


4,011

社区成员

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

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