Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加对DataTable数据的模板渲染 #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions 5.DataTable渲染示例/5.DataTable渲染示例.csproj
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>_5.DataTable渲染示例</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\ExcelReport.Driver.NPOI\ExcelReport.Driver.NPOI.csproj" />
<ProjectReference Include="..\src\ExcelReport.Driver\ExcelReport.Driver.csproj" />
<ProjectReference Include="..\src\ExcelReport\ExcelReport.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Template\Template.xls">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions 5.DataTable渲染示例/Program.cs
@@ -0,0 +1,57 @@
using ExcelReport;
using ExcelReport.Driver.NPOI;
using ExcelReport.Renderers;
using System;
using System.Data;

namespace _5.DataTable渲染示例
{
class Program
{
static void Main(string[] args)
{
Configurator.Put(".xls", new WorkbookLoader());

try
{
var dataTable = GetDataTable();

ExportHelper.ExportToLocal(@"Template\Template.xls", "out.xls",
new SheetRenderer("访视视图",
new DataTableRenderer("Plants", dataTable)
)
);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.WriteLine("finished!");
}
private static DataTable GetDataTable()
{
DataTable dt = new DataTable("试验流程图");
dt.Columns.Add(new DataColumn("研究阶段", typeof(string)));
dt.Columns.Add(new DataColumn("数据集", typeof(string)));
dt.Columns.Add(new DataColumn("筛选期\r\n(第-14~1天)", typeof(string)));
dt.Columns.Add(new DataColumn("第一周期\r\n(第-1天)", typeof(string)));

var row = dt.NewRow();
row["研究阶段"] = "是否重复访视";
row["数据集"] = "";
row["筛选期\r\n(第-14~1天)"] = "否";
row["第一周期\r\n(第-1天)"] = "否";
dt.Rows.Add(row);

var row1 = dt.NewRow();
row1["研究阶段"] = "访视编号";
row1["数据集"] = "";
row1["筛选期\r\n(第-14~1天)"] = "1";
row1["第一周期\r\n(第-1天)"] = "2";
dt.Rows.Add(row1);

return dt;
}
}
}
Binary file added 5.DataTable渲染示例/Template/Template.xls
Binary file not shown.
7 changes: 7 additions & 0 deletions ExcelReport.sln
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExcelReport.Driver.CSV", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "4.CSV示例", "examples\4.CSV示例\4.CSV示例.csproj", "{95CA5870-B791-4086-BB5A-6750F81B0737}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "5.DataTable渲染示例", "5.DataTable渲染示例\5.DataTable渲染示例.csproj", "{8EE37151-3C5D-4CEA-AD96-8A2476931A8F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -61,6 +63,10 @@ Global
{95CA5870-B791-4086-BB5A-6750F81B0737}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95CA5870-B791-4086-BB5A-6750F81B0737}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95CA5870-B791-4086-BB5A-6750F81B0737}.Release|Any CPU.Build.0 = Release|Any CPU
{8EE37151-3C5D-4CEA-AD96-8A2476931A8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EE37151-3C5D-4CEA-AD96-8A2476931A8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EE37151-3C5D-4CEA-AD96-8A2476931A8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EE37151-3C5D-4CEA-AD96-8A2476931A8F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,6 +80,7 @@ Global
{1D4DF3D2-F983-4C16-9DE9-67B4DA5ED73E} = {D482BFE0-F981-4F4B-9F0E-0C586FA804FA}
{E1716F47-8A2D-48FB-892F-D2C628A0ACE2} = {D482BFE0-F981-4F4B-9F0E-0C586FA804FA}
{95CA5870-B791-4086-BB5A-6750F81B0737} = {319FF12B-5B28-42B6-87A6-4A31B8049281}
{8EE37151-3C5D-4CEA-AD96-8A2476931A8F} = {319FF12B-5B28-42B6-87A6-4A31B8049281}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4279AAA3-619A-4506-A58D-AAC9574061E9}
Expand Down
4 changes: 0 additions & 4 deletions README.md
Expand Up @@ -88,8 +88,4 @@ public static class StudentLogic



