打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何用VB.NET控制Excel單元格裡的內容
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  
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
读取Excel内容到MSHFlexgrid
VB.NET向Excel写入并保存数据
Delphi读取excel数据
.Net创建Exce文件实现插入修改方法总结 - IT168 技术开发专区
C#与EXCEL的数据交互(一)
C#常见技能_Excel文件读写
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服