Skip to content

Commit 68c60f9

Browse files
authored
[New] MiniExcel AddPicture (#770)
* [New] MiniExcel AddPicture * Support default sheet entry and spesific sheet name * Support CellAddress and default WidthPx, HeightPx * [doc] MiniExcel.AddPicture
1 parent 536b301 commit 68c60f9

File tree

11 files changed

+456
-4
lines changed

11 files changed

+456
-4
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,10 +1488,30 @@ var config = new OpenXmlConfiguration() { FastMode = true };
14881488
MiniExcel.SaveAs(path, reader,configuration:config);
14891489
```
14901490

1491+
#### 6. Batch Add Image (MiniExcel.AddPicture)
14911492

1492-
1493-
1494-
1493+
```csharp
1494+
var images = new[]
1495+
{
1496+
new MiniExcelPicture
1497+
{
1498+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png")),
1499+
SheetName = null, // default null is first sheet
1500+
CellAddress = "C3", // required
1501+
},
1502+
new MiniExcelPicture
1503+
{
1504+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png")),
1505+
PictureType = "image/png", // default PictureType = image/png
1506+
SheetName = "Demo",
1507+
CellAddress = "C9", // required
1508+
WidthPx = 100,
1509+
HeightPx = 100,
1510+
},
1511+
};
1512+
MiniExcel.AddPicture(path, images);
1513+
```
1514+
![Image](https://github.com/user-attachments/assets/19c4d241-9753-4ede-96c8-f810c1a22247)
14951515

14961516
### Examples:
14971517

README.zh-CN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,30 @@ public class Order
16311631

16321632

16331633

1634+
#### 6. 批量添加/插入图片 (MiniExcel.AddPicture)
16341635

1636+
```csharp
1637+
var images = new[]
1638+
{
1639+
new MiniExcelPicture
1640+
{
1641+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png")),
1642+
SheetName = null, // default null is first sheet
1643+
CellAddress = "C3", // required
1644+
},
1645+
new MiniExcelPicture
1646+
{
1647+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png")),
1648+
PictureType = "image/png", // default PictureType = image/png
1649+
SheetName = "Demo",
1650+
CellAddress = "C9", // required
1651+
WidthPx = 100,
1652+
HeightPx = 100,
1653+
},
1654+
};
1655+
MiniExcel.AddPicture(path, images);
1656+
```
1657+
![Image](https://github.com/user-attachments/assets/19c4d241-9753-4ede-96c8-f810c1a22247)
16351658

16361659

16371660

README.zh-Hant.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,33 @@ var config = new OpenXmlConfiguration() { FastMode = true };
13631363
MiniExcel.SaveAs(path, reader,configuration:config);
13641364
```
13651365

1366+
#### 6. 批量添加/插入圖片 (MiniExcel.AddPicture)
1367+
1368+
```csharp
1369+
var images = new[]
1370+
{
1371+
new MiniExcelPicture
1372+
{
1373+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png")),
1374+
SheetName = null, // default null is first sheet
1375+
CellAddress = "C3", // required
1376+
},
1377+
new MiniExcelPicture
1378+
{
1379+
ImageBytes = File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png")),
1380+
PictureType = "image/png", // default PictureType = image/png
1381+
SheetName = "Demo",
1382+
CellAddress = "C9", // required
1383+
WidthPx = 100,
1384+
HeightPx = 100,
1385+
},
1386+
};
1387+
MiniExcel.AddPicture(path, images);
1388+
```
1389+
1390+
![Image](https://github.com/user-attachments/assets/19c4d241-9753-4ede-96c8-f810c1a22247)
1391+
1392+
13661393

13671394

13681395
### 範例
8.19 KB
Binary file not shown.
35.6 KB
Binary file not shown.

samples/xlsx/TestIssueIBAJXH.xlsx

8.52 KB
Binary file not shown.

src/MiniExcel/MiniExcel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MiniExcelLibs
22
{
3+
using MiniExcelLibs.Picture;
34
using OpenXml;
45
using System;
56
using System.Collections;
@@ -13,6 +14,15 @@
1314

1415
public static partial class MiniExcel
1516
{
17+
public static void AddPicture(string path, params MiniExcelPicture[] images)
18+
{
19+
using (var stream = File.Open(path,FileMode.OpenOrCreate))
20+
MiniExcelPictureImplement.AddPicture(stream, images);
21+
}
22+
public static void AddPicture(Stream excelStream, params MiniExcelPicture[] images)
23+
{
24+
MiniExcelPictureImplement.AddPicture(excelStream, images);
25+
}
1626
public static MiniExcelDataReader GetReader(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = "A1", IConfiguration configuration = null)
1727
{
1828
var stream = FileHelper.OpenSharedRead(path);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using MiniExcelLibs.Utils;
2+
3+
namespace MiniExcelLibs.Picture
4+
{
5+
public class MiniExcelPicture
6+
{
7+
public byte[] ImageBytes { get; set; }
8+
public string SheetName { get; set; }
9+
public string PictureType { get; set; }
10+
public string CellAddress { get; set; }
11+
internal int ColumnNumber => ReferenceHelper.ConvertCellToXY(CellAddress).Item1 -1;
12+
internal int RowNumber => ReferenceHelper.ConvertCellToXY(CellAddress).Item2 - 1;
13+
public int WidthPx { get; set; }
14+
public int HeightPx { get; set; }
15+
}
16+
}

src/MiniExcel/Picture/MiniExcelPictureImplement.cs

Lines changed: 325 additions & 0 deletions
Large diffs are not rendered by default.

src/MiniExcel/SaveByTemplate/InputValueExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using MiniExcelLibs.Utils;
66

77
namespace MiniExcelLibs.OpenXml.SaveByTemplate
8-
{
8+
{
99
public class InputValueExtractor : IInputValueExtractor
1010
{
1111
public IDictionary<string, object> ToValueDictionary(object valueObject)

0 commit comments

Comments
 (0)