### 更多文章
[ExcelReport文档](https://www.yuque.com/motse/excelreport)

### 联系我
![image.png](https://cdn.nlark.com/yuque/0/2019/png/297115/1556684663311-403a17ef-c19d-4f4e-8055-7f21044be0be.png)
4 changes: 4 additions & 0 deletions src/ExcelReport.Driver.CSV/Row.cs
Expand Up @@ -73,5 +73,9 @@ public object GetOriginal()
{
return this;
}
public ICell CopyCell(int sourceIndex, int targetIdnex)
{
throw new NotImplementedException();
}
}
}
5 changes: 5 additions & 0 deletions src/ExcelReport.Driver.CSV/Sheet.cs
@@ -1,4 +1,5 @@
using ExcelReport.Driver.CSV.Extends;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -93,5 +94,9 @@ public object GetOriginal()
{
return this;
}
public void MergeCells(int firstRow, int lastRow, int firstCol, int lastCol)
{
throw new NotImplementedException();
}
}
}
5 changes: 4 additions & 1 deletion src/ExcelReport.Driver.NPOI/ExcelReport.Driver.NPOI.csproj
Expand Up @@ -22,9 +22,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ExcelReport.Driver" Version="2.0.0" />
<PackageReference Include="NPOI" Version="2.4.1" />
<PackageReference Include="NPOI.Extend" Version="1.1.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ExcelReport.Driver\ExcelReport.Driver.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/ExcelReport.Driver.NPOI/Row.cs
Expand Up @@ -34,5 +34,19 @@ public object GetOriginal()
{
return NpoiRow;
}

public ICell CopyCell(int sourceIndex, int targetIdnex)
{
var sourceCell = NpoiRow.GetCell(sourceIndex);
var targetCell = NpoiRow.CopyCell(sourceIndex, targetIdnex);

if (null != sourceCell.CellStyle)
{
targetCell.CellStyle = sourceCell.CellStyle;
}
targetCell.SetCellType(sourceCell.CellType);
ICell cell = targetCell.GetAdapter();
return cell;
}
}
}
7 changes: 7 additions & 0 deletions src/ExcelReport.Driver.NPOI/Sheet.cs
@@ -1,5 +1,6 @@
using ExcelReport.Driver.NPOI.Extends;
using NPOI.Extend;
using NPOI.SS.Util;
using System.Collections;
using System.Collections.Generic;
using NpoiRow = NPOI.SS.UserModel.IRow;
Expand Down Expand Up @@ -47,5 +48,11 @@ public object GetOriginal()
{
return NpoiSheet;
}

public void MergeCells(int firstRow, int lastRow, int firstCol, int lastCol)
{
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
NpoiSheet.AddMergedRegion(region);
}
}
}
1 change: 1 addition & 0 deletions src/ExcelReport.Driver/IRow.cs
Expand Up @@ -8,5 +8,6 @@ public interface IRow : IEnumerable<ICell>, IAdapter
{
get;
}
ICell CopyCell(int sourceIndex, int targetIdnex);
}
}
2 changes: 2 additions & 0 deletions src/ExcelReport.Driver/ISheet.cs
Expand Up @@ -14,5 +14,7 @@ public interface ISheet : IEnumerable<IRow>, IAdapter
int CopyRows(int start, int end);

int RemoveRows(int start, int end);

void MergeCells(int firstRow, int lastRow, int firstCol, int lastCol);
}
}
52 changes: 52 additions & 0 deletions src/ExcelReport/Contexts/SheetContext.cs
Expand Up @@ -36,6 +36,28 @@ public ICell GetCell(Location location)
return row[location.ColumnIndex];
}

public ICell GetCell(int row, int column, bool copyBeforeColumnCell = false)
{
IRow r = _sheet[row];

if (!copyBeforeColumnCell)
{
if (r.IsNull())
{
return null;
}
}
else
{
if (column - 1 >= 0)
{
r.CopyCell(column - 1, column);
}
}

return r[column];
}

public void CopyRepeaterTemplate(Repeater repeater, Action processTemplate)
{
var startRowIndex = _rowIndexAccumulation.GetCurrentRowIndex(repeater.Start.RowIndex);
Expand All @@ -58,5 +80,35 @@ public void RemoveRepeaterTemplate(Repeater repeater)
int span = _sheet.RemoveRows(startRowIndex, endRowIndex);
_rowIndexAccumulation.Add(-span);
}

