名称';c';在使用OpenXml sdk和C#访问Word文件的当前上下文中不存在
上传到服务器后,我使用c#和OpenXMLSDK2.0访问Word文件 我想根据给定的文本检索章节和段落 章节插入表格名称';c';在使用OpenXml sdk和C#访问Word文件的当前上下文中不存在,c#,ms-word,openxml-sdk,C#,Ms Word,Openxml Sdk,上传到服务器后,我使用c#和OpenXMLSDK2.0访问Word文件 我想根据给定的文本检索章节和段落 章节插入表格章节 foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter"))) 章节和段落插入表格章节副标题 使用S
章节
foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))
章节和段落插入表格章节副标题
使用System.Linq
我可以找到章节包含章节
foreach (Paragraph c in wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(c => c.InnerText.Contains("Chapter")))
但是我在获取VisualStudio调试时出现了这个阻塞错误
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;
}
}
}
protectedvoiduploadbutton\u单击(对象发送方,事件参数e)
{
if(FileUploadControl.HasFile&&FileUploadControl.PostedFile.ContentLength>0)
{
尝试
{
var allowedExtensions=新字符串[]{“docx”};
var extension=Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(“.”,”);
if(allowedExtensions.Contains(扩展))
{
字符串filename=Path.GetFileName(FileUploadControl.filename);
FileUploadControl.SaveAs(Server.MapPath(“public/”)+文件名);
StatusLabel.Text=“上载状态:文件已上载!”;
file=Server.MapPath(“public/”)+文件名;
使用(WordProcessingDocumentWordDoc=
WordprocessingDocument.Open(文件,true))
{
body=wordDoc.MainDocumentPart.Document.body;
第“”段;
foreach(附件c段)
wordDoc.MainDocumentPart.Document.Body.Subjections()。其中(c=>c.InnerText.Contains(“章节”))
{
第+=c.InnerText+“
”;
strSql=@“将忽略插入章节(章节)值(?);”;
使用(MySqlConnection-conn)=
新的MySqlConnection(ConfigurationManager.ConnectionString[“cn”].ConnectionString))
{
conn.Open();
使用(mysqlcmd)命令=
新的MySqlCommand(strSql,conn))
{
cmd.Parameters.AddWithValue(“param1”,c.InnerText);
cmd.ExecuteNonQuery();
}
康涅狄格州关闭();
}
}
foreach(见附录p段)
wordDoc.MainDocumentPart.Document.Body.subjections()。其中(p=>p.InnerText.StartsWith(“-”))
{
foreach(在p.substands()中运行r)
{
RunProperties rProp=r.RunProperties;
如果(rProp.Bold!=null)
{
段落+=p.InnerText+“
”;
strSql=@“在子目(子目,章节)中插入忽略值(?,);”;
使用(MySqlConnection-conn)=
新的MySqlConnection(ConfigurationManager.ConnectionString[“cn”].ConnectionString))
{
conn.Open();
使用(mysqlcmd)命令=
新的MySqlCommand(strSql,conn))
{
cmd.Parameters.AddWithValue(“param1”,p.InnerText);
cmd.Parameters.AddWithValue(“param2”,c.InnerText);
cmd.ExecuteNonQuery();
}
康涅狄格州关闭();
}
}
}
}
}
}
其他的
{
StatusLabel.Text=“上载状态:仅接受DOCX文件!”;
}
}
捕获(例外情况除外)
{
StatusLabel.Text=“上载状态:无法上载文件。出现以下错误:“+ex.Message;
}
}
}
编辑#1
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>(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, conn))
{
cmd.Parameters.AddWithValue("param1", 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, 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();
}
}
}
}
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;
}
}
}
protectedvoiduploadbutton\u单击(对象发送方,事件参数e)
{
if(FileUploadControl.HasFile&&FileUploadControl.PostedFile.ContentLength>0)
{
尝试
{
var allowedExtensions=新字符串[]{“docx”};
var extension=Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(“.”,”);
if(allowedExtensions.Contains(扩展))
{
字符串filename=Path.GetFileName(FileUploadControl.filename);
FileUploadControl.SaveAs(Server.MapPath(“public/”)+文件名);
StatusLabel.Text=“上载状态:文件已上载!”;
file=Server.MapPath(“public/”)+文件名;
使用(WordProcessingDocumentWordDoc=
WordprocessingDocument.Open(文件,true))
{
body=wordDoc.MainDocumentPart.Document.body;
第“”段;
foreach(附件c段)
wordDoc.main documentpart.Document.Body.subjections().Where(somethingElse=>somethingElse.InnerText.Contains(“Sezione”))
{
第+=c.InnerText+“
”;
strSql=@“将忽略插入章节(章节)值(?);”;
使用(MySqlConnection-conn)=
新的MySqlConnection(ConfigurationManager.ConnectionString[“cn”].ConnectionString))
{
conn.Open();
使用(
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(file, true))
{
body = wordDoc.MainDocumentPart.Document.Body;
contents = "";
var reg = new Regex(@"^[...customize...]");
foreach (Paragraph co in
wordDoc.MainDocumentPart.Document.Body.Descendants<Paragraph>().Where<Paragraph>(somethingElse => reg.IsMatch(somethingElse.InnerText)))
{
subheading += co.InnerText + "<br />";
strSql = @"INSERT IGNORE INTO subheading (subheading) VALUES (?);";
using (MySqlConnection conn =
new MySqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
conn.Open();
using (MySqlCommand cmd =
new MySqlCommand(strSql, conn))
{
cmd.Parameters.AddWithValue("param1", co.InnerText);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}