EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.8k stars 274 forks source link

copyed lines no longer horizontal #1627

Closed 93910676wangd closed 1 week ago

93910676wangd commented 1 week ago

EPPlus usage

Noncommercial use

Environment

windows

Epplus version

7.2.0

Spreadsheet application

excel

Description

An Excel file contains horizontal lines drawn using the 'Freeform' shape, In version 7.1.3 of Epplus , Using the 'ExcelRangeBase.Copy' method to copy rows can only copy the content of the row, not the shape within the row. In version 7.2.0 of Epplus, Using the 'ExcelRangeBase.Copy' method copy rows can copy both the content of a row and the shape inside the row together, but the shape are offset (the horizontal line becomes no longer horizontal).

The issue of graphic deviation seems to have been raised and resolved by someone in a previous issue(https://github.com/EPPlusSoftware/EPPlus/issues/689). Why did the offset issue occur again in version 7.2.0.

sample src:EPPlusLinuxApp.zip

image

OssianEPPlus commented 1 week ago

This doesn't appear to be a fault in Epplus but rather with the shape and the extension function.

Your original file/rows contains line shapes that do not scale correctly. For example: bild

If we then set the height to 2cm: bild The line becomes non-horizontal. Because that is how Excel's line shape interprets height. The lines, before they are copied exhibit strange behaviour.

Then, your extension method:

static class ExcelWorksheetExtension
    {
        public static void CopyRows(this ExcelWorksheet excelWorksheet, int sourceFrom, int sourceTo, int destFrom, int destTo)
        {
            excelWorksheet.Cells[sourceFrom.ToString() + ":" + sourceTo].Copy(
                excelWorksheet.Cells[destFrom.ToString() + ":" + destTo]);

            for (int i = destFrom; i <= destTo; i++)
            {
                excelWorksheet.Row(i).Height = excelWorksheet.Row(sourceFrom + i - destFrom).Height;
            }
        }
    }

You first copy the row data, including the lines. And then apply height. So you copy lines from a row with a larger height, where the freeform shape is close to outside the cell and then apply it to a cell with a smaller height where the line does not fit. Then change the height of the row. This makes EPPlus try to scale the freeformshape with the cell and since the shape responds strangely to height, you get a strange result.

Your extension method should scale the target row to the correct height and Then copy the data into it to get the result you want. Like so:

    static class ExcelWorksheetExtension
    {
        public static void CopyRows(this ExcelWorksheet excelWorksheet, int sourceFrom, int sourceTo, int destFrom, int destTo)
        {
            for (int i = destFrom; i <= destTo; i++)
            {
                excelWorksheet.Row(i).Height = excelWorksheet.Row(sourceFrom + i - destFrom).Height;
            }

            excelWorksheet.Cells[sourceFrom.ToString() + ":" + sourceTo].Copy(
                excelWorksheet.Cells[destFrom.ToString() + ":" + destTo]);
        }
    }

This seems to get the result you're looking for. This appears unrelated to the issue you've reported previously.