有关利用ADO读取Excel单元格数据的问题

baidu_41665709 2018-03-11 11:06:18
所用Excel(.xsl)只有一个表单(sheet)名为“红米note2充电”,该表单中无表头(即第一行就是数据),分为两列(即只有A、B列)。如下图示:

现已可成功连接Excel,但具体使用代码一个个读取单元格中的数据尚有问题。使用如下网上查的代码读取:

void ADOsql::GetFloat()
{
_variant_t var1 = m_pRecordSet->GetFields()->GetItem((long)0)->Value;
CString strName1;
if (var1.vt != VT_NULL)
strName1 = (LPCSTR)_bstr_t(var1);
AfxMessageBox(strName1);

}

读取出了第一列第二行单元格的数据(A2),如图

将(long)0改为(long)1后读出了第二列的数据(Bn),如图

求大神解答
1.为何不是A1的数据;
2.为何B列的数据显示得有问题(应该是-0.01);
3.函数GetFields()和GetItem()的具体作用是什么;
5.如何使用函数movenext来使其在同一列中一个个往下读取;
6.如何使读取出来的结果最终转化为float类型。
...全文
1270 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_41665709 2018-03-18
  • 打赏
  • 举报
回复
谢谢各位大神的认真回答,这些问题已经自己解决了,只是同一用户不能连续回复3次以上所以没能及时更新我的问题进度。
zgl7903 2018-03-17
  • 打赏
  • 举报
回复
直接使用EXCEL

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

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

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

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!";
       
    //插入超链接
    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/392326155", 
        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;
}


baidu_41665709 2018-03-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
百度搜相关关键字。
你好,又有一个新问题。我想在一个OnTimer里使用如下代码读取数据(一秒自动换行读一个)

double yValue =0.0;
yValue=m_pRecordSet->GetFields()->GetItem((long)0)->Value;
m_pRecordSet->MoveNext();
实际使用时每次都是读出第一行的数据,看来电脑没有接着上次的读取进行MoveNext,而是每次都是重新从第一行读取然后MoveNext,请问如何解决?
baidu_41665709 2018-03-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
百度搜相关关键字。
哦,明白了,应该是因为在转换成CString的时候没有用format()说明数据源类型是什么。
baidu_41665709 2018-03-15
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
百度搜相关关键字。
你好,我上百度又查了一下,还是没查到为什么在读取B列数据时会出现“-.01”这样的错误。
赵4老师 2018-03-11
  • 打赏
  • 举报
回复
百度搜相关关键字。

4,011

社区成员

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

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