c# - 使用 ASP.NET 从 Word 文档获取文本框值?

标签 c# asp.net .net ms-word

我有一个用 ASP.NET(C#) 编写的非常基本的 Web 应用程序和一个包含文本框和下拉列表的基本 Microsoft Word (2007) 文档。

在我的 Web 应用程序代码隐藏文件中,我想按名称调用文本框控件和下拉控件并从中提取值。

我在网上找到的任何文档都只是读取或写入一个Word文档,但我似乎找不到任何有关访问控件并从中提取值的内容。

任何帮助将不胜感激 谢谢您

这是我目前唯一可以对 word 文档执行任何操作的代码。它找到单词 doc 并打开它:

    //File path of the word document that contains the required values 
    string filePath = @"C:\Users\murphycm\Desktop\PlacesFile.docm";

    object fileToOpen = (object)filePath;

    //CREATING OBJECTS OF WORD AND DOCUMENT
    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

    oWordDoc = oWord.Documents.Open(ref fileToOpen);

最佳答案

除非您要在服务器上安装 Microsoft Office,否则我建议您使用 Microsoft 的 Open XML SDK 2.5。使用 SDK,您可以操作 Office 2007 及更高版本的 Microsoft Office 文档:
http://www.microsoft.com/en-us/download/details.aspx?id=30425

以下是使用 OpenXML 和 Office Interop 方法从 TextBox 获取文本的一些代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Word = Microsoft.Office.Interop.Word;

namespace OpenXMLSDKTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open XML Method
            object fileName = @"OpenXmlTest.docx";
            using (WordprocessingDocument myDocument = WordprocessingDocument.Open(fileName.ToString(), true))
            {
                var textbox = myDocument.MainDocumentPart.Document.Descendants<TextBoxContent>().First();
                Console.WriteLine(textbox.InnerText);
            }

            // Office Interop Method
            object missing = System.Reflection.Missing.Value;
            object readOnly = false;
            object isVisible = true;

            Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref  missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            object firstShape = 1;
            string textFrameText = wordApp.ActiveDocument.Shapes.get_Item(ref firstShape).TextFrame.TextRange.Text;
            wordApp.Quit(ref missing, ref missing, ref missing);

            Console.WriteLine(textFrameText);

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }
}

关于c# - 使用 ASP.NET 从 Word 文档获取文本框值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232633/

相关文章:

c# - 当两个实体可以独立存在时, Entity Framework 核心一对零或一

asp.net - 如何获取css文件的完全限定路径?

asp.net - Webkit 中的 AutoCompleteExtender 位置错误

javascript - 为什么我不能使用 jquery 显示/隐藏 Html TR?

.net - 禁用 "application has stopped working"窗口

c# - 如何指定以应用程序域中立的方式加载程序集

c# - 操作系统必须支持异步方法或者是异步程序级功能

c# - 如何实现多索引字典?

c# - List.Contains 上的索引越界

c++ - OpenCV:System.Runtime.InteropServices.SEHException