mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
[New] SaveAs support image #304
This commit is contained in:
parent
fb30be7bd3
commit
45e4a57c20
21
README.md
21
README.md
@ -469,6 +469,27 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi
|
||||
|
||||
|
||||
|
||||
#### 10. Create Image
|
||||
|
||||
```csharp
|
||||
var value = new[] {
|
||||
new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))},
|
||||
new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))},
|
||||
new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))},
|
||||
new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))},
|
||||
new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))},
|
||||
};
|
||||
MiniExcel.SaveAs(path, value);
|
||||
```
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### Fill Data To Excel Template <a name="getstart3"></a>
|
||||
|
@ -473,6 +473,29 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi
|
||||
|
||||
|
||||
|
||||
#### 10. 图片生成
|
||||
|
||||
```csharp
|
||||
var value = new[] {
|
||||
new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))},
|
||||
new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))},
|
||||
new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))},
|
||||
new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))},
|
||||
new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))},
|
||||
};
|
||||
MiniExcel.SaveAs(path, value);
|
||||
```
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 模板填充 Excel <a name="getstart3"></a>
|
||||
|
@ -479,6 +479,27 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi
|
||||
|
||||
|
||||
|
||||
#### 10. 圖片生成
|
||||
|
||||
```csharp
|
||||
var value = new[] {
|
||||
new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))},
|
||||
new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))},
|
||||
new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))},
|
||||
new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))},
|
||||
new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))},
|
||||
};
|
||||
MiniExcel.SaveAs(path, value);
|
||||
```
|
||||
|
||||
![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.20.0
|
||||
- [New] SaveAs support image #304
|
||||
|
||||
### 0.19.3-beta
|
||||
- [Fix] Excelnumberformat 1.1.0 valid date expired (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34)
|
||||
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.20.0
|
||||
- [New] SaveAs 支持图片生成 #304
|
||||
|
||||
### 0.19.3-beta
|
||||
- [Fix] Excelnumberformat 1.1.0 凭证过期 (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34)
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
|
||||
---
|
||||
|
||||
### 0.20.0
|
||||
- [New] SaveAs 支持圖片生成 #304
|
||||
|
||||
### 0.19.3-beta
|
||||
- [Fix] Excelnumberformat 1.1.0 憑證過期 (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34)
|
||||
|
BIN
samples/images/google_logo.png
Normal file
BIN
samples/images/google_logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 557 B |
BIN
samples/images/microsoft_logo.png
Normal file
BIN
samples/images/microsoft_logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 198 B |
BIN
samples/images/reddit_logo.png
Normal file
BIN
samples/images/reddit_logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 754 B |
BIN
samples/images/statck_overflow_logo.png
Normal file
BIN
samples/images/statck_overflow_logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 440 B |
@ -15,18 +15,20 @@ namespace MiniExcelLibs.OpenXml
|
||||
{
|
||||
internal class ImageDto
|
||||
{
|
||||
public string ID { get; set; } = $"R{Guid.NewGuid().ToString("N")}".Substring(0,5);
|
||||
public string ID { get; set; } = $"R{Guid.NewGuid().ToString("N")}";
|
||||
public string Extension { get; set; }
|
||||
public string Path { get { return $"xl/media/image{ID}.{Extension}"; }}
|
||||
public string Path { get { return $"xl/media/image{ID}.{Extension}"; } }
|
||||
public string Path2 { get { return $"/xl/media/image{ID}.{Extension}"; } }
|
||||
public Byte[] Byte { get; set; }
|
||||
public int RowIndex { get; set; }
|
||||
public int CellIndex { get; set; }
|
||||
}
|
||||
internal class SheetDto
|
||||
{
|
||||
public string ID { get; set; } = $"R{Guid.NewGuid().ToString("N")}";
|
||||
public string Name { get; set; }
|
||||
public int SheetIdx { get; set; }
|
||||
public string Path { get { return $"xl/worksheets/sheet{SheetIdx}.xml"; } }
|
||||
public string Path { get { return $"xl/worksheets/sheet{SheetIdx}.xml"; } }
|
||||
}
|
||||
internal class DrawingDto
|
||||
{
|
||||
@ -49,7 +51,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
this._configuration = configuration as OpenXmlConfiguration ?? OpenXmlConfiguration.DefaultConfig;
|
||||
this._printHeader = printHeader;
|
||||
this._value = value;
|
||||
_sheets.Add(new SheetDto{ Name =sheetName, SheetIdx =1}); //TODO:remove
|
||||
_sheets.Add(new SheetDto { Name = sheetName, SheetIdx = 1 }); //TODO:remove
|
||||
}
|
||||
|
||||
public void SaveAs()
|
||||
@ -344,7 +346,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCell(StreamWriter writer, int yIndex, int cellIndex, object value, ExcelCustomPropertyInfo p)
|
||||
private void WriteCell(StreamWriter writer, int rowIndex, int cellIndex, object value, ExcelCustomPropertyInfo p)
|
||||
{
|
||||
var v = string.Empty;
|
||||
var t = "str";
|
||||
@ -400,11 +402,14 @@ namespace MiniExcelLibs.OpenXml
|
||||
{
|
||||
//it can't insert to zip first to avoid cache image to memory
|
||||
//because sheet xml is opening.. https://github.com/shps951023/MiniExcel/issues/304#issuecomment-1017031691
|
||||
//int rowIndex, int cellIndex
|
||||
_images.Add(new ImageDto()
|
||||
{
|
||||
Extension = format.ToString(),
|
||||
Byte = bytes,
|
||||
});
|
||||
RowIndex = rowIndex,
|
||||
CellIndex = cellIndex
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -428,7 +433,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
}
|
||||
}
|
||||
|
||||
var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, yIndex);
|
||||
var columname = ExcelOpenXmlUtils.ConvertXyToCell(cellIndex, rowIndex);
|
||||
if (v != null && (v.StartsWith(" ") || v.EndsWith(" "))) /*Prefix and suffix blank space will lost after SaveAs #294*/
|
||||
writer.Write($"<x:c r=\"{columname}\" {(t == null ? "" : $"t =\"{t}\"")} s=\"{s}\" xml:space=\"preserve\"><x:v>{v}</x:v></x:c>");
|
||||
else
|
||||
@ -582,15 +587,15 @@ namespace MiniExcelLibs.OpenXml
|
||||
{
|
||||
drawing.Append($@"<xdr:oneCellAnchor>
|
||||
<xdr:from>
|
||||
<xdr:col>0</xdr:col>
|
||||
<xdr:col>{i.CellIndex- 1/* why -1 : https://user-images.githubusercontent.com/12729184/150460189-f08ed939-44d4-44e1-be6e-9c533ece6be8.png*/}</xdr:col>
|
||||
<xdr:colOff>0</xdr:colOff>
|
||||
<xdr:row>0</xdr:row>
|
||||
<xdr:row>{i.RowIndex-1}</xdr:row>
|
||||
<xdr:rowOff>0</xdr:rowOff>
|
||||
</xdr:from>
|
||||
<xdr:ext cx=""609600"" cy=""190500"" />
|
||||
<xdr:pic>
|
||||
<xdr:nvPicPr>
|
||||
<xdr:cNvPr id=""{_images.IndexOf(i)+1}"" descr="""" name=""2a3f9147-58ea-4a79-87da-7d6114c4877b"" />
|
||||
<xdr:cNvPr id=""{_images.IndexOf(i) + 1}"" descr="""" name=""2a3f9147-58ea-4a79-87da-7d6114c4877b"" />
|
||||
<xdr:cNvPicPr>
|
||||
<a:picLocks noChangeAspect=""1"" />
|
||||
</xdr:cNvPicPr>
|
||||
@ -641,7 +646,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
CreateZipEntry(@"xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
|
||||
_defaultWorkbookXml.Replace("{{sheets}}", workbookXml.ToString()));
|
||||
CreateZipEntry(@"xl/_rels/workbook.xml.rels", "",
|
||||
_defaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString()));
|
||||
_defaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString()));
|
||||
}
|
||||
|
||||
//[Content_Types].xml
|
||||
|
@ -34,15 +34,23 @@ namespace MiniExcelLibs.Tests
|
||||
[Fact]
|
||||
public void TestIssue304()
|
||||
{
|
||||
var imagePath = PathHelper.GetFile("images/github_logo.png");
|
||||
var image = File.ReadAllBytes(imagePath);
|
||||
var value = Enumerable.Range(1, 5).Select(s => new { image });
|
||||
var path = PathHelper.GetRandomPath();
|
||||
var path = PathHelper.GetTempFilePath();
|
||||
var value = new[] {
|
||||
new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))},
|
||||
new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))},
|
||||
new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))},
|
||||
new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))},
|
||||
new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))},
|
||||
};
|
||||
MiniExcel.SaveAs(path, value);
|
||||
|
||||
|
||||
//TODO: Read from base 64 not work
|
||||
//var image = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAAEElEQVR4nGJgAQAAAP//AwAABgAFV7+r1AAAAABJRU5ErkJggg==");
|
||||
{
|
||||
Assert.Contains("/xl/media/image", Helpers.GetZipFileContent(path, "xl/drawings/_rels/drawing1.xml.rels"));
|
||||
Assert.Contains("ext cx=\"609600\" cy=\"190500\"", Helpers.GetZipFileContent(path, "xl/drawings/drawing1.xml"));
|
||||
Assert.Contains("/xl/drawings/drawing1.xml", Helpers.GetZipFileContent(path, "[Content_Types].xml"));
|
||||
Assert.Contains("drawing r:id=", Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml"));
|
||||
Assert.Contains("../drawings/drawing1.xml", Helpers.GetZipFileContent(path, "xl/worksheets/_rels/sheet1.xml.rels"));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -20,7 +20,7 @@
|
||||
return path;
|
||||
}
|
||||
|
||||
public static string GetRandomPath(string extension = "xlsx")
|
||||
public static string GetTempFilePath(string extension = "xlsx")
|
||||
{
|
||||
return Path.GetTempPath() + Guid.NewGuid().ToString() + "." + extension;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user