简体   繁体   English

根据模板创建Excel报告

[英]Creating an Excel report based on template

I have an Excel template where the format for table header, data section and table footer are specified. 我有一个Excel模板,其中指定了表头,数据节和表脚的格式。 These sections might have images, merged cells, etc. The cells where data needs to be mapped are named cells. 这些部分可能具有图像,合并的单元格等。需要映射数据的单元格称为单元格。 I tried using the EPPlus library for generating the report based on the Excel template. 我尝试使用EPPlus库基于Excel模板生成报告。 I used the following snippet to copy the range of cells 我使用以下代码段复制了单元格范围

 var worksheet = destExcelPackage.Workbook.Worksheets.Add("Sheet 1");
 var sourceRange = sourceExcelPackage.Workbook.Worksheets.First().Cells["B6:P11"];
 sourceRange.Copy(worksheet.Cells["A1"]);

But this didn't make the column widths equal to the source. 但这并没有使列宽等于源宽度。 I had to set the column width to the source width as 我必须将列宽设置为源宽度为

var startCol = sourceRange.Start.Column;
var endCol = sourceRange.End.Column;

for (int j = startCol, destCol = 1; j <= endCol; j++, destCol++)
{
   worksheet.Column(destCol).Width = sourceExcelPackage.Workbook.Worksheets.First().Column(j).Width;
}

I have the following questions: 我有以下问题:


  1. Is there a better way to set the column width equal to the source? 是否有更好的方法将列宽设置为等于源宽度?
  2. The copied cells had an image, but it didn't get copy to the new sheet. 复制的单元格具有图像,但是没有将其复制到新的工作表中。 How to get the image copied? 如何复制图像?
  3. How to identify the named cells in the Excel sheet so that I can set value to the cell from some data source? 如何在Excel工作表中标识命名的单元格,以便可以从某些数据源为单元格设置值?

I have found a way to achieve points 2 and 3 above. 我已经找到一种实现上述第2点和第3点的方法。 It appears that if the picture is named, it is easy to read it. 看来,如果图片已命名,就很容易阅读。 So for #2 所以对于#2

    private static void CopyImage(ExcelPackage sourceExcelPackage, ExcelWorksheet destWorksheet)
    {
        var image = GetImage("Pic01", sourceExcelPackage);
        ExcelPicture pic = destWorksheet.Drawings.AddPicture("Pic01", image.Image);
        pic.From.Column = image.From.Column;
        pic.From.Row = image.From.Row;
        pic.To.Column = image.To.Column;
        pic.To.Row = image.To.Row;
        var destRow = 1;
        var destCol = 1;
        pic.SetPosition(destRow, Pixel2MTU(image.From.RowOff), destCol, Pixel2MTU(image.From.ColumnOff));
        pic.EditAs = eEditAs.TwoCell;
        pic.AdjustPositionAndSize();
    }

    private static ExcelPicture GetImage(string pictureName, ExcelPackage excelFile)
    {
        var sheet = excelFile.Workbook.Worksheets.First();
        var pic = sheet.Drawings[pictureName] as ExcelPicture;
        return pic;
    }

    private static int Pixel2MTU(int fromRowOff)
    {
        return fromRowOff / ExcelDrawing.EMU_PER_PIXEL;
    }

And for #3 对于#3

var cell = sourceExcelPackage.Workbook.Names?.Where(item => item.Name==headerName).FirstOrDefault();

Will return the cell which is named as headerName. 将返回名为headerName的单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM