mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
- Support : empty rows dimension
- Fix : dimension just one column parsing error
This commit is contained in:
parent
a9d30e018e
commit
1c72760c71
BIN
samples/xlsx/TestDimensionC3.xlsx
Normal file
BIN
samples/xlsx/TestDimensionC3.xlsx
Normal file
Binary file not shown.
@ -42,6 +42,7 @@
|
||||
var xy = ExcelOpenXmlUtils.ConvertCellToXY(startCell);
|
||||
|
||||
var defaultFiles = GetDefaultFiles();
|
||||
var dimensionRef = string.Empty;
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
@ -50,6 +51,15 @@
|
||||
if (value is DataTable)
|
||||
{
|
||||
var dt = value as DataTable;
|
||||
|
||||
var maxRowIndex = dt.Rows.Count;
|
||||
var maxColumnIndex = dt.Columns.Count;
|
||||
// dimension
|
||||
{
|
||||
if (maxRowIndex == 0 && maxColumnIndex == 0)
|
||||
dimensionRef = "A1";
|
||||
}
|
||||
|
||||
if (printHeader)
|
||||
{
|
||||
sb.AppendLine($"<x:row r=\"{yIndex.ToString()}\">");
|
||||
@ -169,6 +179,7 @@
|
||||
|
||||
defaultFiles[@"xl/worksheets/sheet1.xml"].Xml = $@"<?xml version=""1.0"" encoding=""utf-8""?>
|
||||
<x:worksheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
|
||||
<dimension ref=""{dimensionRef}""/>
|
||||
<x:sheetData>{sb.ToString()}</x:sheetData>
|
||||
</x:worksheet>";
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ namespace MiniExcelLibs.OpenXml
|
||||
var sharedStringsEntry = entries.SingleOrDefault(w => w.FullName == "xl/sharedStrings.xml");
|
||||
if (sharedStringsEntry == null)
|
||||
return null;
|
||||
using (var reader = sharedStringsEntry.Open())
|
||||
using (var stream = sharedStringsEntry.Open())
|
||||
{
|
||||
var xl = XElement.Load(reader);
|
||||
var xl = XElement.Load(stream);
|
||||
var ts = xl.Descendants(ExcelOpenXmlXName.T).Select((s, i) => new { i, v = s.Value?.ToString() })
|
||||
.ToDictionary(s => s.i, s => s.v)
|
||||
;
|
||||
@ -234,7 +234,8 @@ namespace MiniExcelLibs.OpenXml
|
||||
if (string.IsNullOrEmpty(@ref))
|
||||
throw new InvalidOperationException("Without sheet dimension data");
|
||||
var rs = @ref.Split(':');
|
||||
if (ReferenceHelper.ParseReference(rs[1], out int cIndex, out int rIndex))
|
||||
// issue : https://github.com/shps951023/MiniExcel/issues/102
|
||||
if (ReferenceHelper.ParseReference(rs.Length==2?rs[1]:rs[0], out int cIndex, out int rIndex))
|
||||
{
|
||||
maxColumnIndex = cIndex - 1;
|
||||
maxRowIndex = rIndex - 1;
|
||||
|
@ -14,6 +14,10 @@ using System.Data.SQLite;
|
||||
using Dapper;
|
||||
using System.Globalization;
|
||||
using MiniExcelLibs.Tests.Utils;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace MiniExcelLibs.Tests
|
||||
{
|
||||
@ -251,38 +255,83 @@ namespace MiniExcelLibs.Tests
|
||||
}
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void FixDimensionJustOneColumnParsingError_Test()
|
||||
{
|
||||
{
|
||||
var path = @"..\..\..\..\..\samples\xlsx\TestDimensionC3.xlsx";
|
||||
using (var stream = File.OpenRead(path))
|
||||
{
|
||||
var rows = stream.Query().ToList();
|
||||
var keys = (rows.First() as IDictionary<string, object>).Keys;
|
||||
Assert.Equal(3, keys.Count);
|
||||
Assert.Equal(2, rows.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void SaveAsFileWithDimension()
|
||||
{
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
|
||||
var table = new DataTable();
|
||||
MiniExcel.SaveAs(path, table);
|
||||
Assert.Equal("A1", GetFirstSheetDimensionRefValue(path));
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetFirstSheetDimensionRefValue(string path)
|
||||
{
|
||||
string refV;
|
||||
using (var stream = File.OpenRead(path))
|
||||
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.UTF8))
|
||||
{
|
||||
var sheet = archive.Entries.Single(w => w.FullName.StartsWith("xl/worksheets/sheet1", StringComparison.OrdinalIgnoreCase));
|
||||
using (var sheetStream = sheet.Open())
|
||||
{
|
||||
var dimension = XElement.Load(sheetStream)
|
||||
.Descendants("dimension");
|
||||
refV = dimension.Single().Attribute("ref").Value;
|
||||
}
|
||||
}
|
||||
|
||||
return refV;
|
||||
}
|
||||
|
||||
//[Theory()]
|
||||
//[InlineData(@"..\..\..\..\..\samples\xlsx\ExcelDataReaderCollections\TestOpen\TestOpen.xlsx")]
|
||||
// public void QueryExcelDataReaderCheckTypeMappingTest(string path)
|
||||
// {
|
||||
//#if NETCOREAPP3_1 || NET5_0
|
||||
// System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
//#endif
|
||||
// public void QueryExcelDataReaderCheckTypeMappingTest(string path)
|
||||
// {
|
||||
//#if NETCOREAPP3_1 || NET5_0
|
||||
// System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
//#endif
|
||||
|
||||
// DataSet exceldatareaderResult;
|
||||
// using (var stream = File.OpenRead(path))
|
||||
// using (var reader = ExcelReaderFactory.CreateReader(stream))
|
||||
// {
|
||||
// exceldatareaderResult = reader.AsDataSet();
|
||||
// }
|
||||
// DataSet exceldatareaderResult;
|
||||
// using (var stream = File.OpenRead(path))
|
||||
// using (var reader = ExcelReaderFactory.CreateReader(stream))
|
||||
// {
|
||||
// exceldatareaderResult = reader.AsDataSet();
|
||||
// }
|
||||
|
||||
// using (var stream = File.OpenRead(path))
|
||||
// {
|
||||
// var rows = stream.Query().ToList();
|
||||
// Assert.Equal(exceldatareaderResult.Tables[0].Rows.Count, rows.Count);
|
||||
// foreach (IDictionary<string, object> row in rows)
|
||||
// {
|
||||
// var rowIndex = rows.IndexOf(row);
|
||||
// var keys = row.Keys;
|
||||
// foreach (var key in keys)
|
||||
// {
|
||||
// var eV = exceldatareaderResult.Tables[0].Rows[rowIndex][int.Parse(key)];
|
||||
// var v = row[key] == null ? DBNull.Value : row[key];
|
||||
// Assert.Equal(eV, v);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// using (var stream = File.OpenRead(path))
|
||||
// {
|
||||
// var rows = stream.Query().ToList();
|
||||
// Assert.Equal(exceldatareaderResult.Tables[0].Rows.Count, rows.Count);
|
||||
// foreach (IDictionary<string, object> row in rows)
|
||||
// {
|
||||
// var rowIndex = rows.IndexOf(row);
|
||||
// var keys = row.Keys;
|
||||
// foreach (var key in keys)
|
||||
// {
|
||||
// var eV = exceldatareaderResult.Tables[0].Rows[rowIndex][int.Parse(key)];
|
||||
// var v = row[key] == null ? DBNull.Value : row[key];
|
||||
// Assert.Equal(eV, v);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
[Fact()]
|
||||
public void CreateDataTableTest()
|
||||
|
Loading…
Reference in New Issue
Block a user