c Microsoft Interop Excel change font style for s selected range

0 votes

Some of the cells in an Excel sheet need to have their font type and style changed. Microsoft.Office.Interop.Excel creates Excel sheets.

The B3 to B100 range should now be in the Arial font style. This is my code.

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
worksheet.get_Range("B3", "B100").Cells.Font.FontStyle = "Arial";

for (int i = 1; i < records_datagridview.Columns.Count + 1; i++)
{
   worksheet.Cells[1, i] = records_datagridview.Columns[i - 1].HeaderText;
}

for (int i = 0; i < records_datagridview.Rows.Count; i++)
{
   for (int j = 0; j < records_datagridview.Columns.Count; j++)
   {
      worksheet.Cells[i + 2, j + 1] = records_datagridview.Rows[i].Cells[j].Value.ToString();
   }
}

String file_path = fbd.SelectedPath + "/records_sheet_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + "_.xlsx";

workbook.SaveAs(file_path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing);

app.Quit();

I used

worksheet.get_Range("B3", "B4").Cells.Font.FontStyle = "Arial";

But it's not working Can any one help me on this

Oct 31, 2022 in Others by Kithuzzz
• 38,000 points
1,981 views

1 answer to this question.

0 votes

Change your code to this:

worksheet.get_Range("B3", "B4").Cells.Font.Name = "Arial";

Tip: In many cases, you can answer questions like these yourself: simply record an Excel macro and look at the generated code. Even though it is VBA, not C#, you will often see what you need.

answered Oct 31, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How to reference Microsoft.Office.Interop.Excel dll?

Use NuGet (VS 2013+): The NuGet package manager ...READ MORE

answered Oct 9, 2022 in Others by narikkadan
• 63,600 points
2,869 views
+1 vote
1 answer

Excel macro to make selected range absolute or relative reference?

Try  this: Sub ConverReferenceType() On ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,600 points
1,569 views
0 votes
1 answer

How to add Conditional Formatting in Excel for a Range of Values

Three distinct rules are required, one for ...READ MORE

answered Oct 24, 2022 in Others by narikkadan
• 63,600 points
1,158 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,513 views
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
783 views
0 votes
1 answer

How to create and download excel document using asp.net

First, download the Open XML Format SDK 2.0. It comes ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
2,961 views
0 votes
1 answer

Merge and export Excel/Word/PDF to PDF

Use GroupDocs.Merger for .NET API to merge Word, Excel, ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,506 views
0 votes
1 answer

'Microsoft.Office.Interop.Excel.Range' does not contain a definition for 'get_Default'

You are using C# version 4, the ...READ MORE

answered Oct 16, 2022 in Others by narikkadan
• 63,600 points
2,240 views
0 votes
1 answer

Microsoft.Office.Interop.Excel for Office 2007

Each client must install the  2007 Microsoft ...READ MORE

answered Nov 5, 2022 in Others by narikkadan
• 63,600 points
1,585 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP