了解最新技术文章
为您正在使用的每种格式或设置制作一份副本。
//代码示例
// 将信息从第一个参数 Worksheet 复制到第二个参数 Worksheet。
私人无效副本(工作表从,工作表到)
{
// 显示设置
//复制边框
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行从.Rows )_
{
//复制行高
to.Rows [ row.Index ] .Height = row.Height ; _ _ _
foreach (row.Cells中的WorksheetCell单元格)
{
//复制列宽
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);
}
}