1.VB操作EXCEL2003
- ‘************************************************************************
- ‘
- ‘演示用VB.NET设置Excel单元格值和风格的小程序(例程)
- ‘程序功能是:打开文件,设置单元格的风格和值,冻结窗口等
- ‘网上类似的程序很多,但Excel2003的SaveAs函数由新版本的函数代替,原函数
- ‘为_saveas,这个程序同时也演示了如何使用新函数,另外也演示了如何冻结窗格
- ‘
- ‘关键字:Excel2003SaveAs冻结窗格单元格风格
- ‘开发环境:VisualStudio.NET2003
- ‘Excel环境:Excel2003
- ‘作者:高宏伟(DukeJoe)
- ‘时间:2006-03-1211:27
- ‘地点:黑龙江省哈尔滨市平房区
- ‘注释:无
- ‘***********************************************************************
- ModuleModule1
-
- SubMain()
- DimThisApplicationAsMicrosoft.Office.Interop.Excel.Application=NewMicrosoft.Office.Interop.Excel.Application
- DimThisWorkbookAsMicrosoft.Office.Interop.Excel.Workbook
- DimThisSheetsAsMicrosoft.Office.Interop.Excel.Sheets
- DimThisWorkSheetAsMicrosoft.Office.Interop.Excel.Worksheet
- DimThisRangeAsMicrosoft.Office.Interop.Excel.Range
-
- ThisApplication.Visible=True
- ThisWorkbook=ThisApplication.Workbooks.Add()
- Console.WriteLine(ThisWorkbook.Name)
- ThisSheets=ThisWorkbook.Worksheets
- ThisWorkSheet=ThisSheets(1)
- Console.WriteLine(ThisWorkSheet.Name)
- ’设置整个sheet的填充色为白色
- ThisWorkSheet.Cells.Interior.Color=RGB(255,255)
- ThisWorkSheet.Cells.ClearContents()
- ThisRange=ThisWorkSheet.Range("A1:C5")
- Console.WriteLine(ThisRange.Cells(1,1).Value)
- ’ThisRange.Interior.Color=0
- ThisRange.ClearFormats()
- ThisRange.Cells(1,1).Value="哈尔滨市平房区"
- ThisRange.Cells(1,2).Value="高宏伟"
- "QQ:21807822"
- ThisRange.Cells(2,1).Value="1"
- ThisRange.Cells(3,1).Value="2"
- ThisRange.Cells(4,1).Value="3"
- ThisRange.Cells(5,1).Value="4"
- ’为Range的四周和内部加上边框
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ThisRange.Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle=Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
- ’设置第一行的格式(背景色、粗体、颜色、列宽)
- ThisRange.Range("A1:C1").Interior.ColorIndex=47
- ThisRange.Range("A1:C1").Interior.Pattern=Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid
- ThisRange.Range("A1:C1").Font.ColorIndex=6
- ThisRange.Range("A1:C1").Font.Bold= ThisRange.EntireColumn.ColumnWidth=18.63
- ThisRange.Range("A2:C5").Interior.ColorIndex=16
- ThisRange.Range("A2:C5").Interior.Pattern=Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid
- ThisRange.Range("A2:C5").Font.ColorIndex=2
- ’冻结窗格
- ThisApplication.ActiveWindow.FreezePanes=False
- ThisApplication.Range("A2").Select()
- ThisWorkbook.SaveAs("D:/VisualStudioProjects/VBAReportDemo/bin/VBAReportDemo.xls",Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7,_
- "","",False,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,_
- Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges,False)
- ThisWorkbook.Close()
- ThisApplication.Quit()
- Console.WriteLine("pressanykeytocontinue")
- ’Console.ReadLine()
- EndSub
- Module
2.VC操作EXCEL2003
-
- *这是上面VBAReportDemo的对应程序
- *演示使用VC操作Excel2003
- *功能:设置单元格,风格,冻结窗格。以及如何将对应的VB代码翻译成VC
- *时间:2007-04-1609:31
- *作者:高宏伟(DukeJoe)
- *QQ:21807822
- *Blog:http://dukejoe.yeah.net
- *注释:本代码可单独研究,但最好和上面的VBAReportDemo对应看比较好
- *开发环境:VisualStudio.NET2003
- *操作系统:WindowsXPHomeEditionServicePack2
- *
- *****************************************************************************/
- #include<iostream>
- usingnamespacestd;
- #import"C:/ProgramFiles/CommonFiles/MicrosoftShared/OFFICE11/mso.dll"rename("RGB","MSRGB")
- #import"C:/ProgramFiles/CommonFiles/MicrosoftShared/VBA/VBA6/VBE6EXT.OLB"raw_interfaces_only,/
- rename("Reference","ignorethis"),rename("VBE","JOEVBE")
- #import"C:/ProgramFiles/MicrosoftOffice/OFFICE11/excel.exe"exclude("IFont","IPicture")/
- rename("RGB",rename("DialogBox","JOEVBE"),/
- rename("ReplaceText","JOEReplaceText"),rename("CopyFile","JOECopyFile"),/
- rename("FindText","JOEFindText"),rename("NoPrompt","JOENoPrompt")
- namespaceOffice;
- namespaceVBIDE;
- namespaceExcel;
- intExportExcelFile();
- intmain(intargc,char*argv[])
- {
- if(FAILED(::CoInitialize(NULL)))
- return1;
- ExportExcelFile();
- ::CoUninitialize();
- return0;
- }
- intExportExcelFile()
- _ApplicationPtrpApplication=NULL;
- _WorkbookPtrpThisWorkbook=NULL;
- _WorksheetPtrpThisWorksheet=NULL;
- SheetsPtrpThisSheets=NULL;
- RangePtrpThisRange=NULL;
- _variant_tvt;
- Excel::XlFileFormatvFileFormat;
- Excel::XlSaveAsAccessModevSaveAsAccessMode;
- Excel::XlSaveConflictResolutionvSaveConflictResolution;
- pApplication.CreateInstance("Excel.Application");
- pApplication->PutVisible(0,VARIANT_TRUE);
- pThisWorkbook=pApplication->GetWorkbooks()->Add();
- pThisSheets=pThisWorkbook->GetWorksheets();
- pThisWorksheet=pThisSheets->GetItem((short)1);
- //设置整个sheet的填充色为白色
- pThisWorksheet->GetCells()->GetInterior()->PutColor(RGB(255,255));
- pThisWorksheet->GetCells()->ClearContents();
- pThisRange=pThisWorksheet->GetRange("A1:C5");
- pThisRange->ClearFormats();
- //如果有不会的,可以在debug文件夹的excel.tlh里找找
- //pThisRange->GetItem(1,1);
- pThisRange->PutItem(1,1,_variant_t("哈尔滨市平房区"));
- pThisRange->PutItem(1,2,_variant_t("高宏伟"));
- "QQ:21807822"));
- pThisRange->PutItem(2,_variant_t("1"));
- pThisRange->PutItem(3,_variant_t("2"));
- pThisRange->PutItem(4,_variant_t("3"));
- pThisRange->PutItem(5,_variant_t("4"));
- //为Range的四周和内部加上边框
- pThisRange->GetBorders()->GetItem(xlEdgeLeft)->PutLineStyle(xlContinuous);
- pThisRange->GetBorders()->GetItem(xlEdgeTop)->PutLineStyle(xlContinuous);
- pThisRange->GetBorders()->GetItem(xlEdgeRight)->PutLineStyle(xlContinuous);
- pThisRange->GetBorders()->GetItem(xlEdgeBottom)->PutLineStyle(xlContinuous);
- pThisRange->GetBorders()->GetItem(xlInsideHorizontal)->PutLineStyle(xlContinuous);
- pThisRange->GetBorders()->GetItem(xlInsideVertical)->PutLineStyle(xlContinuous);
- //设置第一行的格式(背景色、粗体、颜色、列宽)
- pThisRange->GetRange("A1:C1")->GetInterior()->ColorIndex=47;
- pThisRange->GetRange("A1:C1")->GetInterior()->Pattern=xlPatternSolid;
- pThisRange->GetRange("A1:C1")->GetFont()->ColorIndex=6;
- pThisRange->GetRange("A1:C1")->GetFont()->Bold=TRUE;
- pThisRange->GetEntireColumn()->ColumnWidth=18.63;
- pThisRange->GetRange("A2:C5")->GetInterior()->ColorIndex=16;
- pThisRange->GetRange("A2:C5")->GetInterior()->Pattern=xlPatternSolid;
- pThisRange->GetRange("A2:C5")->GetFont()->ColorIndex=2;
- //冻结窗格
- pApplication->ActiveWindow->FreezePanes=FALSE;
- pApplication->Range["A2"]->Select();
- pApplication->ActiveWindow->FreezePanes=TRUE;
- //存盘退出
- vSaveAsAccessMode=xlNoChange;
- vFileFormat=xlWorkbookNormal;
- vSaveConflictResolution=xlLocalSessionChanges;
- pThisWorkbook->SaveAs(_variant_t("D://VisualStudioProjects//VCReportDemo//joe.xls"),vFileFormat,_variant_t(""),_variant_t(false),
- _variant_t(false));
- pThisWorkbook->Close();
- pApplication->Quit();
- }
原文网址:http://blog.donews.com/dukejoe/archive/2007/04/16/1156319.aspx (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|