diff --git a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
index c300cbf0..7ec24856 100644
--- a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
+++ b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs
@@ -1,5 +1,6 @@
using System.Globalization;
using MiniExcelLibs.Attributes;
+using System.Linq;
namespace MiniExcelLibs.OpenXml.Constants
{
@@ -46,8 +47,17 @@ internal static string StartRow(int rowIndex)
internal const string EndRow = "";
internal const string StartCols = "";
- internal static string Column(int? colIndex, double? columnWidth)
- => $@"";
+ internal static string Column(int colIndex, double columnWidth)
+ => $@"";
+
+
+ private static readonly int _maxColumnLength = Column(int.MaxValue, double.MaxValue).Length;
+
+ public static int GetColumnPlaceholderLength(int columnCount)
+ {
+ return StartCols.Length + (_maxColumnLength * columnCount) + EndCols.Length;
+ }
+
internal const string EndCols = "";
internal static string EmptyCell(string cellReference, string styleIndex)
diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
index d852a4d7..7df601ec 100644
--- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
+++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
@@ -6,6 +6,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Data;
+using System.Globalization;
using System.IO.Compression;
using System.Linq;
using System.Text;
@@ -104,6 +105,8 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
var yIndex = 1;
int maxColumnIndex;
int maxRowIndex;
+ ExcelWidthCollection widths = null;
+ long columnWidthsPlaceholderPosition = 0;
{
if (_configuration.FastMode)
{
@@ -122,7 +125,15 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
//sheet view
await writer.WriteAsync(GetSheetViews());
- await WriteColumnsWidthsAsync(writer, props);
+ if (_configuration.EnableAutoWidth)
+ {
+ columnWidthsPlaceholderPosition = await WriteColumnWidthPlaceholdersAsync(writer, props);
+ widths = new ExcelWidthCollection(_configuration.MinWidth, _configuration.MaxWidth, props);
+ }
+ else
+ {
+ await WriteColumnsWidthsAsync(writer, ExcelColumnWidth.FromProps(props));
+ }
await writer.WriteAsync(WorksheetXml.StartSheetData);
int fieldCount = reader.FieldCount;
@@ -139,7 +150,7 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
for (int i = 0; i < fieldCount; i++)
{
var cellValue = reader.GetValue(i);
- await WriteCellAsync(writer, yIndex, xIndex, cellValue, props[i]);
+ await WriteCellAsync(writer, yIndex, xIndex, cellValue, props[i], widths);
xIndex++;
}
await writer.WriteAsync(WorksheetXml.EndRow);
@@ -163,6 +174,10 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
{
await WriteDimensionAsync(writer, maxRowIndex, maxColumnIndex, dimensionPlaceholderPostition);
}
+ if (_configuration.EnableAutoWidth)
+ {
+ await OverWriteColumnWidthPlaceholdersAsync(writer, columnWidthsPlaceholderPosition, widths.Columns);
+ }
}
private async Task GenerateSheetByEnumerableAsync(MiniExcelAsyncStreamWriter writer, IEnumerable values)
@@ -248,7 +263,17 @@ private async Task GenerateSheetByEnumerableAsync(MiniExcelAsyncStreamWriter wri
await writer.WriteAsync(GetSheetViews());
//cols:width
- await WriteColumnsWidthsAsync(writer, props);
+ ExcelWidthCollection widths = null;
+ long columnWidthsPlaceholderPosition = 0;
+ if (_configuration.EnableAutoWidth)
+ {
+ columnWidthsPlaceholderPosition = await WriteColumnWidthPlaceholdersAsync(writer, props);
+ widths = new ExcelWidthCollection(_configuration.MinWidth, _configuration.MaxWidth, props);
+ }
+ else
+ {
+ await WriteColumnsWidthsAsync(writer, ExcelColumnWidth.FromProps(props));
+ }
//header
await writer.WriteAsync(WorksheetXml.StartSheetData);
@@ -266,13 +291,13 @@ private async Task GenerateSheetByEnumerableAsync(MiniExcelAsyncStreamWriter wri
switch (mode)
{
case "IDictionary": //Dapper Row
- maxRowIndex = await GenerateSheetByColumnInfoAsync>(writer, enumerator, props, xIndex, yIndex);
+ maxRowIndex = await GenerateSheetByColumnInfoAsync>(writer, enumerator, props, widths, xIndex, yIndex);
break;
case "IDictionary":
- maxRowIndex = await GenerateSheetByColumnInfoAsync(writer, enumerator, props, xIndex, yIndex);
+ maxRowIndex = await GenerateSheetByColumnInfoAsync(writer, enumerator, props, widths, xIndex, yIndex);
break;
case "Properties":
- maxRowIndex = await GenerateSheetByColumnInfoAsync