3

我从我的 LightSwitch 计费应用程序中生成发票,生成发票后,Word 文档中的 SubTotal、SalesTax 和 Total Due 单元格的格式不正确。

我已经浏览了每一个菜单选项,并探索了其他可能的方法来在将其放入文档的内容控件之前对其进行格式化,但我就是无法获得正确的格式。

数字显示为4100.0000,但它们应该类似于4,100.00或至少类似于4100.00

如何才能做到这一点?

我意识到这是离题的边缘,但我想我还是会在这里发帖,因为我相信解决方案涉及编写代码,而不是使用 Word 来完成此任务,因为我已经完成了所有选项,而且只是没有那里似乎不是更改单个单元格格式的选项。

在此处输入图像描述

根据要求,文档生成代码:

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using OfficeIntegration;
namespace LightSwitchApplication
{
    public partial class Billing
    {
        partial void GenerateInvoice_Execute()
        {
            var mappings = new List<ColumnMapping>();

            mappings.Add(new ColumnMapping("InvoiceId", "InvoiceId"));
            mappings.Add(new ColumnMapping("DateGenerated", "DateGenerated"));

            mappings.Add(new ColumnMapping("InvoiceDataGridSubTotal", "SubTotal"));
            mappings.Add(new ColumnMapping("InvoiceDataGridSalesTax", "Tax"));
            mappings.Add(new ColumnMapping("InvoiceDataGridDateDue", "DateDue"));
            mappings.Add(new ColumnMapping("InvoiceDataGridTotalDue", "Total"));

            object doc;
            string path = @"C:\Users\Jason\Documents\SV Invoice.docx";

            doc = OfficeIntegration.Word.GenerateDocument(path, this.BillingTables.SelectedItem, mappings);
        }
    }
}
4

2 回答 2

2

在这种情况下,不需要代码和 Word 格式。为了使您的数字在动态生成的 Word 文档中正确格式化,您必须在 Visual Studio LightSwitch 的数据设计器中设置要使用的格式:

  • 打开您的 LightSwitch 项目
  • 展开项目节点>打开一个Table包含您要格式化的数字
  • 选择Field需要格式化的
  • Properties窗格中,向下滚动直到您看到 Formatting ( Format Pattern)
  • 现在这里是您格式化数字的地方。有关可用选项,请参阅链接。我选择使用C2.

按照上述步骤,我能够生成一个具有以下格式的 Word 文档:

4500.00而不是4500.0000.

于 2012-11-23T06:22:34.660 回答
1

您应该能够为ColumnMapping构造函数提供第三个参数:格式化值的委托:

Func<decimal, string> formatDelegate = x => x.ToString("c2");
mappings.Add(new ColumnMapping("InvoiceDataGridSubTotal", "SubTotal",
                               FormatDelegate: formatDelegate));
于 2012-11-22T10:21:49.287 回答