您的位置:首页 > 编程语言 > VB

如何用VB.NET控制Excel單元格裡的內容

2006-07-18 15:43 363 查看
Public  Sub  SetSheetStyles(ByVal  xlSheet  As  Excel.Worksheet,  _  
                                                                             ByVal  rowCount  As  Int32,  _  
                                                                             ByVal  colCount  As  Int32)  
               Dim  i,  intStart  As  Int32  
 
               With  xlSheet  
 
 
                       If  AllowHeaderColor  Then  .Range(.Cells(1,  1),  .Cells(1,  MasterColumnCount)).Interior.Color  =  GetHeaderBackColorRGB  
 
                       If  IsExistMasterD  Then  '主从表  
                               intStart  =  5  
 
                               .Range(.Cells(1,  1),  .Cells(1,  MasterColumnCount)).Font.Size  =  10  '标头字体大小  
                               .Range(.Cells(2,  1),  .Cells(2,  MasterColumnCount)).Font.Size  =  10  '主数据字体大小  
 
                               .Range(.Cells(1,  1),  .Cells(1,  MasterColumnCount)).Font.Bold  =  True      '标题字体加粗  
                               .Range(.Cells(2,  1),  .Cells(2,  MasterColumnCount)).Font.Italic  =  True      '主数据字体斜体  
 
                               .Range(.Cells(1,  1),  .Cells(1,  MasterColumnCount)).Borders.LineStyle  =  1    '粗边框(已取消)  
                               .Range(.Cells(2,  1),  .Cells(2,  MasterColumnCount)).Borders.LineStyle  =  1  '粗边框(已取消)  
 
                               If  AllowHeaderColor  Then  .Range(.Cells(4,  1),  .Cells(4,  colCount)).Interior.Color  =  GetHeaderBackColorRGB  
                               .Range(.Cells(4,  1),  .Cells(rowCount  +  4,  colCount)).Borders.LineStyle  =  1  '主数据边框样式  
 
                               .Range(.Cells(4,  1),  .Cells(4,  colCount)).Font.Bold  =  True      '标题字体加粗  
                               .Range(.Cells(4,  1),  .Cells(4,  colCount)).Font.Size  =  10  '数据项字体大小  
 
                               .Range(.Cells(5,  1),  .Cells(rowCount  +  4,  colCount)).Font.Size  =  9  '数据项字体大小  
 
                       Else  
                               intStart  =  2  
 
                               .Range(.Cells(1,  1),  .Cells(1,  colCount)).Font.Size  =  10  '标头字体大小  
                               .Range(.Cells(1,  1),  .Cells(1,  colCount)).Font.Bold  =  True      '标题字体加粗  
 
                               .Range(.Cells(1,  1),  .Cells(rowCount  +  1,  colCount)).Borders.LineStyle  =  1  '设表格边框样式  
                               .Range(.Cells(2,  1),  .Cells(rowCount  +  1,  colCount)).Font.Size  =  9  '数据项字体大小  
 
                       End  If  
 
                       If  exportSet.IsApply_PCR_ToExcelWord  AndAlso  exportSet.IsApplyExcel  Then  '允许使用颜色区分  
                               If  rowCount  >  2  AndAlso  exportSet.IsAlwaysQuestion_ApplyPCRToEW  Then  '先询问是否执行颜色区分  
                                       If  MessageBox.Show("数据转换到Excel文档已完成!是否现在就执行奇偶行颜色区分?这可能需要较长的时间。"  &  vbLf  &  "如果不再希望出现此提示,请转到  打印设置的高级选项卡。",  "总是询问",  MessageBoxButtons.YesNo,  MessageBoxIcon.Question,  MessageBoxDefaultButton.Button2)  =  DialogResult.No  Then  Return  
                               End  If  
                               If  rowCount  >  2  Then  Common.Common.OnInfo("开始用颜色来区分奇偶数据行,这可能需要较长的一段时间。请稍候...",  Common.InfoBase.InfoTypeEnum.Working)  
 
                               Dim  bolP  As  Boolean  =  (((rowCount  -  1)  Mod  2)  =  0)  '减去标头行Count  -  1  True=偶行  Even,False=奇行  Odd  
 
                               Dim  startD  As  Double  =  Microsoft.VisualBasic.Timer  
                               Dim  intEBC,  intOBC  As  Int32  
                               intEBC  =  GetEvenBackColorRGB  
                               intOBC  =  GetOddBackColorRGB  
 
                               Dim  intS  As  Int32  =  intStart  
                               rowCount  +=  intS  -  1  
                               For  i  =  intStart  To  rowCount  
                                       If  bolP  Then  '偶行  
                                               .Range(.Cells(i,  1),  .Cells(i,  colCount)).Interior.Color  =  intEBC  
                                       Else  '奇行  
                                               .Range(.Cells(i,  1),  .Cells(i,  colCount)).Interior.Color  =  intOBC  
                                       End  If  
 
                                       bolP  =  Not  bolP  
                               Next  i  
 
                               If  rowCount  >  2  Then  Common.Common.OnInfo("颜色区分奇偶数据行已完成!用时:"  &  (Microsoft.VisualBasic.Timer  -  startD).ToString  &  "  秒",  Common.InfoBase.InfoTypeEnum.Message)  
                       End  If  
 
               End  With  
 
               With  xlSheet.PageSetup  
                       If  MasterColumnCount  >  6  Then  
                               If  MasterColumnCount  >  12  Then  .PaperSize  =  Excel.XlPaperSize.xlPaperA3  '超过12列就用A3纸  
 
                               .Orientation  =  Excel.XlPageOrientation.xlLandscape  '横幅  
 
                       Else  
                               .Orientation  =  Excel.XlPageOrientation.xlPortrait  '竖幅A4纸  
                       End  If  
 
                       '  .TopMargin  =  1'不改变,因为有Header  
                       '.BottomMargin  =  1  
                       .LeftMargin  =  1  
                       .RightMargin  =  1  
 
                       .CenterHorizontally  =  True  '水平居中对齐  
                       '  .LeftHeaderPicture.Filename  =  Application.StartupPath  &  "/Resources/TRI-T  Icon.gif"  
                       .LeftHeader  =  "&""宋体,Bold""&13"  &  "TRI-T  Company  Limited"  '公司  
                       .CenterHeader  =  "&""宋体,Bold""&13"  &  TitleText    '标题"  
                       .LeftFooter  =  "&""宋体""&10制表人:"  &  PreparedBy  '制表人  
                       .CenterFooter  =  "&""宋体""&10制表日期:"  &  GetDatasheetDate  
                       .RightFooter  =  "&""宋体""&10第&P页  共&N页"  
               End  With  
 
       End  Sub  
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vb.net excel header 文档