Infragistics|Ultimate UI WPF|如何複製 Excel 工作表

infragistics log


本篇講述如何為您正在使用的每種格式或設置製作一份副本。

// 範例
// 從第一個參數,copy 至第二個參數
private void copy(Worksheet from, Worksheet to)
{
    //表示設定
    //GridLine 顯示
    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)
            {
                // 公式 Apply
                to.Rows[row.Index].Cells[cell.ColumnIndex].ApplyFormula(cell.Formula.ToString());
            }
            else
            {
                // copy 值
                to.Rows[row.Index].Cells[cell.ColumnIndex].Value = cell.Value;
            }

            // 格式 Copy
            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);
    }
}


延伸閱讀
aa71435723的大頭照
Winston

Eggplant DAI 自動化測試專家。

留言