微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用OpenXml-sdk和C#来访问Word文件的当前上下文中不存在名称“ c”

如何解决使用OpenXml-sdk和C#来访问Word文件的当前上下文中不存在名称“ c”

我正在使用c#并在服务器上上传后打开xml sdk 2.0来访问Word文件

我想根据给定的文本检索章节和段落。

这些章插入在表chapter

各章和各段插入在表章subheading

使用System.Linq可以找到章节Contains Chapter

foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))

和副标题Starts With "- "

foreach (Paragraph p in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.StartsWith("- ")))

但是在进行Visual Studio调试时出现此阻止错误

CS0103: The name 'c' does not exist in the current context

请帮助我

我的下面的代码

protected void UploadButton_Click(object sender,EventArgs e)
{
    if (FileUploadControl.HasFile && FileUploadControl.PostedFile.ContentLength > 0)
    {
        try
        {
            var allowedExtensions = new string[] { "docx" };
            var extension = Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(".","");

            if (allowedExtensions.Contains(extension))
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);

                FileUploadControl.SaveAs(Server.MapPath("public/") + filename);
                StatusLabel.Text = "Upload status: File uploaded!";

                file = Server.MapPath("public/") + filename;

                using (WordprocessingDocument wordDoc =
                    WordprocessingDocument.Open(file,true))
                {
                    body = wordDoc.MainDocumentPart.Document.Body;
                    paras = "";

                    foreach (Paragraph c in
                        wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))
                    {
                        paras += c.InnerText + "<br/>";

                        strsql = @"INSERT IGnorE INTO Chapters (chapter) VALUES (?);";

                        using (MysqLConnection conn =
                            new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                        {
                            conn.open();
                            using (MysqLCommand cmd =
                                new MysqLCommand(strsql,conn))
                            {
                                cmd.Parameters.AddWithValue("param1",c.InnerText);
                                cmd.ExecuteNonQuery();
                            }
                            conn.Close();
                        }
                    }


                    foreach (Paragraph p in
                            wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(p => p.InnerText.StartsWith("- ")))
                    {
                        foreach (Run r in p.Descendants<Run>())
                        {
                            RunProperties rProp = r.RunProperties;

                            if (rProp.Bold != null)
                            {
                                paras += p.InnerText + "<br/>";

                                strsql = @"INSERT IGnorE INTO subheading (subheading,chapter) VALUES (?,?);";

                                using (MysqLConnection conn =
                                    new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                                {
                                    conn.open();
                                    using (MysqLCommand cmd =
                                        new MysqLCommand(strsql,conn))
                                    {
                                        cmd.Parameters.AddWithValue("param1",p.InnerText);
                                        cmd.Parameters.AddWithValue("param2",c.InnerText);
                                        cmd.ExecuteNonQuery();
                                    }
                                    conn.Close();
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
            }
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file Could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

修改#1

protected void UploadButton_Click(object sender,true))
                {
                    body = wordDoc.MainDocumentPart.Document.Body;
                    paras = "";

                    foreach (Paragraph c in
                        wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(somethingElse => somethingElse.InnerText.Contains("Sezione")))
                    {
                        paras += c.InnerText + "<br/>";

                        strsql = @"INSERT IGnorE INTO Chapters (chapter) VALUES (?);";

                        using (MysqLConnection conn =
                            new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                        {
                            conn.open();
                            using (MysqLCommand cmd =
                                new MysqLCommand(strsql,c.InnerText);
                                cmd.ExecuteNonQuery();
                            }
                            conn.Close();
                        }



                        foreach (Paragraph p in
                                wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(somethingElse => somethingElse.InnerText.StartsWith("- ")))
                        {
                            foreach (Run r in p.Descendants<Run>())
                            {
                                RunProperties rProp = r.RunProperties;

                                if (rProp.Bold != null)
                                {
                                    paras += p.InnerText + "<br/>";

                                    strsql = @"INSERT IGnorE INTO subheading (subheading,?);"; //

                                    using (MysqLConnection conn =
                                        new MysqLConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
                                    {
                                        conn.open();
                                        using (MysqLCommand cmd =
                                            new MysqLCommand(strsql,conn))
                                        {
                                            cmd.Parameters.AddWithValue("param1",p.InnerText);
                                            cmd.Parameters.AddWithValue("param2",c.InnerText);
                                            cmd.ExecuteNonQuery();
                                        }
                                        conn.Close();
                                    }
                                }
                            }
                        }
                        Response.Write(paras);
                    }
                    Response.Write(paras);
                }
            }
            else
            {
                StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
            }
        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file Could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

解决方法

尝试

base::aggregate()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。