最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】C#中格式化Excel文件(的标题变为加粗)

C# crifan 3718浏览 0评论

【问题】

已经搞定了C#中输出excel:

【已解决】将C#中的DataGridView中的数据,导出为CSV或Excel

现在需要把输出的excel中的第一行的内容,即标题,格式化为加粗。

【解决过程】

1.试了试:

xlWorkSheet.Rows[0].

但是结果没有类似的font或style,无法去格式化:

no attribute for font or style to set

 

2.参考:

How to format an Excel file using C#

Format an Excel column (or cell) as Text in C#?

microsoft.interop.excel Formatting cells

然后去试试代码:

            //formatting
            //header to bold
            Range headerRow = xlWorkSheet.get_Range("1:1", System.Type.Missing);
            headerRow.Font.Bold = true;

果然可以了:

header is bold now

 

【总结】

其实就是,按照excel中的,这对cell的操作的语法,去选择对应的Range,然后去设置对应的属性即可。

比如此处的,用

get_Range("1:1", System.Type.Missing)

选择一行,然后去设置选中的此行的字体,变粗体:

headerRow.Font.Bold = true;

转载请注明:在路上 » 【已解决】C#中格式化Excel文件(的标题变为加粗)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.161 seconds, using 22.11MB memory