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

本文介绍如何通过VBA代码在Excel中调整单元格字体大小、对齐方式及边框样式等,包括设置字体大小为12或16,内容居中对齐,以及仅设置单元格的下边框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.現在數據是可以發送到Excel單元格裡了,但我想用代碼控制某一單元格字體大一些?(如:fontsize=12   or   fontsize=16)  
   
  2.如何控制單元格的內容靠左或居中或靠右?  
   
  3.如何控制單元下邊框有下劃線,也就是下邊框在打印時能夠打印出來?  
   
   
  請各位大夾指教,謝謝!  
 
问题点数: 100、回复次数: 8
1楼  3tzjq   (永不言弃) 二星用户 该版得分小于等于30000分,大于10000分  回复于 2005-06-24 10:00:29  得分 95

你要的功能都在里面,慢慢看...  
   
  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  
 
2楼  hdhai9451   (新新人类) 四级用户 该版得分小于等于2000分,大于1000分  回复于 2005-06-24 10:21:47  得分 0

先謝謝   3tzjq(永不言弃)  
   
  現在我正在做,字體大小已經調整出來
3楼  hdhai9451   (新新人类) 四级用户 该版得分小于等于2000分,大于1000分  回复于 2005-06-24 10:41:43  得分 0

to:3tzjq(永不言弃)  
   
  .Range(.Cells(1,   1),   .Cells(rowCount   +   1,   colCount)).Borders.LineStyle   =   1   '设表格边框样式  
   
  這個語句得到的是單元格的上下左右邊框都得到,現在我想要的是只得到這個單元格的下邊框,可以嗎?  
 
4楼  3tzjq   (永不言弃) 二星用户 该版得分小于等于30000分,大于10000分  回复于 2005-06-24 10:56:10  得分 0

to     hdhai9451(New   New   People---新新人类):  
  已发送短消息!
5楼  wangchong   (网虫) 二级用户 该版得分小于等于500分,大于100分  回复于 2005-06-24 11:25:09  得分 5

3tzjq(永不言弃)  
  大哥讲的很详细了,我没什么要说了!!!
6楼  hdhai9451   (新新人类) 四级用户 该版得分小于等于2000分,大于1000分  回复于 2005-06-24 11:30:33  得分 0

不好意思,我還是調不出來?  
   
  1.這個語句得到的是單元格的上下左右邊框都得到,現在我想要的是只得到這個單元格的下邊框,可以嗎?  
  2.還有單元格內容靠左或居中或靠右  
 
7楼  3tzjq   (永不言弃) 二星用户 该版得分小于等于30000分,大于10000分  回复于 2005-06-24 13:58:10  得分 0

1,像这样________________对吗?  
        这样就行了。你也可以改成其它样式或颜色。里面有相应参数设置。  
        .Range(.Cells(1,   1),   .Cells(rowCount   +   1,   colCount)).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle   =   Excel.XlLineStyle.xlContinuous  
   
  2,   .Range(.Cells(1,   1),   .Cells(rowCount   +   1,   colCount)).HorizontalAlignment=excel.XlHAlign.xlHAlignRight'右对齐,可以设成其它对齐方式

转载于:https://www.cnblogs.com/365haha/archive/2006/11/09/555783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值