EPPlus:将样式复制到一个范围
EPPlus: Copy Style to a range
我想将 x-new rows/columns 插入工作表并应用插入的 row/column 的样式(backgroundcolor/border 等)。
这是我添加新行的方式:
xlsSheet.InsertRow(18, RowCount);
然后我想 copy/apply 将 "base" 行的样式添加到新插入的行中:
for (int i = 0; i < RowCount; i++)
{
xlsSheet.Cells[16, 1, 16, xlsSheet.Dimension.End.Column].Copy(xlsSheet.Cells[16 + i + 1, 1]);
}
但是这段代码没有 copy/apply "base" 行的样式。目前我有一个互操作的解决方法,但与 epplus 相比这需要数年时间。 :-/
你应该这样定义一个作品sheet :
string sheetName="Your Sheet Name";
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName);
然后就可以使用下面的代码来改变整个sheet的样式了:
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 中破坏了那部分复制功能。请看:
http://epplus.codeplex.com/workitem/15068
因此,可以在复制后手动设置样式 ID:
[TestMethod]
public void Copy_Styles_Test()
{
//
//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();
}
}
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);
}
}
}
它使用 copyStylesFromRow 值,但由于代码顺序,它使用新的行号。因此,如果您想从第 3 行开始插入 4 行:
workbook.Worksheets[1].InsertRow(3,4,6);
这将从第 3 行开始插入 4 个新行,因为包含第 3 行,您必须指向第 6 行。这是一个错误,但您可以解决它。
我想将 x-new rows/columns 插入工作表并应用插入的 row/column 的样式(backgroundcolor/border 等)。
这是我添加新行的方式:
xlsSheet.InsertRow(18, RowCount);
然后我想 copy/apply 将 "base" 行的样式添加到新插入的行中:
for (int i = 0; i < RowCount; i++)
{
xlsSheet.Cells[16, 1, 16, xlsSheet.Dimension.End.Column].Copy(xlsSheet.Cells[16 + i + 1, 1]);
}
但是这段代码没有 copy/apply "base" 行的样式。目前我有一个互操作的解决方法,但与 epplus 相比这需要数年时间。 :-/
你应该这样定义一个作品sheet :
string sheetName="Your Sheet Name";
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(sheetName);
然后就可以使用下面的代码来改变整个sheet的样式了:
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 中破坏了那部分复制功能。请看:
http://epplus.codeplex.com/workitem/15068
因此,可以在复制后手动设置样式 ID:
[TestMethod]
public void Copy_Styles_Test()
{
//
//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();
}
}
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);
}
}
}
它使用 copyStylesFromRow 值,但由于代码顺序,它使用新的行号。因此,如果您想从第 3 行开始插入 4 行:
workbook.Worksheets[1].InsertRow(3,4,6);
这将从第 3 行开始插入 4 个新行,因为包含第 3 行,您必须指向第 6 行。这是一个错误,但您可以解决它。