【发布时间】:2012-06-11 09:11:40
【问题描述】:
我是一名 Java 开发人员。但是,出于某种原因,我必须借助 C# 来完成我的任务。我在下面提到了用于创建 DLL 的 C# 代码。必须在我的 Java 程序中使用该 DLL 才能完成必要的工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace Yrl.Tracingtool
{
public class DocxUtil
{
public Application Jump(string fileName)
{
object fileNameAsObject = (object)fileName;
Application wordApplication;
try
{
wordApplication = new Application();
object readnly = false;
object missing = System.Reflection.Missing.Value;
wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count = 3;
wordApplication.Selection.GoTo(ref what, ref which, ref count, ref missing);
return wordApplication;
}
catch (Exception ex)
{
//LogEntry log = new LogEntry();
//log.Categories.Add("Trace");
//log.Message = ex.ToString();
//Logger.Write(log, "Trace");
throw new System.IO.FileLoadException("File cannot be opened");
}
finally
{
wordApplication = null;
}
}
}
}
我也查看过这个论坛和其他论坛,但大多数都谈到在 JNI 调用中使用 C++ 或 C DLL 文件。如果有人知道从 Java 调用 C# DLL,请告诉我。
【问题讨论】:
-
我认为你必须使用 COM?可能是错的?
-
@duffymo 我不这么认为。那篇文章没有关于 C# DLL 的任何信息。
-
在 Apache 上有一个用于 Microsoft Documents 项目的 Java API,Apache POI。这可以替代混合 Java 和 C# 吗? (当然,我自己没有使用 Apache POI 的经验。)
-
@AndersGustafsson 我已经完成了使用 Apache POI 的可行性研究。但是,这不太符合我的要求。
-
@Raj 有一些 Java/COM 互操作库可用,例如 JACOB 和 j-interop。更多信息在this SO answer。这有什么帮助吗?
标签: c# java dll java-native-interface