简体   繁体   English

EPPlus 将工作表从 Workbook1 复制到 Workbook2

[英]EPPlus To Copy Worksheet From Workbook1 to Workbook2

I have a template workbook with a sheet called ProdData , I need to copy this worksheet to my current workbook.我有一个模板工作簿,其中包含一个名为ProdData的工作表,我需要将此工作表复制到我当前的工作簿中。

Using C# and EPPlus, how can I copy a worksheet from one workbook to another?使用 C# 和 EPPlus,如何将工作表从一个工作簿复制到另一个工作簿? When I look at intellisense it seems to only show that I can copy from within the same workbook.当我查看 intellisense 时,它似乎只表明我可以从同一工作簿中进行复制。

视觉工作室

How do I copy the worksheet to a NEW workbook?如何将工作表复制到工作簿?

This works for me.这对我有用。

     public static void CopySheetValues(string sourcePath, string sheetName, string destPath)
    {
        using (var src = new ExcelPackage(new FileInfo(sourcePath)))
        using (var dest = new ExcelPackage(new FileInfo(destPath)))
        {
            var wsSrc = src.Workbook.Worksheets[1];
            var wsDest = dest.Workbook.Worksheets[wsSrc.Name] ?? dest.Workbook.Worksheets.Add(wsSrc.Name);

            for (var r = 1; r <= wsSrc.Dimension.Rows; r++)
            {
                Console.WriteLine("Row: " + r.ToString());
                for (var c = 1; c <= wsSrc.Dimension.Columns; c++) 
                {
                    Console.WriteLine("Column:  " + c.ToString());
                    var cellSrc = wsSrc.Cells[r, c];
                    var cellDest = wsDest.Cells[r, c];
                    if (cellDest.ToString() == "E10")
                    {

                    }
                    Console.WriteLine(cellDest.ToString());
                    // Copy value
                    cellDest.Value = cellSrc.Value;

                    // Copy cell properties
                    cellDest.Style.Numberformat = cellSrc.Style.Numberformat;
                    cellDest.Style.Font.Bold = cellSrc.Style.Font.Bold;

                    if (cellSrc.Style.Fill.BackgroundColor.Rgb != null)
                    {
                        if (cellSrc.Style.Fill.BackgroundColor.Rgb != "")
                        {

                            var color = cellSrc.Style.Fill.BackgroundColor.Rgb;
                            cellDest.Style.Fill.PatternType = ExcelFillStyle.Solid;

                            cellDest.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#" + color));//.SetColor(color);

                        }
                        else
                        {
                            
                            cellDest.Style.Fill.PatternType = ExcelFillStyle.Solid;

                            cellDest.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#808080"));//.SetColor(color);

                        }
                    }// TODO... Add any additional properties that you may want to copy over
                    cellDest.Style.HorizontalAlignment = 
                    cellSrc.Style.HorizontalAlignment;
                    cellDest.Style.VerticalAlignment = 
                    cellSrc.Style.VerticalAlignment;
                    cellDest.Style.Border.Right.Style = 
                    cellSrc.Style.Border.Right.Style;
                    cellDest.Style.Border.Left.Style = 
                    cellSrc.Style.Border.Left.Style;
                    cellDest.Style.Border.Top.Style = cellSrc.Style.Border.Top.Style;
                    cellDest.Style.Border.Bottom.Style = 
                    cellSrc.Style.Border.Bottom.Style;
                    cellDest.Style.WrapText = cellSrc.Style.WrapText;


                }
            }

            dest.Save();
        }
    }

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

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