文章分類/

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

尚無瀏覽量
2023-03-13 更新

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);
    }
}

快速跳轉目錄

✦ 群昱 AccessSoft 你的全面軟體解決方案 ✦

身為全球眾多知名軟體在台灣合作夥伴,歡迎諮詢你需要的軟體服務!

Picture of 軟體專家
軟體專家

群昱作為全球知名軟體推薦合作夥伴,致力於提供更多軟體解決方案給你!

更多軟體新知

立即詢價

請留下完整資訊,以便我們提供精確的服務內容給你。

詢價資訊