protected void createQuestion(object sender, EventArgs e) { ILog logger = LogManager.GetLogger(typeof(Question)); using (FileStream stream = File.OpenRead("d://000/hhyq.docx")) { XWPFDocument docx = new XWPFDocument(stream); string[] strs = new string[7]; Regex regA = new Regex("^[ABCDabcd]{1}.|、", RegexOptions.IgnoreCase); //以A|B|C|D开头 选项 Regex regNO = new Regex("^[0-9]+.|、", RegexOptions.IgnoreCase); //以数字开头 题干 Regex regChapter = new Regex("^第[一二三四五六七八九十]{1,3}章", RegexOptions.IgnoreCase); //章节标题 Regex regnode = new Regex("^第[一二三四五六七八九十]{1,3}节", RegexOptions.IgnoreCase); Chapter chapter = null, node = null; ChapterManager chaptermanager = new ChapterManager(); Question question = null; QuestionManager questonmanager = new QuestionManager(); foreach (var para in docx.Paragraphs) { string text = para.ParagraphText.Trim(); if (!string.IsNullOrWhiteSpace(text) && !string.IsNullOrEmpty(text))//非空 { if (regChapter.IsMatch(text)) { chapter = new Chapter(); chapter.TextBookId = 1; chapter.IsVerified = true; chapter.ChapterDeep = 0; chapter.ChapterName = regChapter.Replace(text, "").Trim(); chapter.ChapterParentNo = 0; chapter.ChapterRemark = chapter.ChapterName; chaptermanager = new ChapterManager(); chapter.ChapterId = chaptermanager.Add(chapter); } else if (regnode.IsMatch(text)) { node = new Chapter(); node.ChapterName = regnode.Replace(text, "").Trim(); node.ChapterParentNo = chapter.ChapterId; node.ChapterDeep = 1; node.ChapterRemark = node.ChapterName; node.TextBookId = 1; node.IsVerified = true; node.ChapterId = chaptermanager.Add(node); } else { string[] paratext = text.Split(new string[] { "A.", "B.", "C.", "D." }, StringSplitOptions.RemoveEmptyEntries); question = StringsTOQuestion(paratext, "D"); question.QuestionId = questonmanager.Add(question); Console.WriteLine(question.QuestionId + " 添加成功。"); } } System.Threading.Thread.Sleep(300); } } }
public async Task <bool> AddChapter(Chapter chapter, byte[]?content) { await using var transaction = await _context.Database.BeginTransactionAsync(); if (!await _chapterManager.Add(chapter)) { await transaction.RollbackAsync(); return(false); } await ProcessContent(chapter, content); await transaction.CommitAsync(); return(true); }