openXML(C#) 是否支持生成格式化的数字字段(如 1,000,000)

发布于 2024-11-14 13:24:34 字数 1877 浏览 2 评论 0原文

我有使用 OpenXML 从 Excel 生成电子表格的现有代码。它工作正常,但我总是必须随后进入电子表格并添加格式。

无论如何,在我的 C# 代码中,我可以指定我想要一个使用特定数字格式(逗号、小数等)格式化的列吗?

任何示例的链接都会很棒。

这是我现在保存数据的方式:

 public void SetCell<T>(string column, uint rowIndex, T value)
    {
        SheetData sheetData = _positionSheet;
        Cell cell = GetCell(sheetData, column, rowIndex);
        string stringValue = value == null ? "" : value.ToString();
        cell.CellValue = new CellValue(stringValue);
        switch (typeof(T).Name)
        {
            case "Date":
            case "DateTime": 
                cell.DataType = CellValues.Date;
                break;
            case "Int32":
                cell.DataType = CellValues.Number;
                break;
            default:
                cell.DataType = CellValues.String;
                break;
        }
    }

下面是 GetCell() 方法(尽管我猜这与问题并不真正相关:

 static private Cell GetCell(SheetData sheet, string column, uint rowIndex)
    {
        Cell cell;
        Row row = GetRow(sheet, rowIndex);
        if (row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).Any())
        {
            cell = row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).First();
        }
        else
        {
            Cell refCell = null;
            var comparer = new CellComparer();
            foreach (Cell existingCell in row.Elements<Cell>())
            {
                if (comparer.Compare(existingCell.CellReference.Value, column + rowIndex) > 0)
                {
                    refCell = existingCell;
                    break;
                }
            }

            cell = new Cell { CellReference = column + rowIndex };
            row.InsertBefore(cell, refCell);
        }
        return cell;
    }

i have existing code that generates a spreadsheet from excel using OpenXML. It works fine but i always have to go into the spreadsheet afterwards and add formatting.

Is there anyway in my C# code I can specify that i want a column formatted with a certain number formatting) commas, decimals, etc?

any links to examples would be great.

here is how i am saving data now:

 public void SetCell<T>(string column, uint rowIndex, T value)
    {
        SheetData sheetData = _positionSheet;
        Cell cell = GetCell(sheetData, column, rowIndex);
        string stringValue = value == null ? "" : value.ToString();
        cell.CellValue = new CellValue(stringValue);
        switch (typeof(T).Name)
        {
            case "Date":
            case "DateTime": 
                cell.DataType = CellValues.Date;
                break;
            case "Int32":
                cell.DataType = CellValues.Number;
                break;
            default:
                cell.DataType = CellValues.String;
                break;
        }
    }

below is the GetCell() method (even though i guess thats not really relevant for the question:

 static private Cell GetCell(SheetData sheet, string column, uint rowIndex)
    {
        Cell cell;
        Row row = GetRow(sheet, rowIndex);
        if (row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).Any())
        {
            cell = row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).First();
        }
        else
        {
            Cell refCell = null;
            var comparer = new CellComparer();
            foreach (Cell existingCell in row.Elements<Cell>())
            {
                if (comparer.Compare(existingCell.CellReference.Value, column + rowIndex) > 0)
                {
                    refCell = existingCell;
                    break;
                }
            }

            cell = new Cell { CellReference = column + rowIndex };
            row.InsertBefore(cell, refCell);
        }
        return cell;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-11-21 13:24:34

是的,这是可能的,但是它都深深地埋藏在风格中。考虑使用 ClosedXML 它的对象模型类似于旧的优秀 Excel 对象模型,并在底层使用 OpenXML。有了它,一切都变得简单多了。

Yes it's possible, however it's all buried deep into styles. Consider using ClosedXML it have object model similar to old good Excel Object Model and uses OpenXML under the hood. Everything is much simplier with it.

十年不长 2024-11-21 13:24:34

您可以使用 ExtremeML,它基于 OpenXML。
如果使用它,您可以定义一个模板,并通过模板生成excel文件。

book1.xlsx 是模板,book2.xlsx 是最终文件。 tabb1 是 excel 2007 中的一个表。

            FileStream fs = new FileStream ( @"d:\book1.xlsx" , FileMode.Open );
        FileStream msm = new FileStream ( @"d:\book2.xlsx" , FileMode.CreateNew );

        using ( var package = SpreadsheetDocumentWrapper.Open ( fs , msm ) ) {
            var table = package.WorkbookPart.GetTablePart ( "tabb1" ).Table;
            var data = new List<object[]> ( );

            for ( var i = 0 ; i < 10 ; i++ ) {
                data.Add ( new object[] { "1" , "2" , "3" , "4" } );
            }

            table.Fill ( data.ToArray ( ) );
        }

        fs.Close ( );
        msm.Close ( );

You can use ExtremeML, it's based on OpenXML.
If use it, you can define a template, and generate excel file by template.

book1.xlsx is template, book2.xlsx is finally file. tabb1 is a table in excel 2007.

            FileStream fs = new FileStream ( @"d:\book1.xlsx" , FileMode.Open );
        FileStream msm = new FileStream ( @"d:\book2.xlsx" , FileMode.CreateNew );

        using ( var package = SpreadsheetDocumentWrapper.Open ( fs , msm ) ) {
            var table = package.WorkbookPart.GetTablePart ( "tabb1" ).Table;
            var data = new List<object[]> ( );

            for ( var i = 0 ; i < 10 ; i++ ) {
                data.Add ( new object[] { "1" , "2" , "3" , "4" } );
            }

            table.Fill ( data.ToArray ( ) );
        }

        fs.Close ( );
        msm.Close ( );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文