long iSheetCount = sheets.get_Count();
range = range.get_Rows();
long iRow = range.get_Row();
//long iRow = range.get_Count();
range = range.get_Columns();
//long iColumn = range.get_Column();
long iColumn = range.get_Count();
获取EXCEL的表格当前行和列的值有问题
获取行
range = range.get_Rows();
long iRow = range.get_Row();
long iRow = range.get_Count();
在表格无数据,两个获取都是 1 不会获取到0, 有多少行数据,获取的都是 1
获取列
long iColumn = range.get_Column(); 一直获取的都是 1
long iColumn = range.get_Count(); 这个获取的是正常的
想问一下大佬,到底是什么问题,为什么一直获取不到行
5 个解决方案
#1
https://download.csdn.net/download/yeah2000/3576494
参考
参考
#2
// Excel14.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;
}
#3
这是什么啊?
#4
仅凭这些无法判断,贴全代码及excel数据
#5
HRESULT hr;
hr = CoInitialize(NULL);
if( FAILED(hr) )
{
AfxMessageBox(_T("Failed to call Coinitialize()"));
}
CWorkbooks books;
CWorkbook book;
CApplication app;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covOptional((long) DISP_E_PARAMNOTFOUND, VT_ERROR);
CString strPath = m_strAppPath + _T("testExecl.xls");
DWORD dwAttrib = GetFileAttributes(strPath);
BOOL bExists = (INVALID_FILE_ATTRIBUTES != dwAttrib) && (0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
//TRACE("bExists -> %d", bExists);
if (!app.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("无法启动服务器"));
return false;
}
//得到Workbook
books.AttachDispatch(app.get_Workbooks());
if( bExists )
{
lpDisp = books.Open(strPath, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
book.AttachDispatch(lpDisp);
}
else
{
book.AttachDispatch(books.Add(covOptional), true);
}
sheets.AttachDispatch(book.get_Worksheets(), true);
sheet.AttachDispatch(sheets.get_Item(_variant_t("sheet1")), true); // 获取EXCEL, sheet1页面
//range = sheet.get_UsedRange();
range.AttachDispatch(sheet.get_UsedRange());
CRange usedRange;
usedRange.AttachDispatch(range.get_Rows(), true);
long iRow = usedRange.get_Count();
usedRange.AttachDispatch(range.get_Columns(), true);
long iColumn = usedRange.get_Count();
usedRange.ReleaseDispatch();
long iSheetCount = sheets.get_Count();
//range = range.get_Rows();
//long iRow = range.get_Row();
//long iRow = range.get_Count();
//range = range.get_Columns();
//long iColumn = range.get_Column();
//long iColumn = range.get_Count();
TRACE("excel Sheet -> %d \n row -> %d \n column -> %d", iSheetCount, iRow, iColumn);
CString strCell_1, strCell_2, strCell_3, strCell_4;
strCell_1.Format(_T("A%d"), (iRow + 1));
strCell_2.Format(_T("B%d"), (iRow + 1));
strCell_3.Format(_T("C%d"), (iRow + 1));
strCell_4.Format(_T("D%d"), (iRow + 1));
OutputDebugString(_T("\n") + strCell_1 + strCell_2 + strCell_3 + strCell_4);
range = sheet.get_Range(COleVariant(strCell_1), COleVariant(strCell_1)); // 获取单元格
range.put_Value2(COleVariant(cs1)); // 往单元格写入数据
range = sheet.get_Range(COleVariant(strCell_2), COleVariant(strCell_2));
range.put_Value2(COleVariant(cs2));
range = sheet.get_Range(COleVariant(strCell_3), COleVariant(strCell_3));
range.put_Value2(COleVariant(cs3));
range = sheet.get_Range(COleVariant(strCell_4), COleVariant(strCell_4));
range.put_Value2(COleVariant(cs4));
//app.put_Visible(TRUE); // 写入数据时候打开EXCEL文件, 默认为FALSE
//book.SaveCopyAs(COleVariant(strPath));
//当文件不存在用SAVEAS ,文件存在用SAVE, 否则会弹出对话框
if( bExists )
{
book.Save();
}
else
{
book.SaveAs(COleVariant(strPath), vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
0, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
}
book.put_Saved(TRUE);
books.Close(); // 关于屏蔽掉最后“确定改变文件”的对话框,用Book的close方法
app.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
#1
https://download.csdn.net/download/yeah2000/3576494
参考
参考
#2
// Excel14.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;
}
#3
这是什么啊?
#4
仅凭这些无法判断,贴全代码及excel数据
#5
HRESULT hr;
hr = CoInitialize(NULL);
if( FAILED(hr) )
{
AfxMessageBox(_T("Failed to call Coinitialize()"));
}
CWorkbooks books;
CWorkbook book;
CApplication app;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covOptional((long) DISP_E_PARAMNOTFOUND, VT_ERROR);
CString strPath = m_strAppPath + _T("testExecl.xls");
DWORD dwAttrib = GetFileAttributes(strPath);
BOOL bExists = (INVALID_FILE_ATTRIBUTES != dwAttrib) && (0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
//TRACE("bExists -> %d", bExists);
if (!app.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("无法启动服务器"));
return false;
}
//得到Workbook
books.AttachDispatch(app.get_Workbooks());
if( bExists )
{
lpDisp = books.Open(strPath, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
book.AttachDispatch(lpDisp);
}
else
{
book.AttachDispatch(books.Add(covOptional), true);
}
sheets.AttachDispatch(book.get_Worksheets(), true);
sheet.AttachDispatch(sheets.get_Item(_variant_t("sheet1")), true); // 获取EXCEL, sheet1页面
//range = sheet.get_UsedRange();
range.AttachDispatch(sheet.get_UsedRange());
CRange usedRange;
usedRange.AttachDispatch(range.get_Rows(), true);
long iRow = usedRange.get_Count();
usedRange.AttachDispatch(range.get_Columns(), true);
long iColumn = usedRange.get_Count();
usedRange.ReleaseDispatch();
long iSheetCount = sheets.get_Count();
//range = range.get_Rows();
//long iRow = range.get_Row();
//long iRow = range.get_Count();
//range = range.get_Columns();
//long iColumn = range.get_Column();
//long iColumn = range.get_Count();
TRACE("excel Sheet -> %d \n row -> %d \n column -> %d", iSheetCount, iRow, iColumn);
CString strCell_1, strCell_2, strCell_3, strCell_4;
strCell_1.Format(_T("A%d"), (iRow + 1));
strCell_2.Format(_T("B%d"), (iRow + 1));
strCell_3.Format(_T("C%d"), (iRow + 1));
strCell_4.Format(_T("D%d"), (iRow + 1));
OutputDebugString(_T("\n") + strCell_1 + strCell_2 + strCell_3 + strCell_4);
range = sheet.get_Range(COleVariant(strCell_1), COleVariant(strCell_1)); // 获取单元格
range.put_Value2(COleVariant(cs1)); // 往单元格写入数据
range = sheet.get_Range(COleVariant(strCell_2), COleVariant(strCell_2));
range.put_Value2(COleVariant(cs2));
range = sheet.get_Range(COleVariant(strCell_3), COleVariant(strCell_3));
range.put_Value2(COleVariant(cs3));
range = sheet.get_Range(COleVariant(strCell_4), COleVariant(strCell_4));
range.put_Value2(COleVariant(cs4));
//app.put_Visible(TRUE); // 写入数据时候打开EXCEL文件, 默认为FALSE
//book.SaveCopyAs(COleVariant(strPath));
//当文件不存在用SAVEAS ,文件存在用SAVE, 否则会弹出对话框
if( bExists )
{
book.Save();
}
else
{
book.SaveAs(COleVariant(strPath), vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
0, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
}
book.put_Saved(TRUE);
books.Close(); // 关于屏蔽掉最后“确定改变文件”的对话框,用Book的close方法
app.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();