Skip to content

Commit 8799bd2

Browse files
committed
暂存
1 parent dcada0d commit 8799bd2

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
- Pdf支持
6262
- HTML
6363
- 导入结果支持生成HTML输出
64+
- 导入重复验证
6465

6566
### 更新历史
6667

src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ protected virtual void ParseTemplate(ExcelPackage excelPackage)
186186
break;
187187
}
188188

189+
//不处理空表头
190+
if (string.IsNullOrWhiteSpace(header))
191+
{
192+
continue;
193+
}
194+
189195
if (excelHeaders.ContainsKey(header))
190196
{
191197
ImportResult.TemplateErrors.Add(new TemplateErrorInfo()
@@ -257,7 +263,7 @@ protected virtual bool ParseImporterHeader(out Dictionary<int, IDictionary<strin
257263
enumColumns = new Dictionary<int, IDictionary<string, int>>();
258264
boolColumns = new List<int>();
259265
var objProperties = typeof(T).GetProperties();
260-
if (objProperties == null || objProperties.Length == 0)
266+
if (objProperties.Length == 0)
261267
return false;
262268
for (var i = 0; i < objProperties.Length; i++)
263269
{

src/Magicodes.ExporterAndImporter.Html/HtmlExporter.cs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
using System;
1+
using Magicodes.ExporterAndImporter.Core;
2+
using Magicodes.ExporterAndImporter.Core.Models;
3+
using Microsoft.CodeAnalysis.CSharp.Scripting;
4+
using System;
25
using System.Collections.Generic;
36
using System.IO;
47
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Magicodes.ExporterAndImporter.Core;
7-
using Magicodes.ExporterAndImporter.Core.Models;
88
using System.Reflection;
9-
using RazorEngine;
10-
using RazorEngine.Configuration;
9+
using System.Text.RegularExpressions;
10+
using System.Threading.Tasks;
1111
using Encoding = System.Text.Encoding;
12-
using RazorEngine.Templating;
1312

1413
namespace Magicodes.ExporterAndImporter.Html
1514
{
1615
/// <summary>
1716
/// HTML导出
1817
/// </summary>
19-
public class HtmlExporter: IExporterByTemplate
18+
public class HtmlExporter : IExporterByTemplate
2019
{
2120
/// <summary>
2221
/// 根据模板导出
@@ -26,19 +25,59 @@ public class HtmlExporter: IExporterByTemplate
2625
/// <param name="dataItems"></param>
2726
/// <param name="htmlTemplate">Html模板内容</param>
2827
/// <returns></returns>
29-
public Task<TemplateFileInfo> ExportByTemplate<T>(string fileName, IList<T> dataItems, string htmlTemplate = null) where T : class
28+
public async Task<TemplateFileInfo> ExportByTemplate<T>(string fileName, IList<T> dataItems, string htmlTemplate = null) where T : class
3029
{
3130
if (string.IsNullOrWhiteSpace(htmlTemplate))
3231
{
33-
var defaultHtmlTpl = ReadManifestData<HtmlExporter>("default.cshtml");
34-
var config = new TemplateServiceConfiguration()
32+
var defaultHtmlTpl = ReadManifestData<HtmlExporter>("default.html");
33+
34+
var script = CSharpScript.Create<string>("string htmlStr=\"\";");
35+
var matches = Regex.Matches(defaultHtmlTpl, @"@foreach[\w\s{}.<>()/\[\]\+\-?|\\*`$]+}", RegexOptions.Multiline);
36+
foreach (Match match in matches)
3537
{
36-
37-
};
38-
var service = RazorEngineService.Create(config);
39-
Engine.Razor = service;
40-
var res = Engine.Razor.RunCompile(defaultHtmlTpl, fileName, typeof(IList<T>), dataItems);
41-
var t = new TemplateFileInfo();
38+
if (match.Success)
39+
{
40+
//var sb = new StringBuilder();
41+
var list = Regex.Split(match.Value, Environment.NewLine).Where(p => p.Trim() != "{" && p.Trim() != "}")
42+
.ToList();
43+
foreach (var dataItem in dataItems)
44+
{
45+
foreach (var line in list)
46+
{
47+
script.ContinueWith("htmlStr +=\"\\n\";");
48+
if (line.Contains("{"))
49+
{
50+
var codeStr = await CSharpScript.RunAsync<string>("var res=$\"" + line + "\";", globals: dataItem);
51+
var codeRes = await codeStr.ContinueWithAsync("res");
52+
script.ContinueWith("htmlStr +=$\"" + codeRes.ReturnValue + "\";");
53+
}
54+
else
55+
{
56+
script.ContinueWith("htmlStr +=$\"" + line + "\";");
57+
}
58+
59+
//var res = Regex.Matches(line, @"{{[\w\s.()/\[\]\+\-?|\\*`$]+}}", RegexOptions.None);
60+
//if (res.Count == 0)
61+
//{
62+
// script.ContinueWith("htmlStr +=\"" + line + "\"");
63+
//}
64+
//else
65+
//{
66+
// script.ContinueWith("htmlStr +=$\"" + line + "\"");
67+
//}
68+
}
69+
}
70+
71+
72+
73+
}
74+
}
75+
var result = await script.RunAsync();
76+
var a = await result.ContinueWithAsync("res");
77+
var test = a.ReturnValue;
78+
//var result = script.;
79+
//var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(IList<T>));
80+
//script.Compile();
4281
}
4382
throw new NotImplementedException();
4483
}
@@ -54,7 +93,7 @@ public static string ReadManifestData<TSource>(string embeddedFileName) where TS
5493
{
5594
throw new InvalidOperationException("Could not load manifest resource stream.");
5695
}
57-
using (var reader = new StreamReader(stream,encoding: Encoding.UTF8))
96+
using (var reader = new StreamReader(stream, encoding: Encoding.UTF8))
5897
{
5998
return reader.ReadToEnd();
6099
}

src/Magicodes.ExporterAndImporter.Html/Magicodes.ExporterAndImporter.Html.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<None Remove="Tpls\default.cshtml" />
8+
<None Remove="Tpls\default.html" />
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<EmbeddedResource Include="Tpls\default.cshtml" />
12+
<EmbeddedResource Include="Tpls\default.html" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="RazorEngine" Version="3.10.0" />
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.2.1" />
1717
</ItemGroup>
18-
1918
<ItemGroup>
2019
<ProjectReference Include="..\Magicodes.ExporterAndImporter.Core\Magicodes.ExporterAndImporter.Core.csproj" />
2120
</ItemGroup>

src/Magicodes.ExporterAndImporter.Html/Tpls/default.cshtml renamed to src/Magicodes.ExporterAndImporter.Html/Tpls/default.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ <h1 style="text-align:center">{{Title}}</h1>
3030
<th>{{Value}}</th>
3131
</tr>
3232
</thead>
33-
@foreach (var item in Model)
33+
@foreach
3434
{
3535
<tr>
36-
<td>{{Name}}</td>
37-
<td>@item.Name1</td>
38-
36+
<td>{Name1}</td>
37+
<td>{Name2}</td>
3938
</tr>
4039
}
40+
4141
</table>
4242
</body>
4343
</html>

0 commit comments

Comments
 (0)