使用C#更改excel中一个单元格的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11740856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:02:13  来源:igfitidea点击:

Changing font size of one cell in excel using C#

c#excel

提问by Arnout

I am working on a project that writes data to an Excel file.

我正在开发一个将数据写入 Excel 文件的项目。

Everything is finished now, however I need a few cells with a bigger size than the rest (title, etc).

现在一切都完成了,但是我需要一些比其他单元格(标题等)更大的单元格。

I have read about this about the internet, but I keep having the same problem: when I execute my code (see below for what I have tried), everything in the worksheet becomes larger.

我已经在互联网上阅读了有关此内容的信息,但我一直遇到同样的问题:当我执行我的代码时(有关我尝试过的内容,请参见下文),工作表中的所有内容都变得更大。

What I already have tried:

我已经尝试过的:

worksheet.Rows[1].Cells[7].Style.Font.Size = 20; 

worksheet.get_Range("A7", "A7").Style.Font.Size = 20;

None of this seems to work; what is the correct way to increase a cell's font size?

这些似乎都不起作用;增加单元格字体大小的正确方法是什么?

回答by paul

I would just use:

我只会使用:

worksheet.Range["A7"].Style.Font.Size = 20;

edit: sorry, wrong brackets

编辑:对不起,错误的括号

回答by kornbread

I had to use:

我不得不使用:

worksheet.get_Range("A7", "A7").Cells.Font.Size = 20;

回答by Wayne

If the data is consistent and will always be written to the same cells then this is the simplest solution - works well for product details / contact infotype exporting

如果数据一致并且将始终写入相同的单元格,那么这是最简单的解决方案 - 适用于产品详细信息/联系信息类型导出

// set cell A7
worksheet.get_Range("A7", "A7").Font.Size = 20;

// set cells A7, A8
worksheet.get_Range("A7", "A8").Font.Size = 20;

// set cells A7, B7
worksheet.get_Range("A7", "B7").Font.Size = 20;

// set cells A7, A8, B7, B8
worksheet.get_Range("A7", "B8").Font.Size = 20;

If the data varies and will sometimes be written to multiple rows/columns then something like this is more simple - works well for dataset / shopping listtype exporting

如果数据变化并且有时会被写入多行/列,那么这样的事情更简单 - 适用于数据集/购物清单类型导出

int RowNum;
int ColNum;

// some code to set variables

worksheet.Cells[RowNum, ColNum].Font.Size = 20; 

回答by Seichi

When working with interop excel, try not to write your code with "two dots" in order to clean interop excel objects. This also helps having your code more readable. Anyway, to answer your question, and using what I have pointed out... all you have to do is:

使用 interop excel 时,尽量不要用“两个点”编写代码以清理 interop excel 对象。这也有助于让您的代码更具可读性。无论如何,要回答你的问题,并使用我所指出的......你所要做的就是:

//Declare your variables
Application excel = null;
Workbook excelworkBook = null;
Range excelCellrange = null;
Worksheet worksheet = null;
Font excelFont =null;

//start your application
excel = new Application();
try
{
   ...
   //your code goes here...
   excelCellrange = worksheet.Range[worksheet.Cells[1,7],worksheet.Cells[1,7]];
   excelFont = excelCellrange.Font;
   excelfont.Size = 20;
   ...
   ...
}
catch(Exception ex){
}
finally{
   //here put something to clean the interop objects as the link above.
   ...
   Marshal.ReleaseComObject(excelfont);
   ...
}