_Application app;
if(!app.CreateDispatch("Excel.Application"))
{
MessageBox("Could't start Excel!");
return;
}
Workbooks books;
_Workbook book;
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
books=app.GetWorkbooks();
book= books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
Worksheets sheets;
_Worksheet sheet;
Range range;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(i+1)));
MessageBox(sheet.GetName);//取得所有表名
}
14 个解决方案
#1
请各位帮帮忙,小弟急S了!
#2
回答如何读取里面的内容:
Range rgMyRge;
rgMyRge.AttachDispatch(sheet.GetCells(),true);
_variant_t value=rgMyRge.GetItem(_variant_t((long)rowIndex),_variant_t((long)colIndex));
Range rgMyRge;
rgMyRge.AttachDispatch(sheet.GetCells(),true);
_variant_t value=rgMyRge.GetItem(_variant_t((long)rowIndex),_variant_t((long)colIndex));
#3
在回答取得行数和列数:
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
#4
谢谢楼上的朋友,可按照你的方法,怎么显示结果都是1??
代码如下:
。。。。。。
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(i+1)));
rgMyRge.AttachDispatch(sheet.GetCells(),TRUE);
str.Format("Row=%d,Col=%d",rgMyRge.GetRow(),rgMyRge.GetColumn());
MessageBox(sheet.GetName(),str);//取得所有表名
}
代码如下:
。。。。。。
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(i+1)));
rgMyRge.AttachDispatch(sheet.GetCells(),TRUE);
str.Format("Row=%d,Col=%d",rgMyRge.GetRow(),rgMyRge.GetColumn());
MessageBox(sheet.GetName(),str);//取得所有表名
}
#5
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
long cols=rgMyRge.GetCol();
#6
朋友,问题依旧呀!
#7
试试这个:
rgMyRge.AttachDispatch(sheet.GetUsedRange(),true); rgMyRge.AttachDispatch(rgMyRge.GetColumns(),true);
long cols=rgMyRge.GetCount(); //得到列数
rgMyRge.AttachDispatch(rgMyRge.GetRows(),true);
long rows=rgMyRge.GetCount();//得到行数
rgMyRge.AttachDispatch(sheet.GetUsedRange(),true); rgMyRge.AttachDispatch(rgMyRge.GetColumns(),true);
long cols=rgMyRge.GetCount(); //得到列数
rgMyRge.AttachDispatch(rgMyRge.GetRows(),true);
long rows=rgMyRge.GetCount();//得到行数
#8
我帮你看看
#9
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true)
//得到所有的行
rgMyRge.AttachDispatch(wsMysheet.GetRows(),true)
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true)
//得到所有的行
rgMyRge.AttachDispatch(wsMysheet.GetRows(),true)
#10
不知道你有没有释放系统占用的资源:
rgMyRge.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
Book.ReleaseDispatch();
Books.ReleaseDispatch();
app.ReleaseDispatch();
rgMyRge.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
Book.ReleaseDispatch();
Books.ReleaseDispatch();
app.ReleaseDispatch();
#11
该问题已经解决,但退出应用程序后,不知道为什么系统进程中还有一个EXCEL.exe进程,每运行一次系统中就多一个这样的进程?以下是我完整的程序段,不知道为什么呢???
以下是完整的程序片段:::
void CTestDlg::OnButton4()
{
_Application app;
if(!app.CreateDispatch("Excel.Application"))
{
MessageBox("Could't start Excel!");
return;
}else
{
MessageBox("Start!");
}
Workbooks books;
_Workbook book;
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
books=app.GetWorkbooks();
book= books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(1)));
rgMyRge.AttachDispatch(sheet.GetUsedRange(),TRUE);
rgMyRge.AttachDispatch(rgMyRge.GetRows(),TRUE);
str.Format("%d",rgMyRge.GetCount());
MessageBox(sheet.GetName(),str);//取得所有表名
}
//释放对象
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
}
以下是完整的程序片段:::
void CTestDlg::OnButton4()
{
_Application app;
if(!app.CreateDispatch("Excel.Application"))
{
MessageBox("Could't start Excel!");
return;
}else
{
MessageBox("Start!");
}
Workbooks books;
_Workbook book;
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
books=app.GetWorkbooks();
book= books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(1)));
rgMyRge.AttachDispatch(sheet.GetUsedRange(),TRUE);
rgMyRge.AttachDispatch(rgMyRge.GetRows(),TRUE);
str.Format("%d",rgMyRge.GetCount());
MessageBox(sheet.GetName(),str);//取得所有表名
}
//释放对象
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
}
#12
还有一个rgMyRge.ReleaseDispatch(); 但问题依旧呀!
#13
books.AttachDispatch(App.GetWorkbooks(),true);
book.AttachDispatch(books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
//得到Worksheets
sheets.AttachDispatch(Book.GetWorksheets(),true);
book.AttachDispatch(books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
//得到Worksheets
sheets.AttachDispatch(Book.GetWorksheets(),true);
#14
我除了只加了
books.Close();
books.ReleaseDispatch();
app.ReleaseDispatch();
在程序退出的时候,excel进程就会消失了
books.Close();
books.ReleaseDispatch();
app.ReleaseDispatch();
在程序退出的时候,excel进程就会消失了
#1
请各位帮帮忙,小弟急S了!
#2
回答如何读取里面的内容:
Range rgMyRge;
rgMyRge.AttachDispatch(sheet.GetCells(),true);
_variant_t value=rgMyRge.GetItem(_variant_t((long)rowIndex),_variant_t((long)colIndex));
Range rgMyRge;
rgMyRge.AttachDispatch(sheet.GetCells(),true);
_variant_t value=rgMyRge.GetItem(_variant_t((long)rowIndex),_variant_t((long)colIndex));
#3
在回答取得行数和列数:
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
#4
谢谢楼上的朋友,可按照你的方法,怎么显示结果都是1??
代码如下:
。。。。。。
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(i+1)));
rgMyRge.AttachDispatch(sheet.GetCells(),TRUE);
str.Format("Row=%d,Col=%d",rgMyRge.GetRow(),rgMyRge.GetColumn());
MessageBox(sheet.GetName(),str);//取得所有表名
}
代码如下:
。。。。。。
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(i+1)));
rgMyRge.AttachDispatch(sheet.GetCells(),TRUE);
str.Format("Row=%d,Col=%d",rgMyRge.GetRow(),rgMyRge.GetColumn());
MessageBox(sheet.GetName(),str);//取得所有表名
}
#5
long rows=rgMyRge.GetRow();
long cols=rgMyRge.GetCol();
long cols=rgMyRge.GetCol();
#6
朋友,问题依旧呀!
#7
试试这个:
rgMyRge.AttachDispatch(sheet.GetUsedRange(),true); rgMyRge.AttachDispatch(rgMyRge.GetColumns(),true);
long cols=rgMyRge.GetCount(); //得到列数
rgMyRge.AttachDispatch(rgMyRge.GetRows(),true);
long rows=rgMyRge.GetCount();//得到行数
rgMyRge.AttachDispatch(sheet.GetUsedRange(),true); rgMyRge.AttachDispatch(rgMyRge.GetColumns(),true);
long cols=rgMyRge.GetCount(); //得到列数
rgMyRge.AttachDispatch(rgMyRge.GetRows(),true);
long rows=rgMyRge.GetCount();//得到行数
#8
我帮你看看
#9
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true)
//得到所有的行
rgMyRge.AttachDispatch(wsMysheet.GetRows(),true)
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true)
//得到所有的行
rgMyRge.AttachDispatch(wsMysheet.GetRows(),true)
#10
不知道你有没有释放系统占用的资源:
rgMyRge.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
Book.ReleaseDispatch();
Books.ReleaseDispatch();
app.ReleaseDispatch();
rgMyRge.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
Book.ReleaseDispatch();
Books.ReleaseDispatch();
app.ReleaseDispatch();
#11
该问题已经解决,但退出应用程序后,不知道为什么系统进程中还有一个EXCEL.exe进程,每运行一次系统中就多一个这样的进程?以下是我完整的程序段,不知道为什么呢???
以下是完整的程序片段:::
void CTestDlg::OnButton4()
{
_Application app;
if(!app.CreateDispatch("Excel.Application"))
{
MessageBox("Could't start Excel!");
return;
}else
{
MessageBox("Start!");
}
Workbooks books;
_Workbook book;
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
books=app.GetWorkbooks();
book= books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(1)));
rgMyRge.AttachDispatch(sheet.GetUsedRange(),TRUE);
rgMyRge.AttachDispatch(rgMyRge.GetRows(),TRUE);
str.Format("%d",rgMyRge.GetCount());
MessageBox(sheet.GetName(),str);//取得所有表名
}
//释放对象
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
}
以下是完整的程序片段:::
void CTestDlg::OnButton4()
{
_Application app;
if(!app.CreateDispatch("Excel.Application"))
{
MessageBox("Could't start Excel!");
return;
}else
{
MessageBox("Start!");
}
Workbooks books;
_Workbook book;
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
books=app.GetWorkbooks();
book= books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
Worksheets sheets;
_Worksheet sheet;
Range rgMyRge;
CString str;
sheets=book.GetWorksheets();
for(int i=0;i<sheets.GetCount();i++)
{
sheet=sheets.GetItem(COleVariant(long(1)));
rgMyRge.AttachDispatch(sheet.GetUsedRange(),TRUE);
rgMyRge.AttachDispatch(rgMyRge.GetRows(),TRUE);
str.Format("%d",rgMyRge.GetCount());
MessageBox(sheet.GetName(),str);//取得所有表名
}
//释放对象
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
}
#12
还有一个rgMyRge.ReleaseDispatch(); 但问题依旧呀!
#13
books.AttachDispatch(App.GetWorkbooks(),true);
book.AttachDispatch(books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
//得到Worksheets
sheets.AttachDispatch(Book.GetWorksheets(),true);
book.AttachDispatch(books.Open("C:\\Book1.xls",
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional,
VOptional, VOptional, VOptional, VOptional);
//得到Worksheets
sheets.AttachDispatch(Book.GetWorksheets(),true);
#14
我除了只加了
books.Close();
books.ReleaseDispatch();
app.ReleaseDispatch();
在程序退出的时候,excel进程就会消失了
books.Close();
books.ReleaseDispatch();
app.ReleaseDispatch();
在程序退出的时候,excel进程就会消失了