Commit 3fcff474 authored by 黄奎's avatar 黄奎

新增模板导入功能

parent 7f3b834c
This diff is collapsed.
......@@ -5,13 +5,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FreeSpire.Doc" Version="7.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NPOI" Version="2.5.1" />
<PackageReference Include="Spire.Doc" Version="8.12.14" />
</ItemGroup>
</Project>
using Edu.Common.Plugin;
using System;
using System.Collections.Generic;
using System.Text;
namespace Edu.Common.Enum.Course
{
/// <summary>
/// 问题大类型
/// </summary>
public enum QuestionCategoryEnum
{
/// <summary>
/// 默认
/// </summary>
[EnumField("默认")]
Default = 0,
/// <summary>
/// 日语选择题
/// </summary>
[EnumField("日语选择题")]
JapaneseChoose = 1,
/// <summary>
/// 中文选择题
/// </summary>
[EnumField("中文选择题")]
ChineseChoose = 2,
/// <summary>
/// 阅读理解
/// </summary>
[EnumField("阅读理解")]
ReadingComprehensio = 3,
}
}
\ No newline at end of file
......@@ -231,5 +231,65 @@ namespace Edu.Common
return Convert.ToBase64String(imageBytes);
}
}
/// <summary>
/// 字符串转数字
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
public static int ConvertStringToNumber(string inputStr)
{
int result = 0;
var chatArray = inputStr.ToCharArray();
foreach (var cItem in chatArray)
{
switch (cItem)
{
case '一':
result += 1;
break;
case '二':
result += 2;
break;
case '三':
result += 3;
break;
case '四':
result += 4;
break;
case '五':
result += 5;
break;
case '六':
result += 6;
break;
case '七':
result += 7;
break;
case '八':
result += 8;
break;
case '九':
result += 9;
break;
case '十':
result += 10;
break;
}
}
return result;
}
/// <summary>
/// 拼接图片字符串
/// </summary>
/// <param name="base64"></param>
/// <returns></returns>
public static string GetImageString(string base64)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat(@"<img src='data:image/png;base64,{0}' />", base64);
return builder.ToString();
}
}
}
\ No newline at end of file
......@@ -2,10 +2,10 @@
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Edu.Common.Plugin
......@@ -20,12 +20,18 @@ namespace Edu.Common.Plugin
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static List<string> GetWordData(string filePath)
public static List<QuestionWordData> GetWordData(string filePath)
{
List<string> list = new List<string>();
List<QuestionWordData> list = new List<QuestionWordData>();
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(filePath);
document.SaveToFile("toDoc.html", FileFormat.Html);
string basepath = AppContext.BaseDirectory;
string tempPath = basepath + "\\upfile\\temporary\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
List<string> imageList = new List<string>();
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
foreach (Section section in document.Sections)
{
foreach (Paragraph paragraph in section.Paragraphs)
......@@ -36,22 +42,130 @@ namespace Edu.Common.Plugin
var pic = docObject as DocPicture;
if (pic != null)
{
string imageName = string.Format(@"Image-{0}.png", DateTime.Now.Ticks);
string imageName = string.Format(@"WordImage_{0}.png", DateTime.Now.Ticks);
using (MemoryStream ms = new MemoryStream(pic.ImageBytes))
{
Image outputImg = Image.FromStream(ms);
outputImg.Save(@"D:\" + imageName, System.Drawing.Imaging.ImageFormat.Png);
string tempImagePath = tempPath + imageName;
imageList.Add(tempImagePath);
outputImg.Save(tempImagePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
}
}
string str = document.GetText();
str = str.Replace("Evaluation Warning: The document was created with Spire.Doc for .NET.", "");
//“\r\n”开始+“1234567890”出现1次到多次+“、”结尾
string pattern = @"\r\n[1234567890]*、";
var tempArray = Regex.Split(str, pattern);
//解析大类
string categoryRule1 = @"[第][一二三四五六七八九十][部][分]";
Regex categoryReg = new Regex(categoryRule1);
string[] tempArray = null;
if (categoryReg.IsMatch(str))
{
tempArray = Regex.Split(str, categoryRule1);
if (tempArray != null && tempArray.Length > 0)
{
foreach (var item in tempArray)
{
if (!string.IsNullOrEmpty(item))
{
list.Add(AnalysisQuestionCategory(item, imageList, isBigType: true));
}
}
}
}
else
{
list.Add(AnalysisQuestionCategory(str, imageList));
}
return list;
}
/// <summary>
/// 解析问题大类下面的问题列表
/// </summary>
/// <param name="questionStr"></param>
/// <param name="isBigType">是否是大类</param>
/// <returns></returns>
private static QuestionWordData AnalysisQuestionCategory(string questionStr, List<string> imageList, bool isBigType = false)
{
QuestionWordData questionWordData = new QuestionWordData();
// \r\n【图一】\r\n
string imgPattern = @"\r\n[【][图][一二三四五六七八九十]*[】]";
Regex imgReg = new Regex(imgPattern);
//有图片
if (imgReg.IsMatch(questionStr))
{
var matchs = Regex.Matches(questionStr, imgPattern);
if (matchs != null && matchs.Count > 0)
{
foreach (var item in matchs)
{
var numStr = item.ToString().Replace("【", "").Replace("】", "").Replace("图", "").Trim();
var newNumber= ConvertHelper.ConvertStringToNumber(numStr);
if (newNumber > 0)
{
string filePath = imageList != null && imageList.Count >= newNumber ? imageList[newNumber - 1] : "";
if (!string.IsNullOrEmpty(filePath))
{
string imageBase64 = ConvertHelper.ConvertImageToBase64(filePath);
if (!string.IsNullOrEmpty(imageBase64))
{
questionStr = questionStr.Replace(item.ToString(), ConvertHelper.GetImageString(imageBase64));
}
}
}
}
}
}
string[] tempArray = null;
//“\r\n”开始+“1234567890”出现1次到多次+“、或.”结尾
string pattern1 = @"\r\n[1234567890]*[、.]";
Regex reg1 = new Regex(pattern1);
if (isBigType)
{
string pattern2 = @"\r\n[(][一二三四五六七八九十]*[)]";
Regex reg2 = new Regex(pattern2);
if (reg2.IsMatch(questionStr))
{
tempArray = Regex.Split(questionStr, pattern2);
}
else if (reg1.IsMatch(questionStr))
{
tempArray = Regex.Split(questionStr, pattern1);
}
if (tempArray != null && tempArray.Length > 0)
{
string str = tempArray[0].Trim().Replace("、", "").Replace(".", "");
questionWordData.BigTitle = str;
ArrayList al = new ArrayList(tempArray);
al.RemoveAt(0);
var newQuestionArray = (string[])al.ToArray(typeof(string));
questionWordData.QuestionList = AnalysisQuestionList(newQuestionArray);
}
}
else
{
if (reg1.IsMatch(questionStr))
{
tempArray = Regex.Split(questionStr, pattern1);
}
questionWordData.BigTitle = "默认";
questionWordData.QuestionList = AnalysisQuestionList(tempArray);
}
return questionWordData;
}
/// <summary>
/// 解析问题列表
/// </summary>
/// <param name="tempArray"></param>
/// <returns></returns>
private static List<string> AnalysisQuestionList(string[] tempArray)
{
List<string> list = new List<string>();
if (tempArray != null && tempArray.Length > 0)
{
foreach (var item in tempArray)
......@@ -65,4 +179,20 @@ namespace Edu.Common.Plugin
return list;
}
}
/// <summary>
/// 问题数据
/// </summary>
public class QuestionWordData
{
/// <summary>
/// 大类名称
/// </summary>
public string BigTitle { get; set; }
/// <summary>
/// 问题列表
/// </summary>
public List<string> QuestionList { get; set; }
}
}
......@@ -8,18 +8,39 @@
".NETCoreApp,Version=v3.0": {
"Edu.Common/1.0.0": {
"dependencies": {
"FreeSpire.Doc": "7.11.0",
"Microsoft.AspNetCore.Http": "2.2.2",
"Microsoft.AspNetCore.Mvc.Formatters.Json": "2.2.0",
"Microsoft.Extensions.Configuration": "3.1.8",
"Microsoft.Extensions.Configuration.Json": "3.1.8",
"NPOI": "2.5.1",
"Newtonsoft.Json": "12.0.3",
"Spire.Doc": "8.12.14"
"Newtonsoft.Json": "12.0.3"
},
"runtime": {
"Edu.Common.dll": {}
}
},
"FreeSpire.Doc/7.11.0": {
"dependencies": {
"SkiaSharp": "1.68.0",
"System.Security.Cryptography.Xml": "4.5.0",
"System.Text.Encoding.CodePages": "4.5.0"
},
"runtime": {
"lib/netcoreapp2.0/Spire.Doc.dll": {
"assemblyVersion": "7.11.0.41420",
"fileVersion": "7.11.0.41420"
},
"lib/netcoreapp2.0/Spire.License.dll": {
"assemblyVersion": "1.3.7.320",
"fileVersion": "1.3.7.320"
},
"lib/netcoreapp2.0/Spire.Pdf.dll": {
"assemblyVersion": "5.11.0.2420",
"fileVersion": "5.11.0.2420"
}
}
},
"Microsoft.AspNetCore.Authentication.Abstractions/2.2.0": {
"dependencies": {
"Microsoft.AspNetCore.Http.Abstractions": "2.2.0",
......@@ -568,27 +589,6 @@
}
}
},
"Spire.Doc/8.12.14": {
"dependencies": {
"SkiaSharp": "1.68.0",
"System.Security.Cryptography.Xml": "4.5.0",
"System.Text.Encoding.CodePages": "4.5.0"
},
"runtime": {
"lib/netstandard2.0/Spire.Doc.dll": {
"assemblyVersion": "8.12.14.0",
"fileVersion": "8.12.14.4420"
},
"lib/netstandard2.0/Spire.License.dll": {
"assemblyVersion": "1.3.8.320",
"fileVersion": "1.3.8.320"
},
"lib/netstandard2.0/Spire.Pdf.dll": {
"assemblyVersion": "6.12.20.0",
"fileVersion": "6.12.20.2420"
}
}
},
"System.AppContext/4.1.0": {
"dependencies": {
"System.Runtime": "4.1.0"
......@@ -978,6 +978,13 @@
"serviceable": false,
"sha512": ""
},
"FreeSpire.Doc/7.11.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-Rg7axyvwCU8PPuhsetIM4FZuuOL6PROJOXlhJo760wDS/4JJdo99JVDjqpGHej78yLYMSvtSuq0jjuTIYNEZgw==",
"path": "freespire.doc/7.11.0",
"hashPath": "freespire.doc.7.11.0.nupkg.sha512"
},
"Microsoft.AspNetCore.Authentication.Abstractions/2.2.0": {
"type": "package",
"serviceable": true,
......@@ -1293,13 +1300,6 @@
"path": "skiasharp/1.68.0",
"hashPath": "skiasharp.1.68.0.nupkg.sha512"
},
"Spire.Doc/8.12.14": {
"type": "package",
"serviceable": true,
"sha512": "sha512-ZHJVAjEcPNvUbEIe+46zXlYG7J7T1Pua7jpsuH3yMllqD1xysMdVhR9hAywkGYnaHodv0gjIP33SiD4Kgh7ykQ==",
"path": "spire.doc/8.12.14",
"hashPath": "spire.doc.8.12.14.nupkg.sha512"
},
"System.AppContext/4.1.0": {
"type": "package",
"serviceable": true,
......
using Edu.Common.Enum;
using Edu.Common.Enum.Course;
using Edu.Common.Enum.Question;
using System;
using VT.FW.DB;
......@@ -101,5 +102,10 @@ namespace Edu.Model.Entity.Question
/// 填空题(答案顺序打乱也判正确)
/// </summary>
public int IsMutex { get; set; }
/// <summary>
/// 题目所属大类
/// </summary>
public QuestionCategoryEnum? Category { get; set; }
}
}
......@@ -91,6 +91,7 @@ namespace Edu.Module.Question
{nameof(RB_Question_ViewModel.UpdateTime),model.UpdateTime },
{nameof(RB_Question_ViewModel.Answer),model.Answer },
{nameof(RB_Question_ViewModel.IsMutex),model.IsMutex },
{nameof(RB_Question_ViewModel.Category),model.Category },
};
flag = questionRepository.Update(fileds, new WhereHelper(nameof(RB_Question_ViewModel.QuestionId), model.QuestionId));
}
......
......@@ -6,9 +6,15 @@ namespace Edu.Test
{
static void Main(string[] args)
{
//string filePath = @"C:/Users/qiaoyajun/Desktop/Word模板.docx";
//var data = Common.Data.QuestionHelper.GetWordQuestionData(filePath);
//Console.WriteLine(Common.Plugin.JsonHelper.Serialize(data));
string filePath = "";
filePath = @"C:/Users/qiaoyajun/Desktop/模板二.docx";
filePath = @"C:/Users/qiaoyajun/Desktop/EduWordTemplate.doc";
var data = Common.Data.QuestionHelper.GetWordQuestionData(filePath);
Console.WriteLine(Common.Plugin.JsonHelper.Serialize(data));
if (data != null && data.Count > 0)
{
Console.WriteLine("总共:" + data.Count);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment