使用VB或VC操作Excel 2003

1.VB操作EXCEL2003

[vb]  view plain copy
  1. ‘************************************************************************  
  2. ‘  
  3. ‘ 演示用VB.NET 设置 Excel 单元格值和风格的小程序(例程)  
  4. ‘ 程序功能是:打开文件,设置单元格的风格和值,冻结窗口等  
  5. ‘ 网上类似的程序很多,但Excel 2003的SaveAs函数由新版本的函数代替,原函数  
  6. ‘ 为_saveas,这个程序同时也演示了如何使用新函数,另外也演示了如何冻结窗格  
  7. ‘  
  8. ‘ 关键字:Excel 2003 SaveAs 冻结窗格 单元格 风格  
  9. ‘  
  10. ‘  
  11. ‘ 开发环境:Visual Studio .NET 2003  
  12. ‘ Excel环境:Excel 2003  
  13. ‘ 作者:高宏伟(DukeJoe)  
  14. ‘ 时间:2006-03-12 11:27  
  15. ‘ 地点:黑龙江省哈尔滨市平房区  
  16. ‘ 注释:无  
  17. ‘  
  18. ‘***********************************************************************  
  19. Module Module1  
  20.   
  21. Sub Main()  
  22.     Dim ThisApplication As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application  
  23.     Dim ThisWorkbook As Microsoft.Office.Interop.Excel.Workbook  
  24.     Dim ThisSheets As Microsoft.Office.Interop.Excel.Sheets  
  25.     Dim ThisWorkSheet As Microsoft.Office.Interop.Excel.Worksheet  
  26.     Dim ThisRange As Microsoft.Office.Interop.Excel.Range  
  27.   
  28.     ThisApplication.Visible = True  
  29.     ThisWorkbook = ThisApplication.Workbooks.Add()  
  30.   
  31.     Console.WriteLine(ThisWorkbook.Name)  
  32.     ThisSheets = ThisWorkbook.Worksheets  
  33.     ThisWorkSheet = ThisSheets(1)  
  34.     Console.WriteLine(ThisWorkSheet.Name)  
  35.     ’ 设置整个sheet的填充色为白色  
  36.     ThisWorkSheet.Cells.Interior.Color = RGB(255, 255, 255)  
  37.     ThisWorkSheet.Cells.ClearContents()  
  38.     ThisRange = ThisWorkSheet.Range("A1:C5")  
  39.     Console.WriteLine(ThisRange.Cells(1, 1).Value)  
  40.     ’ThisRange.Interior.Color = 0  
  41.     ThisRange.ClearFormats()  
  42.     ThisRange.Cells(1, 1).Value = "哈尔滨市平房区"  
  43.     ThisRange.Cells(1, 2).Value = "高宏伟"  
  44.     ThisRange.Cells(1, 3).Value = "QQ:21807822"  
  45.     ThisRange.Cells(2, 1).Value = "1"  
  46.     ThisRange.Cells(3, 1).Value = "2"  
  47.     ThisRange.Cells(4, 1).Value = "3"  
  48.     ThisRange.Cells(5, 1).Value = "4"  
  49.     ’ 为Range的四周和内部加上边框  
  50.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  51.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  52.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  53.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  54.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  55.     ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous  
  56.     ’ 设置第一行的格式(背景色、粗体、颜色、列宽)  
  57.     ThisRange.Range("A1:C1").Interior.ColorIndex = 47  
  58.     ThisRange.Range("A1:C1").Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid  
  59.     ThisRange.Range("A1:C1").Font.ColorIndex = 6  
  60.     ThisRange.Range("A1:C1").Font.Bold = True  
  61.     ThisRange.EntireColumn.ColumnWidth = 18.63  
  62.     ThisRange.Range("A2:C5").Interior.ColorIndex = 16  
  63.     ThisRange.Range("A2:C5").Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid  
  64.     ThisRange.Range("A2:C5").Font.ColorIndex = 2  
  65.     ’ 冻结窗格  
  66.     ThisApplication.ActiveWindow.FreezePanes = False  
  67.     ThisApplication.Range("A2").Select()  
  68.     ThisApplication.ActiveWindow.FreezePanes = True  
  69.   
  70.     ThisWorkbook.SaveAs("D:/Visual Studio Projects/VBAReportDemo/bin/VBAReportDemo.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, _  
  71.         """"FalseFalse, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, _  
  72.         Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, False)  
  73.     ThisWorkbook.Close()  
  74.     ThisApplication.Quit()  
  75.   
  76.     Console.WriteLine("press any key to continue")  
  77.     ’Console.ReadLine()  
  78.   
  79. End Sub  
  80.   
  81. End Module  

 2.VC操作EXCEL2003

[cpp]  view plain copy
  1. /****************************************************************************** 
  2. * 
  3. * 这是上面VBAReportDemo的对应程序 
  4. * 演示使用VC操作Excel 2003 
  5. * 功能:设置单元格,风格,冻结窗格。以及如何将对应的VB代码翻译成VC 
  6. * 
  7. * 时间:2007-04-16 09:31 
  8. * 作者:高宏伟(DukeJoe) 
  9. * QQ:21807822 
  10. * Blog:http://dukejoe.yeah.net 
  11. * 注释:本代码可单独研究,但最好和上面的VBAReportDemo对应看比较好 
  12. * 
  13. * 开发环境:Visual Studio.NET 2003 
  14. * 操作系统:Windows XP Home Edition Service Pack 2 
  15. * 
  16. * 
  17. *****************************************************************************/  
  18. #include <iostream>  
  19.   
  20. using namespace std ;  
  21.   
  22. #import "C:/Program Files/Common Files/Microsoft Shared/OFFICE11/mso.dll" rename("RGB", "MSRGB")  
  23.   
  24. #import "C:/Program Files/Common Files/Microsoft Shared/VBA/VBA6/VBE6EXT.OLB" raw_interfaces_only, /  
  25. rename("Reference""ignorethis"), rename("VBE""JOEVBE")  
  26.   
  27. #import "C:/Program Files/Microsoft Office/OFFICE11/excel.exe" exclude("IFont", "IPicture") /  
  28. rename("RGB""ignorethis"), rename("DialogBox""ignorethis"), rename("VBE""JOEVBE"), /  
  29. rename("ReplaceText""JOEReplaceText"), rename("CopyFile","JOECopyFile"), /  
  30. rename("FindText""JOEFindText"), rename("NoPrompt""JOENoPrompt")  
  31.   
  32. using namespace Office;  
  33. using namespace VBIDE;  
  34. using namespace Excel ;  
  35.   
  36. int ExportExcelFile() ;  
  37.   
  38. int main(int argc, char* argv[])  
  39. {  
  40.     if(FAILED(::CoInitialize(NULL)))  
  41.         return 1 ;  
  42.   
  43.     ExportExcelFile() ;  
  44.   
  45.     ::CoUninitialize();  
  46.   
  47.     return 0;  
  48. }  
  49.   
  50. int ExportExcelFile()  
  51. {  
  52.     _ApplicationPtr pApplication = NULL ;  
  53.     _WorkbookPtr pThisWorkbook = NULL ;  
  54.     _WorksheetPtr pThisWorksheet = NULL ;  
  55.     SheetsPtr pThisSheets = NULL ;  
  56.     RangePtr pThisRange = NULL ;  
  57.     _variant_t vt ;  
  58.     Excel::XlFileFormat vFileFormat ;  
  59.     Excel::XlSaveAsAccessMode vSaveAsAccessMode ;  
  60.     Excel::XlSaveConflictResolution vSaveConflictResolution ;  
  61.   
  62.     pApplication.CreateInstance("Excel.Application");  
  63.     pApplication->PutVisible (0,VARIANT_TRUE);  
  64.     pThisWorkbook = pApplication->GetWorkbooks()->Add() ;  
  65.     pThisSheets = pThisWorkbook->GetWorksheets() ;  
  66.     pThisWorksheet = pThisSheets->GetItem((short)1);  
  67.     // 设置整个sheet的填充色为白色  
  68.     pThisWorksheet->GetCells()->GetInterior()->PutColor(RGB(255, 255, 255));  
  69.     pThisWorksheet->GetCells()->ClearContents() ;  
  70.     pThisRange = pThisWorksheet->GetRange("A1:C5") ;  
  71.     pThisRange->ClearFormats() ;  
  72.     // 如果有不会的,可以在debug文件夹的excel.tlh里找找  
  73.     //pThisRange->GetItem(1,1) ;  
  74.     pThisRange->PutItem(1, 1, _variant_t("哈尔滨市平房区")) ;  
  75.     pThisRange->PutItem(1, 2, _variant_t("高宏伟")) ;  
  76.     pThisRange->PutItem(1, 3, _variant_t("QQ:21807822")) ;  
  77.     pThisRange->PutItem(2, 1, _variant_t("1")) ;  
  78.     pThisRange->PutItem(3, 1, _variant_t("2")) ;  
  79.     pThisRange->PutItem(4, 1, _variant_t("3")) ;  
  80.     pThisRange->PutItem(5, 1, _variant_t("4")) ;  
  81.     // 为Range的四周和内部加上边框  
  82.     pThisRange->GetBorders()->GetItem(xlEdgeLeft)->PutLineStyle(xlContinuous) ;  
  83.     pThisRange->GetBorders()->GetItem(xlEdgeTop)->PutLineStyle(xlContinuous) ;  
  84.     pThisRange->GetBorders()->GetItem(xlEdgeRight)->PutLineStyle(xlContinuous) ;  
  85.     pThisRange->GetBorders()->GetItem(xlEdgeBottom)->PutLineStyle(xlContinuous) ;  
  86.     pThisRange->GetBorders()->GetItem(xlInsideHorizontal)->PutLineStyle(xlContinuous) ;  
  87.     pThisRange->GetBorders()->GetItem(xlInsideVertical)->PutLineStyle(xlContinuous) ;  
  88.     // 设置第一行的格式(背景色、粗体、颜色、列宽)  
  89.     pThisRange->GetRange("A1:C1")->GetInterior()->ColorIndex = 47 ;  
  90.     pThisRange->GetRange("A1:C1")->GetInterior()->Pattern = xlPatternSolid ;  
  91.     pThisRange->GetRange("A1:C1")->GetFont()->ColorIndex = 6 ;  
  92.     pThisRange->GetRange("A1:C1")->GetFont()->Bold = TRUE ;  
  93.     pThisRange->GetEntireColumn()->ColumnWidth = 18.63 ;  
  94.     pThisRange->GetRange("A2:C5")->GetInterior()->ColorIndex = 16 ;  
  95.     pThisRange->GetRange("A2:C5")->GetInterior()->Pattern = xlPatternSolid ;  
  96.     pThisRange->GetRange("A2:C5")->GetFont()->ColorIndex = 2 ;  
  97.     // 冻结窗格  
  98.     pApplication->ActiveWindow->FreezePanes = FALSE ;  
  99.     pApplication->Range["A2"]->Select() ;  
  100.     pApplication->ActiveWindow->FreezePanes = TRUE ;  
  101.   
  102.     // 存盘退出  
  103.     vSaveAsAccessMode = xlNoChange ;  
  104.     vFileFormat = xlWorkbookNormal ;  
  105.     vSaveConflictResolution = xlLocalSessionChanges ;  
  106.     pThisWorkbook->SaveAs(_variant_t("D://Visual Studio Projects//VCReportDemo//joe.xls"), vFileFormat,_variant_t(""),_variant_t(""), _variant_t(false),  
  107.     _variant_t(false), vSaveAsAccessMode, vSaveConflictResolution, _variant_t(false)) ;  
  108.     pThisWorkbook->Close();  
  109.     pApplication->Quit();  
  110.   
  111.     return 0 ;  
  112. }  

 

原文网址:http://blog.donews.com/dukejoe/archive/2007/04/16/1156319.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值