From 093ce71f2d549adcd4d9e1cadf5b3d17c14c171e Mon Sep 17 00:00:00 2001 From: wei Date: Thu, 22 Apr 2021 14:42:43 +0800 Subject: [PATCH] [Bug] Fixed MiniExcel.SaveAs(path, value,sheetName:"Name"), the actual sheetName is Sheet1 --- docs/README.md | 1 + docs/README.zh-CN.md | 1 + docs/README.zh-Hant.md | 1 + src/MiniExcel/Csv/CsvWriter.cs | 2 +- src/MiniExcel/IExcelWriter.cs | 2 +- src/MiniExcel/MiniExcel.cs | 8 +++++--- src/MiniExcel/OpenXml/DefualtOpenXml.cs | 6 +++--- src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs | 4 ++-- tests/MiniExcelTests/MiniExcelIssueTests.cs | 16 ++++++++++++++++ 9 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/README.md b/docs/README.md index b828fe33..edc0d5c6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,6 +10,7 @@ - [New] Support open with read only mode, avoid error of The process cannot access the file because it is being used by another process [#87](https://github.com/shps951023/MiniExcel/issues/87) - [Breaking Change] Change CSV SaveAs datetime default format : "yyyy-MM-dd HH:mm:ss" - [Bug] Fixed SaveAsByTemplate when merge cells will cause collection rendering error [#207](https://github.com/shps951023/MiniExcel/issues/207) +- [Bug] Fixed MiniExcel.SaveAs(path, value,sheetName:"Name"), the actual sheetName is Sheet1 ### 0.13.2 - [Bug] Fix Column more than 255 rows cannot be read error [#208](https://github.com/shps951023/MiniExcel/issues/208) diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index 86140c90..a690bc32 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -11,6 +11,7 @@ - [New] 支持 Excel 单纯读取模式,避免同时改模版又运行 MiniExcel 出现错误 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/87) - [Breaking Change] CSV SaveAs datetime 预设格式改为 "yyyy-MM-dd HH:mm:ss" - [Bug] 修正模版模式集合渲染遇到合并列会出现异常问题 [#207](https://github.com/shps951023/MiniExcel/issues/207) +- [Bug] 修正 MiniExcel.SaveAs(path, value,sheetName:"Name"), 实际 sheetName 是 Sheet1 ### 0.13.2 - [Bug] 超过 255 列无法读取错误 [#208](https://github.com/shps951023/MiniExcel/issues/208) diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 97866b18..70d3bbb7 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -11,6 +11,7 @@ - [New] 支持 Excel 單純讀取模式,避免同時改模版又運行 MiniExcel 出現錯誤 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/87) - [Breaking Change] CSV SaveAs datetime 預設格式改為 "yyyy-MM-dd HH:mm:ss" - [Bug] 修正模版模式集合渲染遇到合併列會出現異常問題 [#207](https://github.com/shps951023/MiniExcel/issues/207) +- [Bug] 修正 MiniExcel.SaveAs(path, value,sheetName:"Name"), 實際 sheetName 是 Sheet1 ### 0.13.2 - [Bug] 超過 255 列無法讀取錯誤 [#208](https://github.com/shps951023/MiniExcel/issues/208) diff --git a/src/MiniExcel/Csv/CsvWriter.cs b/src/MiniExcel/Csv/CsvWriter.cs index 2173f6a9..99c9c2b0 100644 --- a/src/MiniExcel/Csv/CsvWriter.cs +++ b/src/MiniExcel/Csv/CsvWriter.cs @@ -19,7 +19,7 @@ public CsvWriter(Stream stream) this._stream = stream; } - public void SaveAs(object value, bool printHeader, IConfiguration configuration) + public void SaveAs(object value, string sheetName, bool printHeader, IConfiguration configuration) { var cf = configuration == null ? CsvConfiguration.DefaultConfiguration : (CsvConfiguration)configuration; var seperator = cf.Seperator.ToString(); diff --git a/src/MiniExcel/IExcelWriter.cs b/src/MiniExcel/IExcelWriter.cs index 41256714..a2ae072d 100644 --- a/src/MiniExcel/IExcelWriter.cs +++ b/src/MiniExcel/IExcelWriter.cs @@ -4,6 +4,6 @@ namespace MiniExcelLibs { internal interface IExcelWriter { - void SaveAs(object value, bool printHeader, IConfiguration configuration); + void SaveAs(object value,string sheetName, bool printHeader, IConfiguration configuration); } } diff --git a/src/MiniExcel/MiniExcel.cs b/src/MiniExcel/MiniExcel.cs index 978b347f..6618a65a 100644 --- a/src/MiniExcel/MiniExcel.cs +++ b/src/MiniExcel/MiniExcel.cs @@ -9,7 +9,7 @@ public static partial class MiniExcel { - public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) + public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) { using (FileStream stream = new FileStream(path, FileMode.CreateNew)) SaveAs(stream, value, printHeader, sheetName, GetExcelType(path, excelType), configuration); @@ -18,11 +18,13 @@ public static void SaveAs(string path, object value, bool printHeader = true, st /// /// Default SaveAs Xlsx file /// - public static void SaveAs(this Stream stream, object value, bool printHeader = true, string sheetName = null, ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) + public static void SaveAs(this Stream stream, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null) { + if (string.IsNullOrEmpty(sheetName)) + throw new InvalidDataException("Sheet name can not be empty or null"); if (excelType == ExcelType.UNKNOWN) throw new InvalidDataException("Please specify excelType"); - ExcelWriterFactory.GetProvider(stream, excelType).SaveAs(value, printHeader, configuration); + ExcelWriterFactory.GetProvider(stream, excelType).SaveAs(value, sheetName, printHeader, configuration); } public static IEnumerable Query(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) where T : class, new() diff --git a/src/MiniExcel/OpenXml/DefualtOpenXml.cs b/src/MiniExcel/OpenXml/DefualtOpenXml.cs index b4510669..604511a3 100644 --- a/src/MiniExcel/OpenXml/DefualtOpenXml.cs +++ b/src/MiniExcel/OpenXml/DefualtOpenXml.cs @@ -51,7 +51,7 @@ internal static class DefualtOpenXml - + "; @@ -69,14 +69,14 @@ private static string MinifyXml(string xml) => xml ; //TODO:read from static generated file looks like more better? - internal static Dictionary GenerateDefaultOpenXml(ZipArchive archive) + internal static Dictionary GenerateDefaultOpenXml(ZipArchive archive,string sheetName) { var defaults = new Dictionary>() { { @"_rels/.rels", new Tuple(DefualtOpenXml.DefaultRels, "application/vnd.openxmlformats-package.relationships+xml")}, { @"xl/_rels/workbook.xml.rels", new Tuple(DefualtOpenXml.DefaultWorkbookXmlRels, "application/vnd.openxmlformats-package.relationships+xml")}, { @"xl/styles.xml", new Tuple(DefualtOpenXml.DefaultStylesXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")}, - { @"xl/workbook.xml", new Tuple(DefualtOpenXml.DefaultWorkbookXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")}, + { @"xl/workbook.xml", new Tuple(DefualtOpenXml.DefaultWorkbookXml.Replace("{{SheetName}}",sheetName), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")}, //{ @"xl/worksheets/sheet1.xml",new Tuple(DefualtOpenXml.DefaultSheetXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")}, }; diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index 6e3a093f..fee4a871 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -22,11 +22,11 @@ public ExcelOpenXmlSheetWriter(Stream stream) this._stream = stream; } - public void SaveAs(object value,bool printHeader, IConfiguration configuration) + public void SaveAs(object value, string sheetName, bool printHeader, IConfiguration configuration) { using (var archive = new MiniExcelZipArchive(_stream, ZipArchiveMode.Create, true, _utf8WithBom)) { - var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive); + var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive,sheetName); var sheetPath = "xl/worksheets/sheet1.xml"; { ZipArchiveEntry entry = archive.CreateEntry(sheetPath); diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index e4f5fb54..8144c742 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -23,6 +23,22 @@ public MiniExcelIssueTests(ITestOutputHelper output) this.output = output; } + /// + /// MiniExcel.SaveAs(path, table,sheetName:“Name”) ,the actual sheetName is Sheet1 + /// https://github.com/shps951023/MiniExcel/issues/212 + /// + [Fact] + public void Issue212() + { + var sheetName = "Demo"; + var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx"); + MiniExcel.SaveAs(path, new[] { new { x = 1, y = 2 } }, sheetName: sheetName); + + var actualSheetName = MiniExcel.GetSheetNames(path).ToList()[0]; + + Assert.Equal(sheetName, actualSheetName); + } + /// /// Version <= v0.13.1 Template merge row list rendering has no merge /// https://github.com/shps951023/MiniExcel/issues/207