使用している書式や設定を一つ一つコピーしてください。
// コード例
// 第一引数のWorksheetから第二引数のWorksheetへ情報をコピーします。
private void copy(Worksheet from, Worksheet to)
{
//表示設定
//枠線をコピーします
to.DisplayOptions.ShowGridlines = from.DisplayOptions.ShowGridlines;
//印刷設定
//用紙サイズをコピーします
to.PrintOptions.PaperSize = from.PrintOptions.PaperSize;
//印刷の方向をコピーします
to.PrintOptions.Orientation = from.PrintOptions.Orientation;
//余白をコピーします
to.PrintOptions.TopMargin = from.PrintOptions.TopMargin;
to.PrintOptions.BottomMargin = from.PrintOptions.BottomMargin;
to.PrintOptions.RightMargin = from.PrintOptions.RightMargin;
to.PrintOptions.LeftMargin = from.PrintOptions.LeftMargin;
to.PrintOptions.HeaderMargin = from.PrintOptions.HeaderMargin;
to.PrintOptions.FooterMargin = from.PrintOptions.FooterMargin;
//改ページをコピーします
for (int j = 0; j < from.PrintOptions.HorizontalPageBreaks.Count; j++)
{
to.PrintOptions.HorizontalPageBreaks.Add(to.PrintOptions.HorizontalPageBreaks[j]);
}
for (int k = 0; k < from.PrintOptions.VerticalPageBreaks.Count; k++)
{
to.PrintOptions.VerticalPageBreaks.Add(to.PrintOptions.VerticalPageBreaks[k]);
}
foreach (WorksheetRow row in from.Rows)
{
// 行の高さをコピーします
to.Rows[row.Index].Height = row.Height;
foreach (WorksheetCell cell in row.Cells)
{
// 列の幅をコピーします
to.Columns[cell.ColumnIndex].Width = from.Columns[cell.ColumnIndex].Width;
if (cell.Formula != null)
{
// 関数をコピーします
to.Rows[row.Index].Cells[cell.ColumnIndex].ApplyFormula(cell.Formula.ToString());
}
else
{
// セルの値をコピーします
to.Rows[row.Index].Cells[cell.ColumnIndex].Value = cell.Value;
}
// セルのフォーマットをコピーします
to.Rows[row.Index].Cells[cell.ColumnIndex].CellFormat.SetFormatting(cell.CellFormat);
to.Rows[row.Index].Cells[cell.ColumnIndex].CellFormat.TopBorderStyle = cell.GetResolvedCellFormat().TopBorderStyle;
to.Rows[row.Index].Cells[cell.ColumnIndex].CellFormat.BottomBorderStyle = cell.GetResolvedCellFormat().BottomBorderStyle;
to.Rows[row.Index].Cells[cell.ColumnIndex].CellFormat.LeftBorderStyle = cell.GetResolvedCellFormat().LeftBorderStyle;
to.Rows[row.Index].Cells[cell.ColumnIndex].CellFormat.RightBorderStyle = cell.GetResolvedCellFormat().RightBorderStyle;
}
}
//結合セルの情報をコピーします
foreach (var m in from.MergedCellsRegions)
{
to.MergedCellsRegions.Add(m.FirstRow, m.FirstColumn, m.LastRow, m.LastColumn);
to.MergedCellsRegions[to.MergedCellsRegions.Count - 1].CellFormat.SetFormatting(m.CellFormat);
}
}