简体   繁体   中英

Set format for all cells in Excel without selections, using C# and VSTO

Let's assume that I want to set the format ("Text", "Number", "Percent", "Date", and so on) for all cells in the Excel worksheet.

I now how to set the format for a single row or a single column. For example, the code setting the "Percent" format for all cells of the 3rd row looks as follows (the decimal delimiter depends on your Windows localization):

this.worksheet.Rows[3].NumberFormat = "%0,00";

However, when I'm trying to do the same for all cells of the worksheet with the following code:

this.worksheet.Cells.NumberFormat = "%0,00";

, I get a problem - in fact, the format of the cells will be changed but to a some wrong format (in the present case, the real set format will be "#000%", which is recognized by Excel as a "Custom Format").

I know how to solve this problem using selections. So, the following code performs the mentioned task perfectly:

this.worksheet.Cells.Select();
this.worksheet.Application.Selection.NumberFormat = "%0,00";

, however, I really want to solve this problem without selections. How can I do it?

Although I find your responses to people trying to help you very offensive I tried to ignore that a bit to keep in mind that we try to help each other here and focus on finding a solution for the scenario.

So here is my final attempt to please you ...

[TestMethod]
public void TestExcelNumberFormatOnWholeSheet()
{
    Excel.Application excel = new Excel.Application();
    excel.Visible = true;

    Excel.Workbook workbook1 = excel.Workbooks.Add();
    Excel.Worksheet sheet = workbook1.Sheets.Add();

    var lastCol = sheet.Range["a1"].End[Excel.XlDirection.xlToRight].Column;
    var lastRow = sheet.Cells[65536, lastCol].End[Excel.XlDirection.xlDown].Row;

    sheet.Range["a1", sheet.Cells[lastRow, lastCol]].NumberFormat = "%0,00";
}

I've tested it here and seems to work (keep in mind that as you already mentioned you may need to switch the decimal character depending on your localization).

Let me answer my own question.

If shortly, instead of the code

this.worksheet.Cells.NumberFormat = "%0,00";

, one should use the following one:

((dynamic)this.worksheet.Cells).NumberFormat = "%0,00";

Then, the result format will be correct!

Now, my explanation of this: The NumberFormat property looks to be correctly applicable only for the dynamic type, but not for the Microsoft.Office.Interop.Excel.Range type. When one writes something like this.worksheet.Cells or this.worksheet.Range["A1", "C3"] , he/she gets the Microsoft.Office.Interop.Excel.Range type; when one writes something like this.worksheet.Rows[3] , he/she gets the dynamic type. Therefore, in order to use the NumberFormat property, just cast your Microsoft.Office.Interop.Excel.Range object to the dynamic one!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM