Pregled posta

Adresa bloga: https://blog.dnevnik.hr/spireexcel

Marketing

Control Excel Font or Color of One Row by Using C#

Developers often use C# to operate Excel for realizing Excel automation. Generally speaking, there are several parts included in Excel automation, for example, Excel creation, data import and export and so on. In order to generate a complete Excel file, developers often have Excel formatted.
The frequently used formatting in Excel is font styles, which is one part of cell formatting. There are lots of font styles for Excel. When we open an Excel, we can find the Format in menu. After selecting Cell, kinds of formatting tools are displayed. Click the Font tab and we can select the wanted styles for the cells which are needed to be formatted.
Sometimes, we may make specified cells be obvious by using different color and size from other cells. The special cell may be the column title or the most important information. For example, if the data in one cell is the total quantity of one column, the data may be set as red or other bright colors. Although it is easy to set font style, according to users’ requirements, we may come across some difficult questions.
Recently, I found a problem about that how to control one row font or color when exporting Excel by using C#. After trying several methods, the following code may be helpful to solve this problem.
Firstly, we need to set the column format to complete to set Excel Font by using C#.
Excel.Range my range = mysheet.get_Range(mysheet.Cells[1,1], mysheet.Cells[5,1]);
Myrange.NumberFormatLocal = “@”;
The last line code means that the cell format type is text formatting.

Then, set the color of the tenth row as red.
mysheet.get_Range((Excel.Range)mysheet.Cells[10,1],(Excel.Range)mysheet.Cells[10,200]).Select();
mysheet.get_Range((Excel.Range)mysheet.Cells[10,1],(Excel.Range)mysheet.Cells[10,200]).Interior.ColorIndex=3;
mysheet.get_Range((Excel.Range)mysheet.Cells[10,1],(Excel.Range)mysheet.Cells[10,200]).Font.ColorIndex=3;
After running the code, we can find that all the characters in cells of the tenth row become red.


Post je objavljen 04.01.2011. u 09:00 sati.