public void FillDataTablerLayout(DataTabler dataTabler, Action processDataTableFilled)
{
var startRowIndex = _rowIndexAccumulation.GetCurrentRowIndex(dataTabler.DataStart.RowIndex);
var endRowIndex = _rowIndexAccumulation.GetCurrentRowIndex(dataTabler.DataEnd.RowIndex);

ICell headerCell = GetCell(dataTabler.HeaderStart);
headerCell.Value = headerCell.GetStringValue().CutEndOf($"$<{dataTabler.Name}>[header]");

ICell dataStartCell = GetCell(dataTabler.DataStart);
dataStartCell.Value = dataStartCell.GetStringValue().CutEndOf($"$<{dataTabler.Name}>[data]");

for (int s = 0, l = endRowIndex - startRowIndex; s < l; s++)
{
_sheet.CopyRows(startRowIndex + s, startRowIndex + s);
}

processDataTableFilled();

_rowIndexAccumulation.Add(endRowIndex - startRowIndex + 1);
}

public void MergeCells(int firstRow, int lastRow, int firstCol, int lastCol)
{
if (firstRow < 0 || lastRow < firstRow || firstCol < 0 || lastCol < firstCol)
{
return;
}
_sheet.MergeCells(firstRow, lastRow, firstCol, lastCol);
}
}
}
2 changes: 1 addition & 1 deletion src/ExcelReport/ExcelReport.csproj
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ExcelReport.Driver" Version="2.0.0" />
<ProjectReference Include="..\ExcelReport.Driver\ExcelReport.Driver.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/ExcelReport/Meta/DataTabler.cs
@@ -0,0 +1,10 @@
namespace ExcelReport.Meta
{
public class DataTabler:Named
{
public Location HeaderStart { get; set; }
public Location HenderEnd { get; set; }
public Location DataStart { get; set; }
public Location DataEnd { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/ExcelReport/Meta/SheetContainer.cs
Expand Up @@ -5,5 +5,7 @@ public class SheetContainer : Named
public Container<Parameter> Parameters { get; } = new Container<Parameter>();

public Container<Repeater> Repeaters { get; } = new Container<Repeater>();

public Container<DataTabler> DataTablers { get; } = new Container<DataTabler>();
}
}
12 changes: 12 additions & 0 deletions src/ExcelReport/Parsers/DataTableDataParser.cs
@@ -0,0 +1,12 @@
using System;
using System.Text.RegularExpressions;

namespace ExcelReport.Parsers
{
public class DataTableDataParser : RegexParser
{
private static readonly Regex regex = new Regex(@"(?<=\$\<)([\w]*)(?=\>\[data\])");

public override Regex Regex => regex;
}
}
12 changes: 12 additions & 0 deletions src/ExcelReport/Parsers/DataTableHeaderParser.cs
@@ -0,0 +1,12 @@
using System;
using System.Text.RegularExpressions;

namespace ExcelReport.Parsers
{
public sealed class DataTableHeaderParser:RegexParser
{
private static readonly Regex regex = new Regex(@"(?<=\$\<)([\w]*)(?=\>\[header\])");

public override Regex Regex => regex;
}
}
14 changes: 14 additions & 0 deletions src/ExcelReport/Parsers/TemplateParser.cs
Expand Up @@ -12,6 +12,10 @@ public sealed class TemplateParser

private static readonly RepeaterEndParser REPEATER_END_PARSER = new RepeaterEndParser();

private static readonly DataTableHeaderParser DATATABLE_HEADER_PARSER = new DataTableHeaderParser();

private static readonly DataTableDataParser DATATABLE_DATA_PARSER = new DataTableDataParser();

public WorkbookContainer Parse(IWorkbook workbook)
{
WorkbookContainer workbookContainer = new WorkbookContainer();
Expand All @@ -37,6 +41,16 @@ public WorkbookContainer Parse(IWorkbook workbook)
{
workbookContainer.Sheets[sheet.SheetName].Repeaters[tagName].End = new Location(cell.RowIndex, cell.ColumnIndex);
}

foreach (var tagName in DATATABLE_HEADER_PARSER.Parse(cell.GetStringValue()))
{
workbookContainer.Sheets[sheet.SheetName].DataTablers[tagName].HeaderStart = new Location(cell.RowIndex, cell.ColumnIndex);
}

foreach (var tagName in DATATABLE_DATA_PARSER.Parse(cell.GetStringValue()))
{
workbookContainer.Sheets[sheet.SheetName].DataTablers[tagName].DataStart = new Location(cell.RowIndex, cell.ColumnIndex);
}
}
}
}
Expand Down