首页 \ 问答 \ 如何使用Apache POI从MS word文档的文本框中获取文本?(How to get text from textbox of MS word document using Apache POI?)

如何使用Apache POI从MS word文档的文本框中获取文本?(How to get text from textbox of MS word document using Apache POI?)

我想在MS Word文档中获取在Textbox中编写的信息。 我正在使用Apache POI来解析word文档。

目前我正在迭代所有Paragraph对象,但是这个Paragraph列表不包含来自TextBox的信息,所以我在输出中缺少这个信息。

例如

paragraph in plain text

**<some information in text box>**

one more paragraph in plain text

我要提取的内容:

<para>paragraph in plain text</para>

<text_box>some information in text box</text_box>

<para>one more paragraph in plain text</para>

我目前得到的是什么:

段落的纯文本

还有一段纯文字

任何人都知道如何使用Apache POI从文本框中提取信息?


I want to get information written in Textbox in an MS word document. I am using Apache POI to parse word document.

Currently I am iterating through all the Paragraph objects but this Paragraph list does not contain information from TextBox so I am missing this information in output.

e.g.

paragraph in plain text

**<some information in text box>**

one more paragraph in plain text

what i want to extract :

<para>paragraph in plain text</para>

<text_box>some information in text box</text_box>

<para>one more paragraph in plain text</para>

what I am getting currently :

paragraph in plain text

one more paragraph in plain text

Anyone knows how to extract information from text box using Apache POI?


原文:https://stackoverflow.com/questions/5457771
更新时间:2022-01-19 10:01

最满意答案

第一个问题:我需要声明存储在MyType中的类型的新变量。

基本上,你不能这样做。

编译时需要知道变量类型。 您不能使用仅在执行时知道的类型声明变量。 您可以通过泛型获得一些方法:

public class Foo<T>
{
    private T value;
}

你甚至可以添加一个约束来要求T有一个无参数的构造函数,然后你可以调用它:

public class Foo<T> : new()
{
    private T value = new T();
}

...并且您可以在使用反射仅知道执行时的T类型时创建Foo<T>

Type genericDefinition = typeof(Foo<>);
Type constructed = genericDefinition.MakeGenericType(A.MyType);
object instance = Activator.CreateInstance(constructed);

...但它会很快变得凌乱。 你没有告诉我们你想要实现什么,所以很难提出另一种选择 - 但如果你可以重新设计以避免这种情况,我会这样做。

第二个问题:我需要一个函数来表明是否可以将'Type'

您可能正在寻找Type.IsAssignableFrom ,但目前还不清楚......


First problem: I need declare new variable with type stored in MyType.

You can't do this, basically.

Variable types need to be known at compile-time. You can't declare a variable using a type which is only known at execution time. You can get some of the way there using generics:

public class Foo<T>
{
    private T value;
}

You can even add a constraint to require that T has a parameterless constructor which you can then call:

public class Foo<T> : new()
{
    private T value = new T();
}

... and you could create a Foo<T> when you only know the type of T at execution time using reflection:

Type genericDefinition = typeof(Foo<>);
Type constructed = genericDefinition.MakeGenericType(A.MyType);
object instance = Activator.CreateInstance(constructed);

... but it will get messy pretty quickly. You haven't told us anything about what you're actually trying to achieve, so it's hard to suggest an alternative - but if you can possibly redesign to avoid this, I would.

Second problem: I need a function that indicates whether reducible to each other variables of the 'Type'

It's possible that you're looking for Type.IsAssignableFrom, but it's not clear...

相关问答

更多
  • 确定你可以在这里是一个简单的(假设这是一个T型演员)演员,如果方便(假设我们可以将其转换为T)转换: public T CastExamp1(object input) { return (T) input; } public T ConvertExamp1(object input) { return (T) Convert.ChangeType(input, typeof(T)); } 编辑: 有些人在评论中说这个答案没有回答这个问题。 但是线(T) Conv ...
  • 如果你这样做: index.php?url=asd&url[]=asd //asd&url[]=asd being the dynamic part 然后$_GET['url']将是一个数组。 If you do this: index.php?url=asd&url[]=asd //asd&url[]=asd being the dynamic part Then $_GET['url'] will be an array.
  • 第一个问题:我需要声明存储在MyType中的类型的新变量。 基本上,你不能这样做。 编译时需要知道变量类型。 您不能使用仅在执行时知道的类型声明变量。 您可以通过泛型获得一些方法: public class Foo { private T value; } 你甚至可以添加一个约束来要求T有一个无参数的构造函数,然后你可以调用它: public class Foo : new() { private T value = new T(); } ...并且您可以在使用反射仅知道执行 ...
  • 使用fn = S {x=n} ,类型变量是不必要的 Use f n = S {x=n}, the type variable is unnecessary
  • 两者完全一样,对于任何类型都是如此。 就个人而言,我会避免第一种形式, (T)x ,赞成明确的静态演员: y = static_cast(x); 这表示您要将x 转换为类型T 第二种形式更能唤起构造函数的调用,这有时更可取: v = std::vector(10); // not: v = static_cast>(10) 重复一遍,两种形式完全相同,而且这是你喜欢的味道问题。 如果这是有道理的,我会使用静态转换来“转换”和构造器语法来“构建”。 Th ...
  • 问题是具体的:“一个变量名称的声明声明...”(强调我的) 如果它说“一个变量名称声明...”,你可能会认为这是一个诡计问题。 但是,由于这个问题专门针对宣言本身,所以正确答案是正确的 。 The question is specific: "A declaration of a variable name declares..." (emphasis mine) If it said "A variable name declares..." you may be correct in assuming ...
  • 有几种方法可以解决这个问题,但最简单的方法是使用Activator类。 http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx 例: Type t = someClassInstance.GetType(); object o = Activator.CreateInstance(t); There are a few ways to go about this, but the simplest would ...
  • 在您的示例中, size是一个类型为FreshJuiceSize的成员变量( enum约束为您定义的3个值)。 也许让它看起来不寻常的是缺少一个访问修饰符: public , protected或private 。 对于没有声明访问修饰符的成员,它是“package-private”,这意味着同一个包中的其他类可以访问它,但包外的类可能不会。 有关控制对类成员的访问的Java文档描述了这一点。 In your example, size is a member variable with type Fres ...
  • InRange: Boolean; ManagerType: TManagerType; ... InRange := ManagerType in [Low(TManagerType)..High(TManagerType)]; 正如Nickolay O.所说 - 虽然上面的布尔表达式直接对应于: (Low(TManagerType) <= ManagerType) and (ManagerType <= High(TManagerType)) 编译器不会根据单个子范围执行针对立即设置检查成员资格的优 ...
  • 这是团队必须做出的风格选择。 是的,它是重复的,因为它可以由编译器推断出来; 然而,编写它们可以更容易让开发人员知道而无需点击许多函数(因为someFunc可以从其他东西推断它的类型)。 // The compiler knows that a is a number, developers will have to look // inside someFunc and otherFunc to know that const a = someFunc(); function someFunc() { ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)