Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EPPlus:将样式复制到某个范围_C#_Copy_Range_Epplus - Fatal编程技术网

C# EPPlus:将样式复制到某个范围

C# EPPlus:将样式复制到某个范围,c#,copy,range,epplus,C#,Copy,Range,Epplus,我想在工作表中插入x新行/列,并应用插入行/列的样式(背景色/边框等) 以下是我添加新行的方式: xlsSheet.InsertRow(18, RowCount); 然后,我想将“基本”行的样式复制/应用到新插入的行: for (int i = 0; i < RowCount; i++) { xlsSheet.Cells[16, 1, 16, xlsSheet.Dimension.End.Column].Copy(xlsSheet.Cells[16 + i + 1, 1]); }

我想在工作表中插入x新行/列,并应用插入行/列的样式(背景色/边框等)

以下是我添加新行的方式:

xlsSheet.InsertRow(18, RowCount);
然后,我想将“基本”行的样式复制/应用到新插入的行:

for (int i = 0; i < RowCount; i++)
{
    xlsSheet.Cells[16, 1, 16, xlsSheet.Dimension.End.Column].Copy(xlsSheet.Cells[16 + i + 1, 1]);
}
for(int i=0;i

但此代码不复制/应用“基本”行的样式。目前,我有一个interop解决方案,但与ePlus相比,这需要几年的时间

您应该这样定义工作表:

   string sheetName="Your Sheet Name";
   ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName);
然后可以使用以下代码更改整个图纸的样式:

      Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B8C9E9");
      ws.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
      ws.Cells.Style.Fill.BackgroundColor.SetColor(colFromHex);
      ws.Cells.Style.Border.Top.Style = ExcelBorderStyle.Medium;
      // . . . . .
并使用以下代码更改范围的样式:

       Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B8C9E9");
       ws.Cells["A1:H16"].Style.Fill.PatternType = ExcelFillStyle.Solid;
       ws.Cells["A1:H16"].Style.Fill.BackgroundColor.SetColor(colFromHex);
       ws.Cells["A1:H16"].Style.Border.Top.Style = ExcelBorderStyle.Medium;

我认为他们在版本4中破坏了部分复制功能。见此:

因此,可以在复制后手动设置样式ID:

[TestMethod]
public void Copy_Styles_Test()
{
    //http://stackoverflow.com/questions/31853046/epplus-copy-style-to-a-range

    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[] {new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (int)) });

    for (var i = 0; i < 20; i++)
    {
        var row = datatable.NewRow();
        row[0] = i; row[1] = i * 10; row[2] = i * 100; 
        datatable.Rows.Add(row);
    }

    var existingFile = new FileInfo(@"c:\temp\test.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var pck = new ExcelPackage(existingFile))
    {
        const int rowCount = 5;
        const int startRow = 18;

        //Show the data
        var xlsSheet = pck.Workbook.Worksheets.Add("Sheet1");
        xlsSheet.Cells.LoadFromDataTable(datatable, true);

        //Throw in some styles for testing
        xlsSheet.Row(startRow).Style.Fill.PatternType = ExcelFillStyle.Solid;
        xlsSheet.Row(startRow).Style.Fill.BackgroundColor.SetColor(Color.Aqua);
        xlsSheet.Cells[String.Format("A{0}:C{0}", startRow)].Style.Fill.BackgroundColor.SetColor(Color.Red);

        //Insert new rows
        xlsSheet.InsertRow(startRow, rowCount);

        //Copy the cells and manually set the style IDs
        var copyrow = startRow + rowCount;
        for (var i = 0; i < rowCount; i++)
        {
            var row = startRow + i;
            xlsSheet.Cells[String.Format("{0}:{0}", copyrow)].Copy(xlsSheet.Cells[String.Format("{0}:{0}", row)]);
            xlsSheet.Row(row).StyleID = xlsSheet.Row(copyrow).StyleID;
        }

        //May not be needed but cant hurt
        xlsSheet.Cells.Worksheet.Workbook.Styles.UpdateXml();

        //save it
        pck.Save();
    }
}
[TestMethod]
公共无效副本\u样式\u测试()
{
//http://stackoverflow.com/questions/31853046/epplus-copy-style-to-a-range
//加入一些数据
var数据表=新数据表(“tblData”);
datatable.Columns.AddRange(new[]{new DataColumn(“Col1”,typeof(int)),new DataColumn(“Col2”,typeof(int)),new DataColumn(“Col3”,typeof(int))});
对于(变量i=0;i<20;i++)
{
var row=datatable.NewRow();
行[0]=i;行[1]=i*10;行[2]=i*100;
datatable.Rows.Add(行);
}
var existingFile=new FileInfo(@“c:\temp\test.xlsx”);
if(existingFile.Exists)
existingFile.Delete();
使用(var pck=新的ExcelPackage(现有文件))
{
const int rowCount=5;
常数int startRow=18;
//显示数据
var xlsSheet=pck.Workbook.Worksheets.Add(“Sheet1”);
xlsSheet.Cells.LoadFromDataTable(datatable,true);
//加入一些测试样式
xlsSheet.Row(startRow).Style.Fill.PatternType=ExcelFillStyle.Solid;
xlsSheet.Row(startRow).Style.Fill.BackgroundColor.SetColor(Color.Aqua);
xlsSheet.Cells[String.Format(“A{0}:C{0}”,startRow)].Style.Fill.BackgroundColor.SetColor(Color.Red);
//插入新行
xlsSheet.InsertRow(startRow,rowCount);
//复制单元格并手动设置样式ID
var copyrow=startRow+rowCount;
对于(变量i=0;i
在4.0.4代码中:

if (copyStylesFromRow > 0)
{
    var cseS = new CellsStoreEnumerator<int>(_styles, copyStylesFromRow, 0, copyStylesFromRow, ExcelPackage.MaxColumns); //Fixes issue 15068 , 15090
    while(cseS.Next())
    {
        for (var r = 0; r < rows; r++)
        {
            _styles.SetValue(rowFrom + r, cseS.Column, cseS.Value);
        }
    }
}

这将从第3行开始插入4个新行,因为包含第3行,所以必须指向第6行。这是一个错误,但你可以解释它。

范围内有5-6种不同的颜色和单元格样式,这就是我希望应用范围内样式的原因。谢谢厄尼,我将尝试一下!
workbook.Worksheets[1].InsertRow(3,4,6);