我正在尝试使用VSTO将具有右向左方向的波斯日期而不是文本[日期]放入单词文件中。但是当替换代码运行时,在word文件中复制的文本具有从左到右的方向。
我这样做的代码是这样的:
Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = AssemblyDirectory + "\\ReceiptTemplate.docx";
try
{
wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
var sel = wordApp.Selection;
sel.Find.Text = "[Date]";
sel.Find.Replacement.Text = PersianCharacterConverter.ToPersianNumber(chequeInfo.ChequeDate);
sel.Find.Wrap = WdFindWrap.wdFindContinue;
sel.Find.Forward = true;
sel.Find.Format = false;
sel.Find.MatchCase = false;
sel.Find.MatchWholeWord = false;
sel.Find.Execute(Replace: WdReplace.wdReplaceAll);
wordDoc.PrintOut();
//wordDoc.ExportAsFixedFormat(chequeInfo.Path+"\\" + chequeInfo.ChequeNumber + ".pdf", WdExportFormat.wdExportFormatPDF);
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
wordDoc.Close(false);
if (wordDoc != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
wordDoc = null;
}
catch (Exception ex)
{
var my = ex.Message;
throw;
}
但是当它在Word中被替换时,日期变为:
۰۲/۰۲/۱۳۹۵
虽然我需要这个:
1395年2月2日
这也是我的PersianCharacterConverter:
public static class PersianCharacterConverter
{
private static readonly string[] pn = { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
private static readonly string[] en = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public static string ToPersianNumber(string strNum)
{
string chash = strNum;
for (int i = 0; i < 10; i++)
chash = chash.Replace(en[i], pn[i]);
return chash;
}
public static string ToPersianNumber(int intNum)
{
string chash = intNum.ToString();
for (int i = 0; i < 10; i++)
chash = chash.Replace(en[i], pn[i]);
return chash;
}
}
答案 0 :(得分:1)
您要解析的值位于不同的datetime
,您需要解析日期并将其转换为Persian Calendar
,然后将其转换为ToPersianNumber
。
这是我通过搜索找到的波斯日历方法:
string GregorianDate = "Thursday, October 24, 2013";
DateTime d = DateTime.Parse(GregorianDate);
PersianCalendar pc = new PersianCalendar();
Console.WriteLine(string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)));
答案 1 :(得分:1)
最后我发现了一个解决问题的技巧:
private string ConvertToPersianDateFormat(string date)
{
String[] items = date.Split('/');
string temp = "";
temp = items[0];
items[0] = items[2];
items[2] = temp;
return items[0]+"/"+items[1]+"/"+items[2];
}
我打电话时使用此方法:
PersianCharacterConverter.ToPersianNumber(chequeInfo.ChequeDate);
我用ConvertToPersianDateFormat方法包装它:
ConvertToPersianDateFormat(PersianCharacterConverter.ToPersianNumber(chequeInfo.ChequeDate));
所以它改变了一年一天的地方