[Bug] Fixed MiniExcel.SaveAs(path, value,sheetName:"Name"), the actual sheetName is Sheet1

This commit is contained in:
wei 2021-04-22 14:42:43 +08:00
parent 760bd5e851
commit 093ce71f2d
9 changed files with 31 additions and 10 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -19,7 +19,7 @@ namespace MiniExcelLibs.Csv
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();

View File

@ -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);
}
}

View File

@ -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 @@
/// <summary>
/// Default SaveAs Xlsx file
/// </summary>
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<T> Query<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) where T : class, new()

View File

@ -51,7 +51,7 @@ namespace MiniExcelLibs.OpenXml
<x:workbook xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""
xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main"">
<x:sheets>
<x:sheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" name=""Sheet1"" sheetId=""1"" r:id=""R1274d0d920f34a32"" />
<x:sheet xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships"" name=""{{SheetName}}"" sheetId=""1"" r:id=""R1274d0d920f34a32"" />
</x:sheets>
</x:workbook>";
@ -69,14 +69,14 @@ namespace MiniExcelLibs.OpenXml
;
//TODO:read from static generated file looks like more better?
internal static Dictionary<string, ZipPackageInfo> GenerateDefaultOpenXml(ZipArchive archive)
internal static Dictionary<string, ZipPackageInfo> GenerateDefaultOpenXml(ZipArchive archive,string sheetName)
{
var defaults = new Dictionary<string, Tuple<string, string>>()
{
{ @"_rels/.rels", new Tuple<string,string>(DefualtOpenXml.DefaultRels, "application/vnd.openxmlformats-package.relationships+xml")},
{ @"xl/_rels/workbook.xml.rels", new Tuple<string,string>(DefualtOpenXml.DefaultWorkbookXmlRels, "application/vnd.openxmlformats-package.relationships+xml")},
{ @"xl/styles.xml", new Tuple<string,string>(DefualtOpenXml.DefaultStylesXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")},
{ @"xl/workbook.xml", new Tuple<string,string>(DefualtOpenXml.DefaultWorkbookXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")},
{ @"xl/workbook.xml", new Tuple<string,string>(DefualtOpenXml.DefaultWorkbookXml.Replace("{{SheetName}}",sheetName), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")},
//{ @"xl/worksheets/sheet1.xml",new Tuple<string,string>(DefualtOpenXml.DefaultSheetXml, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")},
};

View File

@ -22,11 +22,11 @@ namespace MiniExcelLibs.OpenXml
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);

View File

@ -23,6 +23,22 @@ namespace MiniExcelLibs.Tests
this.output = output;
}
/// <summary>
/// MiniExcel.SaveAs(path, table,sheetName:“Name”) the actual sheetName is Sheet1
/// https://github.com/shps951023/MiniExcel/issues/212
/// </summary>
[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);
}
/// <summary>
/// Version <= v0.13.1 Template merge row list rendering has no merge
/// https://github.com/shps951023/MiniExcel/issues/207