手动区分
文档->右键->属性
代码实现
namespace WpsAndOfficeDifferent
{
internal class Program
{
static void Main(string[] args)
{
string root = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase ?? "";
#region 区分office和wps创建的doc和docx文件
//Console.WriteLine(GetWordAppName(Path.Combine(root,"Word","office.doc")));
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "wps.doc")));
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "office.docx")));
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "wps.docx")));
//Console.WriteLine("使用wps修改office word");
office创建的doc,wps修改也不会改变,docx会改变
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "office2wps.doc")));
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "office2wps.docx")));
//Console.WriteLine("使用office修改wps word");
wps创建的doc,office修改也不会改变,docx会改变
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "wps2office.doc")));
//Console.WriteLine(GetWordAppName(Path.Combine(root, "Word", "wps2office.docx")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "office.doc")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "wps.doc")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "office.docx")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "wps.docx")));
Console.WriteLine("使用wps修改office word");
//office创建的doc,wps修改也不会改变,docx会改变
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "office2wps.doc")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "office2wps.docx")));
Console.WriteLine("使用office修改wps word");
//wps创建的doc,office修改也不会改变,docx会改变
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "wps2office.doc")));
Console.WriteLine(GetWordAppNameByAspose(Path.Combine(root, "Word", "wps2office.docx")));
#endregion
#region 区分office和wps创建的xls和xlsx
//Console.WriteLine(GetExcelAppName(Path.Combine(root,"Excel", "office.xls")));
//Console.WriteLine(GetExcelAppName(Path.Combine(root, "Excel", "wps.xls")));
//Console.WriteLine(GetExcelAppName(Path.Combine(root, "Excel", "office.xlsx")));
//spire.xls不支持读取wps创建的xlsx
//Console.WriteLine(GetExcelAppName(Path.Combine(root, "Excel", "wps.xlsx")));
//xls能区分谁创建的,xlsx wps创建会多一些自定义属性,
//但是wps修改office也会多wps自定义属性,ApplicationName一样
//wps和office的xlsx格式兼容区分不出来影响不大
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "office.xls")));
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "wps.xls")));
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "office.xlsx")));
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "wps.xlsx")));
Console.WriteLine("使用wps修改office excel");
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "office2wps.xls")));
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "office2wps.xlsx")));
Console.WriteLine("使用office修改wps excel");
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "wps2office.xls")));
Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "Excel", "wps2office.xlsx")));
#endregion
#region 区分office和wps创建的ppt和pptx
//ppt能区分谁创建的,修改了不会改变
//pptx能区分谁创建的,但是谁修改了就变成对应的那个应用程序
//Console.WriteLine(GetPptAppName(Path.Combine(root, "PPT", "office.ppt")));
//Console.WriteLine(GetPptAppName(Path.Combine(root, "PPT", "wps.ppt")));
//Console.WriteLine(GetPptAppName(Path.Combine(root, "PPT", "office.pptx")));
//Console.WriteLine(GetPptAppName(Path.Combine(root, "PPT", "wps.pptx")));
//Console.WriteLine("使用wps修改office ppt");
//Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "PPT", "office2wps.ppt")));
//Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "PPT", "office2wps.pptx")));
//Console.WriteLine("使用office修改wps ppt");
//Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "PPT", "wps2office.ppt")));
//Console.WriteLine(GetExcelAppNameByAspose(Path.Combine(root, "PPT", "wps2office.pptx")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "office.ppt")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "wps.ppt")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "office.pptx")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "wps.pptx")));
Console.WriteLine("使用wps修改office ppt");
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "office2wps.ppt")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "office2wps.pptx")));
Console.WriteLine("使用office修改wps ppt");
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "wps2office.ppt")));
Console.WriteLine(GetPptAppNameByAspose(Path.Combine(root, "PPT", "wps2office.pptx")));
#endregion
Console.Read();
}
/// <summary>
/// 区分wrod
/// </summary>
/// <param name="docPath"></param>
static string GetWordAppName(string docPath)
{
//创建一个Document实例
using var doc = new Spire.Doc.Document();
//加载一个现有Word文档
doc.LoadFromFile(docPath);
var appName = doc.BuiltinDocumentProperties.ApplicationName;
return appName;
//获取摘要
//Console.WriteLine("摘要\n");
//Console.WriteLine("标题: " + doc.BuiltinDocumentProperties.Title);
//Console.WriteLine("主题: " + doc.BuiltinDocumentProperties.Subject);
//Console.WriteLine("作者: " + doc.BuiltinDocumentProperties.Author);
//Console.WriteLine("主管: " + doc.BuiltinDocumentProperties.Manager);
//Console.WriteLine("单位: " + doc.BuiltinDocumentProperties.Company);
//Console.WriteLine("类别: " + doc.BuiltinDocumentProperties.Category);
//Console.WriteLine("关键字: " + doc.BuiltinDocumentProperties.Keywords);
//Console.WriteLine("备注: " + doc.BuiltinDocumentProperties.Comments);
//Console.WriteLine("修订号: " + doc.BuiltinDocumentProperties.RevisionNumber);
//Console.WriteLine("程序名称:" + doc.BuiltinDocumentProperties.ApplicationName);
//获取自定义属性
//Console.WriteLine("\n自定义属性\n");
//for (int i = 0; i < doc.CustomDocumentProperties.Count; i++)
//{
// Console.WriteLine(doc.CustomDocumentProperties[i].Name + ": " + doc.CustomDocumentProperties[i].Value);
//}
}
/// <summary>
/// 区分wrod
/// </summary>
/// <param name="docPath"></param>
static string GetWordAppNameByAspose(string docPath)
{
//创建一个Document实例
var doc = new Aspose.Words.Document(docPath);
var appName = doc.BuiltInDocumentProperties.NameOfApplication;
return appName;
}
/// <summary>
/// 区分excel
/// </summary>
/// <param name="excelPath"></param>
/// <returns></returns>
static string GetExcelAppName(string excelPath)
{
//创建一个Document实例
using var workbook = new Spire.Xls.Workbook();
//加载一个现有Word文档
workbook.LoadFromFile(excelPath);
var appName = workbook.DocumentProperties.ApplicationName;
return appName;
}
/// <summary>
/// 区分excel
/// </summary>
/// <param name="excelPath"></param>
/// <returns></returns>
static string GetExcelAppNameByAspose(string excelPath)
{
using var workbook = new Aspose.Cells.Workbook(excelPath);
var appName = workbook.BuiltInDocumentProperties.NameOfApplication;
//foreach (var item in workbook.CustomDocumentProperties)
//{
// Console.WriteLine(item.Name);
// Console.WriteLine(item.Value);
//}
var isWpsCreate = workbook.CustomDocumentProperties.Any(p => p.Name.Equals("KSOProductBuildVer"));
if (isWpsCreate)
{
Console.WriteLine("wps创建");
}
return appName;
}
/// <summary>
/// 区分wrod
/// </summary>
/// <param name="pptPath"></param>
static string GetPptAppName(string pptPath)
{
//创建一个PPT实例
using var ppt = new Spire.Presentation.Presentation();
//加载一个现有PPT文档
ppt.LoadFromFile(pptPath);
var appName = ppt.DocumentProperty.Application;
return appName;
}
/// <summary>
/// 区分wrod
/// </summary>
/// <param name="pptPath"></param>
static string GetPptAppNameByAspose(string pptPath)
{
//创建一个Document实例
var ppt = new Aspose.Slides.Presentation(pptPath);
var appName = ppt.DocumentProperties.NameOfApplication;
return appName;
}
}
}
结论
总结,如果是之查看不修改,则不会改变创建的应用
doc、xls、ppt这种以前的格式,只会保留创建的应用,即使其他的程序修改了也不会改变
docx、xlsx和pptx这种如果有新的软件修改则记录新的修改软件
wps xlsx和office的格式一致,通过applicationname没法区分,
wps创建或者修改过就会多一个属性KSOProductBuildVer,即使office再次改变还是会有