mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
Async implementation of freezing top row (#684)
* Add async implementation of frozen rows and columns * Add unit tests for async implementation of frozen rows and columns * Fix formatting and delete temp file in unit test * move freezing top row implementation to DefaultOpenXml.cs
This commit is contained in:
parent
a0797a53f6
commit
92b295d4b2
@ -119,6 +119,9 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
maxColumnIndex = props.Count;
|
||||
|
||||
//sheet view
|
||||
await writer.WriteAsync(GetSheetViews());
|
||||
|
||||
await WriteColumnsWidthsAsync(writer, props);
|
||||
|
||||
await writer.WriteAsync(WorksheetXml.StartSheetData);
|
||||
@ -241,6 +244,9 @@ namespace MiniExcelLibs.OpenXml
|
||||
await writer.WriteAsync(WorksheetXml.Dimension(GetDimensionRef(maxRowIndex, maxColumnIndex)));
|
||||
}
|
||||
|
||||
//sheet view
|
||||
await writer.WriteAsync(GetSheetViews());
|
||||
|
||||
//cols:width
|
||||
await WriteColumnsWidthsAsync(writer, props);
|
||||
|
||||
@ -309,6 +315,9 @@ namespace MiniExcelLibs.OpenXml
|
||||
props.Add(prop);
|
||||
}
|
||||
|
||||
//sheet view
|
||||
await writer.WriteAsync(GetSheetViews());
|
||||
|
||||
await WriteColumnsWidthsAsync(writer, props);
|
||||
|
||||
await writer.WriteAsync(WorksheetXml.StartSheetData);
|
||||
|
@ -84,6 +84,103 @@ namespace MiniExcelLibs.OpenXml
|
||||
return info;
|
||||
}
|
||||
|
||||
private string GetSheetViews()
|
||||
{
|
||||
// exit early if no style to write
|
||||
if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
// start sheetViews
|
||||
sb.Append(WorksheetXml.StartSheetViews);
|
||||
sb.Append(WorksheetXml.StartSheetView());
|
||||
|
||||
// Write panes
|
||||
sb.Append(GetPanes());
|
||||
|
||||
// end sheetViews
|
||||
sb.Append(WorksheetXml.EndSheetView);
|
||||
sb.Append(WorksheetXml.EndSheetViews);
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string GetPanes()
|
||||
{
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
string activePane;
|
||||
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
|
||||
{
|
||||
activePane = "bottomRight";
|
||||
}
|
||||
else if (_configuration.FreezeColumnCount > 0)
|
||||
{
|
||||
activePane = "topRight";
|
||||
}
|
||||
else
|
||||
{
|
||||
activePane = "bottomLeft";
|
||||
}
|
||||
sb.Append(
|
||||
WorksheetXml.StartPane(
|
||||
xSplit: _configuration.FreezeColumnCount > 0 ? _configuration.FreezeColumnCount : (int?)null,
|
||||
ySplit: _configuration.FreezeRowCount > 0 ? _configuration.FreezeRowCount : (int?)null,
|
||||
topLeftCell: ExcelOpenXmlUtils.ConvertXyToCell(
|
||||
_configuration.FreezeColumnCount + 1,
|
||||
_configuration.FreezeRowCount + 1
|
||||
),
|
||||
activePane: activePane,
|
||||
state: "frozen"
|
||||
)
|
||||
);
|
||||
|
||||
// write pane selections
|
||||
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
|
||||
{
|
||||
// freeze row and column
|
||||
/*
|
||||
<selection pane="topRight" activeCell="B1" sqref="B1"/>
|
||||
<selection pane="bottomLeft" activeCell="A3" sqref="A3"/>
|
||||
<selection pane="bottomRight" activeCell="B3" sqref="B3"/>
|
||||
*/
|
||||
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, 1);
|
||||
sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
|
||||
|
||||
var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount + 1);
|
||||
sb.Append(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));
|
||||
|
||||
var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, _configuration.FreezeRowCount + 1);
|
||||
sb.Append(WorksheetXml.PaneSelection("bottomRight", cellBR, cellBR));
|
||||
}
|
||||
else if (_configuration.FreezeColumnCount > 0)
|
||||
{
|
||||
// freeze column
|
||||
/*
|
||||
<selection pane="topRight" activeCell="A1" sqref="A1"/>
|
||||
*/
|
||||
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1);
|
||||
sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// freeze row
|
||||
/*
|
||||
<selection pane="bottomLeft"/>
|
||||
*/
|
||||
sb.Append(WorksheetXml.PaneSelection("bottomLeft", null, null));
|
||||
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
|
||||
}
|
||||
|
||||
private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName)
|
||||
{
|
||||
var prop = new ExcelColumnInfo
|
||||
|
@ -152,7 +152,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
maxColumnIndex = props.Count;
|
||||
|
||||
//sheet view
|
||||
WriteSheetViews(writer);
|
||||
writer.Write(GetSheetViews());
|
||||
|
||||
WriteColumnsWidths(writer, props);
|
||||
|
||||
@ -279,7 +279,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
|
||||
//sheet view
|
||||
WriteSheetViews(writer);
|
||||
writer.Write(GetSheetViews());
|
||||
|
||||
//cols:width
|
||||
WriteColumnsWidths(writer, props);
|
||||
@ -352,7 +352,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
|
||||
//sheet view
|
||||
WriteSheetViews(writer);
|
||||
writer.Write(GetSheetViews());
|
||||
|
||||
WriteColumnsWidths(writer, props);
|
||||
|
||||
@ -411,91 +411,6 @@ namespace MiniExcelLibs.OpenXml
|
||||
writer.Write(WorksheetXml.EndCols);
|
||||
}
|
||||
|
||||
private void WriteSheetViews(MiniExcelStreamWriter writer) {
|
||||
// exit early if no style to write
|
||||
if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// start sheetViews
|
||||
writer.Write(WorksheetXml.StartSheetViews);
|
||||
writer.Write(WorksheetXml.StartSheetView());
|
||||
|
||||
// Write panes
|
||||
WritePanes(writer);
|
||||
|
||||
// end sheetViews
|
||||
writer.Write(WorksheetXml.EndSheetView);
|
||||
writer.Write(WorksheetXml.EndSheetViews);
|
||||
}
|
||||
|
||||
private void WritePanes(MiniExcelStreamWriter writer) {
|
||||
|
||||
string activePane;
|
||||
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
|
||||
{
|
||||
activePane = "bottomRight";
|
||||
}
|
||||
else if (_configuration.FreezeColumnCount > 0)
|
||||
{
|
||||
activePane = "topRight";
|
||||
}
|
||||
else
|
||||
{
|
||||
activePane = "bottomLeft";
|
||||
}
|
||||
writer.Write( WorksheetXml.StartPane(
|
||||
xSplit: _configuration.FreezeColumnCount > 0 ? _configuration.FreezeColumnCount : (int?)null,
|
||||
ySplit: _configuration.FreezeRowCount > 0 ? _configuration.FreezeRowCount : (int?)null,
|
||||
topLeftCell: ExcelOpenXmlUtils.ConvertXyToCell(
|
||||
_configuration.FreezeColumnCount + 1,
|
||||
_configuration.FreezeRowCount + 1
|
||||
),
|
||||
activePane: activePane,
|
||||
state: "frozen"
|
||||
) );
|
||||
|
||||
// write pane selections
|
||||
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
|
||||
{
|
||||
// freeze row and column
|
||||
/*
|
||||
<selection pane="topRight" activeCell="B1" sqref="B1"/>
|
||||
<selection pane="bottomLeft" activeCell="A3" sqref="A3"/>
|
||||
<selection pane="bottomRight" activeCell="B3" sqref="B3"/>
|
||||
*/
|
||||
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount+1, 1);
|
||||
writer.Write(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
|
||||
|
||||
var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount+1);
|
||||
writer.Write(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));
|
||||
|
||||
var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount+1, _configuration.FreezeRowCount+1);
|
||||
writer.Write(WorksheetXml.PaneSelection("bottomRight", cellBR, cellBR));
|
||||
}
|
||||
else if ( _configuration.FreezeColumnCount > 0 )
|
||||
{
|
||||
// freeze column
|
||||
/*
|
||||
<selection pane="topRight" activeCell="A1" sqref="A1"/>
|
||||
*/
|
||||
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1);
|
||||
writer.Write(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// freeze row
|
||||
/*
|
||||
<selection pane="bottomLeft"/>
|
||||
*/
|
||||
writer.Write(WorksheetXml.PaneSelection("bottomLeft", null, null));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void PrintHeader(MiniExcelStreamWriter writer, List<ExcelColumnInfo> props)
|
||||
{
|
||||
var xIndex = 1;
|
||||
|
@ -894,6 +894,70 @@ namespace MiniExcelLibs.Tests
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
|
||||
[Fact()]
|
||||
public async Task SaveAsFrozenRowsAndColumnsTest()
|
||||
{
|
||||
|
||||
var config = new OpenXmlConfiguration
|
||||
{
|
||||
FreezeRowCount = 1,
|
||||
FreezeColumnCount = 2
|
||||
};
|
||||
|
||||
{
|
||||
// Test enumerable
|
||||
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
|
||||
await MiniExcel.SaveAsAsync(
|
||||
path,
|
||||
new[] {
|
||||
new { Column1 = "MiniExcel", Column2 = 1 },
|
||||
new { Column1 = "Github", Column2 = 2}
|
||||
},
|
||||
configuration: config
|
||||
);
|
||||
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
var rows = stream.Query(useHeaderRow: true).ToList();
|
||||
|
||||
Assert.Equal("MiniExcel", rows[0].Column1);
|
||||
Assert.Equal(1, rows[0].Column2);
|
||||
Assert.Equal("Github", rows[1].Column1);
|
||||
Assert.Equal(2, rows[1].Column2);
|
||||
}
|
||||
|
||||
Assert.Equal("A1:B3", Helpers.GetFirstSheetDimensionRefValue(path));
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
{
|
||||
// test table
|
||||
var table = new DataTable();
|
||||
{
|
||||
table.Columns.Add("a", typeof(string));
|
||||
table.Columns.Add("b", typeof(decimal));
|
||||
table.Columns.Add("c", typeof(bool));
|
||||
table.Columns.Add("d", typeof(DateTime));
|
||||
table.Rows.Add("some text", 1234567890, true, DateTime.Now);
|
||||
table.Rows.Add(@"<test>Hello World</test>", -1234567890, false, DateTime.Now.Date);
|
||||
}
|
||||
var pathTable = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
|
||||
await MiniExcel.SaveAsAsync(pathTable, table, configuration: config);
|
||||
|
||||
Assert.Equal("A1:D3", Helpers.GetFirstSheetDimensionRefValue(pathTable));
|
||||
|
||||
|
||||
// data reader
|
||||
var reader = table.CreateDataReader();
|
||||
var pathReader = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
|
||||
|
||||
await MiniExcel.SaveAsAsync(pathReader, reader, configuration: config);
|
||||
Assert.Equal("A1:D3", Helpers.GetFirstSheetDimensionRefValue(pathTable));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public async Task SaveAsByDapperRows()
|
||||
{
|
||||
|
@ -860,7 +860,8 @@ namespace MiniExcelLibs.Tests
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void SaveAsFrozenRowsAndColumnsTest() {
|
||||
public void SaveAsFrozenRowsAndColumnsTest()
|
||||
{
|
||||
|
||||
var config = new OpenXmlConfiguration
|
||||
{
|
||||
@ -880,7 +881,8 @@ namespace MiniExcelLibs.Tests
|
||||
configuration: config
|
||||
);
|
||||
|
||||
using (var stream = File.OpenRead(path)) {
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
var rows = stream.Query(useHeaderRow: true).ToList();
|
||||
|
||||
Assert.Equal("MiniExcel", rows[0].Column1);
|
||||
@ -890,7 +892,7 @@ namespace MiniExcelLibs.Tests
|
||||
}
|
||||
|
||||
Assert.Equal("A1:B3", Helpers.GetFirstSheetDimensionRefValue(path));
|
||||
//File.Delete(path);
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
{
|
||||
@ -905,7 +907,7 @@ namespace MiniExcelLibs.Tests
|
||||
table.Rows.Add(@"<test>Hello World</test>", -1234567890, false, DateTime.Now.Date);
|
||||
}
|
||||
var pathTable = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
|
||||
MiniExcel.SaveAs(pathTable, table, configuration: config );
|
||||
MiniExcel.SaveAs(pathTable, table, configuration: config);
|
||||
|
||||
Assert.Equal("A1:D3", Helpers.GetFirstSheetDimensionRefValue(pathTable));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user