Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenXML SDK - Excel Change Cell Format - C#

Tags:

I have a web app in C#. And am using an xlsx template to create documents. The formatting of xlsx is fairly involved and hence I chose the template route instead of creating from scratch. In a cell x2, most data coming in as a percent, but in some cases it would be an amount. But I cannot seem to change the format of that cell when it is amount to percent. The template was saved with percent format in that cell. The code to set the cell value is as below and works for other cells, which were saved with currency format. I tried to set the cell style index to 103U based on that code in another cell with the same format.

How do I change the format of the cell x2? ANy help would be greatly appreciated. Thanks in advance.`

public void setCellValueNum(WorksheetPart ws, int row, int col, Double newVal)
    {
        Cell cl = getCell(ws.Worksheet, getColLetter(col), row);
        cl.CellValue = new CellValue(newVal.ToString());
        cl.CellReference = getColLetter(col) + row.ToString();
        cl.DataType =
            new EnumValue<CellValues>(CellValues.Number);

        //cl.StyleIndex = (UInt32Value)103U;

       }
like image 485
user3150378 Avatar asked Mar 22 '18 21:03

user3150378


People also ask

Does OpenXml support XLS?

xls files. Excel for . NET can load and save data and formatting information in OpenXml files; however, formulas are not loaded or saved. They are copied in BIFF format as opaque.

What is the difference between OpenXml and ClosedXml?

Macros – ClosedXml doesn't support macros as its base library OpenXml also doesn't support it. Embedding – We cannot embed any file into Excel using ClosedXml, no APIs built for that, so some features of OpenXml still need to be implemented. Charts – No functionality related to charting is present.

How do I make the header row bold with OpenXml in Excel?

According to your description, you want to set the font to be bold in the header row of a spreadsheet by Open XML SDK. To edit the font of cell in Open XML SDK, we need to add a new Font element and related CellFormat element under WorkbookStylesPart. Stylesheet element in the spreadsheet.

What is OpenXml for Excel?

Open format? Office Open XML (also informally known as OOXML) is a zipped, XML-based file format developed by Microsoft for representing spreadsheets, charts, presentations and word processing documents. Ecma International standardized the initial version as ECMA-376.


1 Answers

See numbering format its an style. Add style to Excel in openxml.

According to Reading dates from OpenXml Excel files, format with ID value less than 164 are built in. You can find a list of formats there.

Here is an example: Applying % number format to a cell value using OpenXMl . Just change the format to something like

nf2decimal.FormatCode = StringValue.FromString("0.0");

You need to add a style that references the cell. Here is an example: https://blogs.msdn.microsoft.com/chrisquon/2009/11/30/stylizing-your-excel-worksheets-with-open-xml-2-0/

The openXML installs a tool to generate c# code to create a selected xlsx (https://tech.trailmax.info/2014/04/open-xml-sdk-tool-to-analyse-documents-and-generated-c-code/).

like image 100
Xavier Avatar answered Oct 11 '22 12:10

Xavier