更新 Excel 文件中的小计

发布于 2024-08-16 14:35:19 字数 698 浏览 6 评论 0原文

我使用 OpenXML 库获取数据表并将其粘贴到预先格式化的 Excel 文件中。这很好用。

我遇到的问题是预格式化 Excel 文件的顶部有一个小计行,它被设置为对该数据的每一列进行小计(因此每列的顶部都有一个小计)。当我在创建Excel文件后打开它时,这些值都设置为0,插入数据表时它们没有更新。如果突出显示这些小计单元格之一,然后按 Enter 键,它将更新并显示正确的值。

当用户打开下载的电子表格时,让这些值更新并显示正确值的最简单方法是什么?

创建电子表格的代码:

MemoryStream memoryStream = SpreadsheetReader.StreamFromFile(TemplateDirectory + @"\" + "exceltTemplate.xlsx");
doc = SpreadsheetDocument.Open(memoryStream, true);
worksheetPart = SpreadsheetReader.GetWorksheetPartByName(doc, currentSheetName);
writer = new WorksheetWriter(doc, worksheetPart);
cellName = "A8";
writer.PasteDataTable(reports.Tables[0], cellName);
SpreadsheetWriter.Save(doc);

I using the OpenXML library to take a datatable and paste it into a pre formatted excel file. This works fine.

The problem I have is there is a subtotal row at the top of the pre-formatted excel file, that is set to subtotal each column of this data (so there is a subtotal at the top of each column). When I open the excel file after it has been created, these values are all set to 0, they have not updated when the datatable was inserted. If you highlight one of these subtotal cells and then press enter, it updates and shows the right value.

What is the easiest way to get these values to update and show the correct value as soon as the user opens the downloaded spreadsheet?

Code for creating the spreadsheet:

MemoryStream memoryStream = SpreadsheetReader.StreamFromFile(TemplateDirectory + @"\" + "exceltTemplate.xlsx");
doc = SpreadsheetDocument.Open(memoryStream, true);
worksheetPart = SpreadsheetReader.GetWorksheetPartByName(doc, currentSheetName);
writer = new WorksheetWriter(doc, worksheetPart);
cellName = "A8";
writer.PasteDataTable(reports.Tables[0], cellName);
SpreadsheetWriter.Save(doc);

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

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

发布评论

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

评论(1

淡看悲欢离合 2024-08-23 14:35:19

OpenXML 库不会重新计算公式的结果。解决此问题的一种方法是从小计单元格中删除值(而不是公式)。这会导致 Excel 自动重新计算。

这个链接更详细地解释了它。大约在页面的中间位置,您将看到以下代码,这对您的需求非常重要:

// remove all values of cells with formulas on a sheet
// so that Excel refreshes them upon Open
public static void ClearAllValuesInSheet
      (SpreadsheetDocument spreadSheet, string sheetName)
{
    WorksheetPart worksheetPart =
        GetWorksheetPartByName(spreadSheet, sheetName);

    foreach (Row row in
       worksheetPart.Worksheet.
          GetFirstChild().Elements())
    {
    foreach (Cell cell in row.Elements())
    {
        if (cell.CellFormula != null &&
              cell.CellValue != null)
        {
        cell.CellValue.Remove();
        }
    }

    }

    worksheetPart.Worksheet.Save();
}

此代码删除包含公式和值的所有单元格的值。为了加快速度,您可以非常轻松地自定义它以仅处理小计单元格。

The OpenXML library does not recalculate the results of formulas. One way to solve this is to remove the values (not the formulas) from your subtotal cells. This causes Excel to do an automatic re-calculate.

This link explains it in more detail. About halfway down the page, you'll see the following code, which is the bit that is important for your needs:

// remove all values of cells with formulas on a sheet
// so that Excel refreshes them upon Open
public static void ClearAllValuesInSheet
      (SpreadsheetDocument spreadSheet, string sheetName)
{
    WorksheetPart worksheetPart =
        GetWorksheetPartByName(spreadSheet, sheetName);

    foreach (Row row in
       worksheetPart.Worksheet.
          GetFirstChild().Elements())
    {
    foreach (Cell cell in row.Elements())
    {
        if (cell.CellFormula != null &&
              cell.CellValue != null)
        {
        cell.CellValue.Remove();
        }
    }

    }

    worksheetPart.Worksheet.Save();
}

This code removes the values for all cells that contain formulas and values. To speed it up you can customize it pretty easily to just deal with your subtotal cells.

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