首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ASP.NET从word文档中获取文本框值?

使用ASP.NET从word文档中获取文本框值?
EN

Stack Overflow用户
提问于 2013-08-14 21:08:45
回答 2查看 3.7K关注 0票数 2

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

在我的web应用程序代码隐藏文件中,我想按名称调用textbox控件和dropdown控件,并从中提取值。

我在网上找到的任何文档都只是简单地读写word文档,但我似乎找不到任何关于访问控件和从控件中提取值的内容。

如有任何帮助,我将不胜感激

这是我目前唯一能处理word文档的代码。它会找到单词doc并打开它:

代码语言:javascript
复制
    //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);
EN

回答 2

Stack Overflow用户

发布于 2013-08-14 22:32:46

除非您打算在服务器上安装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获取文本的一些代码:

代码语言:javascript
复制
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();
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2017-10-27 21:32:26

代码语言:javascript
复制
public List<string> GetTagsFromNewTemplate(string filePath)
        {
            var tags = new HashSet<string>();

            using (WordprocessingDocument myDocument = WordprocessingDocument.Open(filePath, false))
            {
                var textbox = myDocument.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Tag>().Select(x => x.Val);
                textbox.ForEach(x => tags.Add(x));
            }
            return tags.Distinct().ToList();
        } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18232633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档