How to change format of a column of excel sheet in c#?

calendar_today Asked Apr 4, 2013
thumb_up 5 upvotes
history Updated April 14, 2026

Direct Answer

Now ,before the loop I need to change the format of cells under Column-D to Text Format. How to do the same from code in c#? To change the format of the entire column use this //…. This is a 3-line Excel VBA snippet, ranked #282nd of 303 by community upvote score, from 2013.


The Problem (Q-score 6, ranked #282nd of 303 in the Excel VBA archive)

The scenario as originally posted in 2013

I am creating an excel sheet dynamically and inserting values in to the same.But value in some cells are getting inserted in the wrong format.
Following is my excel sheet enter image description here
value in the selected cell should have been 0002-9343 but it is inserted as Feb-43.This due to some wrong format for that column(column-D) in the excel sheet.
I need to change the whole column-D (all cells comming under heading D “data2”) to ‘Text Format’ before entering the data.
Following is the code for creating the excel sheet and inserting data

excel = new Application();
            excel.Visible = false;
            wb = (_Workbook)(excel.Workbooks.Add(System.Reflection.Missing.Value));
            sheet = wb.Sheets.Add();
            sheet.Name = "TestSheet1";
            sheet.Cells[1, "A"].Value2 = "Id";
            sheet.Cells[1, "B"].Value2 = "Name";
            sheet.Cells[1, "C"].Value2 = "Data1";
            sheet.Cells[1, "D"].Value2 = "Data2";
            sheet.Cells[1, "E"].Value2 = "Data3";

            for (int i = 0; i < 10; i++)
            {
                    id = i;
                    result = object;
                    data = JsonConvert.DeserializeObject(result);

        name  = (data != null) ? data.name : string.Empty;
                    data1 = (data != null) ? data.data1 : string.Empty;
                    data2 = (data != null) ? data.data2 : string.Empty;
                    data3 = (data != null) ? data.data3 : string.Empty;

                    sheet.Cells[i + 2, "A"].Value2 = name;  
                    sheet.Cells[i + 2, "B"].Value2 = data1; 
                    sheet.Cells[i + 2, "C"].Value2 = data2; 
                    sheet.Cells[i + 2, "D"].Value2 = data3;
            }
            string ExcelPath = Some_path;
            wb.SaveAsExcelPath,XlFileFormat.xlWorkbookNormal, null, null, false, false, XlSaveAsAccessMode.xlShared, false, false, null, null, null);
            wb.Close(true);
            excel.Quit();

Now ,before the loop I need to change the format of cells under Column-D to Text Format.
How to do the same from code in c#?

Why community consensus is tight on this one

Across 303 Excel VBA entries in the archive, the accepted answer here holds niche answer (below median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — niche answer (below median) (+5)

3-line Excel VBA pattern (copy-ready)

Now ,before the loop I need to change the format of cells under Column-D to Text Format. How to do the same from code in c#?

To change the format of the entire column use this

// 4 is for Col D
sheet.Cells[1, 4].EntireColumn.NumberFormat = "@";

Remember to format Col D before you attempt writing to it. Using .EntireColumn will ensure that you will not have to change the format of the Excel Cells individually 🙂


When to Use It — classic (2013–2016)

Ranked #282nd in its category — specialized fit

This pattern sits in the 99% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2013 and 2026

The answer is 13 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +5 vs the Excel VBA archive median ~4; this entry is niche. The score plus 6 supporting upvotes on the question itself (+6) means the asker and 4 subsequent voters all validated the approach.

Does the 3-line snippet run as-is in Office 2026?
expand_more

Yes. The 3-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

Published around 2013 — what’s changed since?
expand_more

Published 2013, which is 13 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #281?
expand_more

The pattern one rank above is “Apache POI autoSizeColumn() not working right”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 6, Answer-score 5, original post 2013, ranked #282nd of 303 in the Excel VBA archive. Last regenerated April 14, 2026.