Skip to content

Commit 92f6e4f

Browse files
committed
#250 Add image offset support
1 parent dccc1a6 commit 92f6e4f

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

docs/8. Import and export Excel as pictures.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ This chapter of the tutorial explains how to use Magicodes.IE.Excel to import an
1616
+ **Height**: Height(Default 15)
1717
+ **Width**:Width(Default 50)
1818
+ **Alt**:Replace text when image does not exist
19+
+ **YOffset**:垂直偏移(可进行移动图片)
20+
+ **XOffset**:水平偏移(可进行移动图片)
1921

2022
### ImportImageFieldAttribute
2123

docs/8.Excel图片导入导出.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
+ **Height**: 高度(默认15)
1717
+ **Width**:宽度(默认50)
1818
+ **Alt**:图片不存在时替换文本
19+
+ **YOffset**:垂直偏移(可进行移动图片)
20+
+ **XOffset**:水平偏移(可进行移动图片)
21+
1922

2023
### ImportImageFieldAttribute
2124

src/Magicodes.ExporterAndImporter.Core/Fields/ExportImageFieldAttribute.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ public class ExportImageFieldAttribute : Attribute
2121
/// 图片不存在时的替代文本
2222
/// </summary>
2323
public string Alt { get; set; }
24+
25+
/// <summary>
26+
/// 垂直偏移
27+
/// </summary>
28+
public int YOffset { get; set; } = 0;
29+
30+
/// <summary>
31+
/// 水平偏移
32+
/// </summary>
33+
public int XOffset { get; set; } = 0;
34+
2435
/// <summary>
2536
/// </summary>
2637
/// <param name="height"></param>

src/Magicodes.ExporterAndImporter.Excel/Utility/ExportHelper.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Magicodes.ExporterAndImporter.Core.Filters;
44
using Magicodes.ExporterAndImporter.Core.Models;
55
using OfficeOpenXml;
6+
using OfficeOpenXml.Drawing;
67
using OfficeOpenXml.Style;
78
using OfficeOpenXml.Table;
89
using System;
@@ -756,7 +757,13 @@ protected void AddPictures(int rowCount)
756757
else
757758
{
758759
var pic = CurrentExcelWorksheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bitmap);
759-
pic.SetPosition(rowIndex + (ExcelExporterSettings.HeaderRowIndex > 1 ? ExcelExporterSettings.HeaderRowIndex : 0), ExporterHeaderList[colIndex].ExportImageFieldAttribute.Height / 5, colIndex - ignoreCount, 0);
760+
AddImage((rowIndex + (ExcelExporterSettings.HeaderRowIndex > 1 ? ExcelExporterSettings.HeaderRowIndex : 0)),
761+
colIndex - ignoreCount, pic, ExporterHeaderList[colIndex].ExportImageFieldAttribute.YOffset, ExporterHeaderList[colIndex].ExportImageFieldAttribute.XOffset);
762+
763+
//pic.SetPosition
764+
// (rowIndex + (ExcelExporterSettings.HeaderRowIndex > 1 ? ExcelExporterSettings.HeaderRowIndex : 0),
765+
// ExporterHeaderList[colIndex].ExportImageFieldAttribute.Height / 5, colIndex - ignoreCount, 0);
766+
760767
CurrentExcelWorksheet.Row(rowIndex + 1).Height = ExporterHeaderList[colIndex].ExportImageFieldAttribute.Height;
761768
//pic.SetSize(ExporterHeaderList[colIndex].ExportImageFieldAttribute.Width * 7, ExporterHeaderList[colIndex].ExportImageFieldAttribute.Height);
762769
pic.SetSize(ExporterHeaderList[colIndex].ExportImageFieldAttribute.Width * 7, ExporterHeaderList[colIndex].ExportImageFieldAttribute.Height);
@@ -785,6 +792,24 @@ protected void AddPictures(int rowCount)
785792
}
786793
}
787794

795+
private void AddImage(int rowIndex, int colIndex, ExcelPicture picture, int yOffset, int xOffset)
796+
{
797+
if (picture != null)
798+
{
799+
picture.From.Column = colIndex;
800+
picture.From.Row = rowIndex;
801+
//调整对齐
802+
picture.From.ColumnOff = Pixel2MTU(xOffset);
803+
picture.From.RowOff = Pixel2MTU(yOffset);
804+
}
805+
}
806+
807+
private int Pixel2MTU(int pixels)
808+
{
809+
int mtus = pixels * 9525;
810+
return mtus;
811+
}
812+
788813

789814
/// <summary>
790815
///

src/Magicodes.ExporterAndImporter.Tests/Models/Export/ExportTestDataWithPricture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ExportTestDataWithPicture
1414
[ExporterHeader(DisplayName = "忽略", IsIgnore = true)]
1515
public string Text3 { get; set; }
1616

17-
[ExportImageField(Width = 20, Height = 120)]
17+
[ExportImageField(Width = 20, Height = 120, YOffset = 15)]
1818
[ExporterHeader(DisplayName = "图1")]
1919
public string Img1 { get; set; }
2020
[ExporterHeader(DisplayName = "数值", Format = "#,##0")]

0 commit comments

Comments
 (0)