需要将生成的表格存为excel文件
因为要对报表进行格式上的设定,比如设置某行的颜色等
所以不能用odbc
怎么样使用automation或是别的方法操作?
具体怎么做的呢?如果有例程感激不尽
分不够再加,给思路也一定给分
请各位大虾帮帮忙!
因为要对报表进行格式上的设定,比如设置某行的颜色等
所以不能用odbc
怎么样使用automation或是别的方法操作?
具体怎么做的呢?如果有例程感激不尽
分不够再加,给思路也一定给分
请各位大虾帮帮忙!
//change font color of a Cell in excel, change cell background color, set border
/////////////////////////////////////////////////////////////////////
void CCellcolorDlg::OnOK()
{//HOWTO: Create Automation Project Using MFC and a Type Library Q178749
//change font color of a Cell in excel, change cell background color, set border
//Do not forget to call AfxOleInit();
try
{
_Application app; // app is an _Application object.
_Workbook book; // More object declarations.
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range; // Used for Microsoft Excel 97 components.
LPDISPATCH lpDisp; // Often reused variable.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
app.SetVisible(TRUE);
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
// to the books object.
lpDisp = books.Open("C:\\temp\\book1.xls", // Test.xls is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional ); // Return Workbook's IDispatch
// pointer.
book.AttachDispatch( lpDisp );
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
// Get sheet #1 and attach the IDispatch pointer to your sheet
// object.
lpDisp = sheets.GetItem( COleVariant((short)(1)) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("b3"));
range.AttachDispatch(lpDisp);
// set number format
// range.SetNumberFormat(COleVariant("@"));
// range.SetItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666")));
// set cell font here
Font newfont;
lpDisp=range.GetFont();
newfont.AttachDispatch(lpDisp);
//newfont.SetName(COleVariant("宋体"));
//newfont.SetSize(COleVariant((long)24));
newfont.SetColorIndex(COleVariant((short)3));//use VBA to see index value of a certain color
newfont.ReleaseDispatch();//set background color
lpDisp=range.GetInterior();
Interior cellinterior;
cellinterior.AttachDispatch(lpDisp);
cellinterior.SetColorIndex(COleVariant((short)6));//use VBA to see index value of a certain color
cellinterior.ReleaseDispatch();
// Set border
//xlDiagonalDown =5 xlDiagonalUp=6 xlEdgeLeft=7
//xlNone = -4142 xlContinuous=1 xlThin=2 xlAutomatic=-4105
//xlEdgeTop =8 xlEdgeBottom =9 xlEdgeRight=10
lpDisp = range.GetBorders();
Borders bds;
bds.AttachDispatch(lpDisp);
Border bd;
lpDisp = bds.GetItem(8);
bd.AttachDispatch(lpDisp);
bd.SetLineStyle(COleVariant((short)1));// // Release dispatch pointers.
range.ReleaseDispatch();
sheet.ReleaseDispatch();
// This is not really necessary because
// the default second parameter of AttachDispatch releases
// when the current scope is lost. } // End of processing. catch(COleException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
}
catch(COleDispatchException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf,
"COleDispatchException. SCODE: %08lx, Description: \"%s\".",
(long)e->m_wCode,(LPSTR)e->m_strDescription.GetBuffer(512));
::MessageBox(NULL, buf, "COleDispatchException",
MB_SETFOREGROUND | MB_OK);
}
catch(...)
{
::MessageBox(NULL, "General Exception caught.", "Catch-All",
MB_SETFOREGROUND | MB_OK);
}
CDialog::OnOK();
}
void CCsdn3Dlg::OnOK()
{
try
{
_Application app; // app is an _Application object.
_Workbook book; // More object declarations.
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range; // Used for Microsoft Excel 97 components.
LPDISPATCH lpDisp; // Often reused variable.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
app.SetVisible(TRUE);
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
// to the books object.
lpDisp = books.Open("C:\\temp\\book1.xls", // Test.xls is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional ); // Return Workbook's IDispatch
// pointer.
book.AttachDispatch( lpDisp );
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
// Get sheet #1 and attach the IDispatch pointer to your sheet
// object.
lpDisp = sheets.GetItem( COleVariant((short)(1)) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp); lpDisp = sheet.GetRange(COleVariant("B2"), COleVariant("b6"));
range.AttachDispatch(lpDisp);
range.SetNumberFormat(COleVariant("@"));
range.SetItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666")));
// Release dispatch pointers.
range.ReleaseDispatch();
sheet.ReleaseDispatch();
// This is not really necessary because
// the default second parameter of AttachDispatch releases
// when the current scope is lost. } // End of processing. catch(COleException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
} catch(COleDispatchException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf,
"COleDispatchException. SCODE: %08lx, Description: \"%s\".",
(long)e->m_wCode,(LPSTR)e->m_strDescription.GetBuffer(512));
::MessageBox(NULL, buf, "COleDispatchException",
MB_SETFOREGROUND | MB_OK);
} catch(...)
{
::MessageBox(NULL, "General Exception caught.", "Catch-All",
MB_SETFOREGROUND | MB_OK);
}
CDialog::OnOK();
}
///////////////////////////////////////////////////////////////////////////////////////
//change cell font of Excel
///////////////////////////////////////////////////////////////////////////////////////
void CC2Dlg::OnOK()
{//HOWTO: Create Automation Project Using MFC and a Type Library Q178749
//change font of a Cell in excel
try
{
_Application app; // app is an _Application object.
_Workbook book; // More object declarations.
_Worksheet sheet;
Workbooks books;
Worksheets sheets;
Range range; // Used for Microsoft Excel 97 components.
LPDISPATCH lpDisp; // Often reused variable.
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
app.SetVisible(TRUE);
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
// to the books object.
lpDisp = books.Open("C:\\temp\\book1.xls", // Test.xls is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional ); // Return Workbook's IDispatch
// pointer.
book.AttachDispatch( lpDisp );
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
// Get sheet #1 and attach the IDispatch pointer to your sheet
// object.
lpDisp = sheets.GetItem( COleVariant((short)(1)) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetRange(COleVariant("B3"), COleVariant("b3"));
range.AttachDispatch(lpDisp);
range.SetNumberFormat(COleVariant("@"));
range.SetItem(COleVariant((long)(1)),COleVariant((long)(1)),COleVariant(LPCTSTR("000666")));
Font newfont;
lpDisp=range.GetFont();
newfont.AttachDispatch(lpDisp);
newfont.SetName(COleVariant("宋体"));
newfont.SetSize(COleVariant((long)24));
newfont.ReleaseDispatch();
// Release dispatch pointers.
range.ReleaseDispatch();
sheet.ReleaseDispatch();
// This is not really necessary because
// the default second parameter of AttachDispatch releases
// when the current scope is lost. } // End of processing. catch(COleException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf, "COleException. SCODE: %08lx.", (long)e->m_sc);
::MessageBox(NULL, buf, "COleException", MB_SETFOREGROUND | MB_OK);
}
catch(COleDispatchException *e)
{
char buf[1024]; // For the Try...Catch error message.
sprintf(buf,
"COleDispatchException. SCODE: %08lx, Description: \"%s\".",
(long)e->m_wCode,(LPSTR)e->m_strDescription.GetBuffer(512));
::MessageBox(NULL, buf, "COleDispatchException",
MB_SETFOREGROUND | MB_OK);
}
catch(...)
{
::MessageBox(NULL, "General Exception caught.", "Catch-All",
MB_SETFOREGROUND | MB_OK);
}
CDialog::OnOK();
}
谢谢
:)
[email protected]
没有收到啊。
YP2002CN(老婆我不敢了,老婆我愛你)
可不可以说详细一点?
我倒..
那末您知道如何通过ole读写xls文件的方法了?
说一说呀
http://support.microsoft.com/default.aspx?scid=kb;en-us;q178781还有csdn别人提的问题,估计你也会再碰到:在调用excel后我该怎样退出excel程序,好象每次执行后excel还在后台,没有退出来。帮帮我吧
回复人: bljbljbljblj(无尽空虚) ( ) 信誉:107 2002-03-29 07:57:00 得分:0
你是怎么调用EXCEL的?是CREATEPROCESS还是用 _com_ptr_t处理的?
回复人: madaw(madaw) ( ) 信誉:100 2002-04-03 16:07:00 得分:0
调用
WorkBooks.Close();
_Application m_ExcelApp;
m_ExcelApp.ReleaseDispatch();
m_ExcelApp.Quit();
俺指的是谁有xls文件的格式说明???
这样是不是一定要打开excel?
binbin(滨滨,不是二进制):
文件格式的话 这里:
Microsoft Excel File Format (versions 2, 3, 4, 5, 95, 97, 2000, XP)
http://www.wotsit.org/filestore/excel.zip
是不是您要的?
照你的代码关闭文件时候
它会弹出对话框文件已更改,是否要保存文件
如何让它直接保存关闭?
用一下语句还是不行:
sheet.SaveAs(m_strFilePath,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional); 多谢帮忙了!
covOptional ,covOptional ,
covOptional ,covOptional
,covOptional ,0,
covOptional, covOptional ,covOptional ,
covOptional );
看清楚其中一个参数=0;
如果把第七个参数改成0的话,
sheet是这样定义的: _Worksheet sheet;sheet.SaveAs(m_strFilePath, // Test.xls is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,0, covOptional, covOptional);编译不通过
错误信息
error C2664: 'SaveAs' : cannot convert parameter 7 from 'const int' to 'const struct tagVARIANT &'