對欄位应用自訂格式
有時使用者必須對欄位套用自訂格式。 在這個文章中,我們將會看一些例子來了解這如何可以完成。
更多選項,請參閱 Fields namespace 內相應類別中每個欄位種類的完整屬性清單。
如何對欄位結果應用自訂格式
Aspose.Words 提供 API 來格式化欄位結果。 您可以實作 IFieldResultFormatter 介面來控制結果欄位是如何格式化。 您可以使用數字格式開關, 即 QQ “#. □” , 日期/ 時間格式開關, 即 QQ “dd. MM. yyyy” , 和數字格式開關, 即 QQ Ordinal 。
以下程式碼範例說明如何為欄位結果應用自訂格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
doc.FieldOptions.ResultFormatter = new FieldResultFormatter("[{0:N2}]", null); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Field field = builder.InsertField("=-1234567.89 \\# \"### ### ###.000\"", null); | |
field.Update(); | |
doc.Save(ArtifactsDir + "WorkingWithFields.FormatFieldResult.docx"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
class FieldResultFormatter : IFieldResultFormatter | |
{ | |
public FieldResultFormatter(string numberFormat, string dateFormat) | |
{ | |
mNumberFormat = numberFormat; | |
mDateFormat = dateFormat; | |
} | |
public FieldResultFormatter() | |
: this(null, null) | |
{ | |
} | |
public override string FormatNumeric(double value, string format) | |
{ | |
mNumberFormatInvocations.Add(new object[] { value, format }); | |
return string.IsNullOrEmpty(mNumberFormat) | |
? null | |
: string.Format(mNumberFormat, value); | |
} | |
public override string FormatDateTime(DateTime value, string format, CalendarType calendarType) | |
{ | |
mDateFormatInvocations.Add(new object[] { value, format, calendarType }); | |
return string.IsNullOrEmpty(mDateFormat) | |
? null | |
: string.Format(mDateFormat, value); | |
} | |
public override string Format(string value, GeneralFormat format) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override string Format(double value, GeneralFormat format) | |
{ | |
throw new NotImplementedException(); | |
} | |
private readonly string mNumberFormat; | |
private readonly string mDateFormat; | |
private readonly ArrayList mNumberFormatInvocations = new ArrayList(); | |
private readonly ArrayList mDateFormatInvocations = new ArrayList(); | |
} |
如何評估 IF
條件
如果你想評估 IF
條件後 Mail Merge 的結果,你可以利用 EvaluateCondition 方法立即取得該表達式評估的結果。
以下範例說明了如何使用此方法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
FieldIf field = (FieldIf) builder.InsertField("IF 1 = 1", null); | |
FieldIfComparisonResult actualResult = field.EvaluateCondition(); | |
Console.WriteLine(actualResult); |
如何將自訂格式套用到時間欄位上
預設 Aspose.Words 會更新 TIME
欄位,使用當前文化短時間格式。 我們發現 Microsoft Word 格式與 .NET/Windows 格式之間有差異,而不同版本的 .NET Framework 也有差異。 如果你想要按照你的要求來格式化 TIME
欄位,你可以透過實作 IFieldUpdateCultureProvider 介面來達成。
下列程式碼範例示範如何將自訂格式套用至 TIME
欄位:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertField(FieldType.FieldTime, true); | |
doc.FieldOptions.FieldUpdateCultureSource = FieldUpdateCultureSource.FieldCode; | |
doc.FieldOptions.FieldUpdateCultureProvider = new FieldUpdateCultureProvider(); | |
doc.Save(ArtifactsDir + "WorkingWithFields.FieldUpdateCulture.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
class FieldUpdateCultureProvider : IFieldUpdateCultureProvider | |
{ | |
public CultureInfo GetCulture(string name, Field field) | |
{ | |
switch (name) | |
{ | |
case "ru-RU": | |
CultureInfo culture = new CultureInfo(name, false); | |
DateTimeFormatInfo format = culture.DateTimeFormat; | |
format.MonthNames = new[] | |
{ | |
"месяц 1", "месяц 2", "месяц 3", "месяц 4", "месяц 5", "месяц 6", "месяц 7", "месяц 8", | |
"месяц 9", "месяц 10", "месяц 11", "месяц 12", "" | |
}; | |
format.MonthGenitiveNames = format.MonthNames; | |
format.AbbreviatedMonthNames = new[] | |
{ | |
"мес 1", "мес 2", "мес 3", "мес 4", "мес 5", "мес 6", "мес 7", "мес 8", "мес 9", "мес 10", | |
"мес 11", "мес 12", "" | |
}; | |
format.AbbreviatedMonthGenitiveNames = format.AbbreviatedMonthNames; | |
format.DayNames = new[] | |
{ | |
"день недели 7", "день недели 1", "день недели 2", "день недели 3", "день недели 4", | |
"день недели 5", "день недели 6" | |
}; | |
format.AbbreviatedDayNames = new[] | |
{ "день 7", "день 1", "день 2", "день 3", "день 4", "день 5", "день 6" }; | |
format.ShortestDayNames = new[] { "д7", "д1", "д2", "д3", "д4", "д5", "д6" }; | |
format.AMDesignator = "До полудня"; | |
format.PMDesignator = "После полудня"; | |
const string pattern = "yyyy MM (MMMM) dd (dddd) hh:mm:ss tt"; | |
format.LongDatePattern = pattern; | |
format.LongTimePattern = pattern; | |
format.ShortDatePattern = pattern; | |
format.ShortTimePattern = pattern; | |
return culture; | |
case "en-US": | |
return new CultureInfo(name, false); | |
default: | |
return null; | |
} | |
} | |
} |