Skip to content

Commit

Permalink
[Bug] Fixed MiniExcel.SaveAs(path, value,sheetName:"Name"), the actua…
Browse files Browse the repository at this point in the history
…l sheetName is Sheet1
  • Loading branch information
shps951023 committed Apr 22, 2021
1 parent 760bd5e commit 093ce71
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/Csv/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/IExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 5 additions & 3 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -18,11 +18,13 @@ public static void SaveAs(string path, object value, bool printHeader = true, st
/// <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()
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/OpenXml/DefualtOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal static class DefualtOpenXml
<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>";

Expand All @@ -69,14 +69,14 @@ private static string MinifyXml(string xml) => xml
;

//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")},
};

Expand Down
4 changes: 2 additions & 2 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public MiniExcelIssueTests(ITestOutputHelper output)
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
Expand Down

0 comments on commit 093ce71

Please sign in to comment.