본문 바로가기
일기장 - 아둘파

클립보드의 행 개수에 따라 FarPoint Spread에 행을 추가하면서 데이터를 붙여넣는 샘플로직

by 아둘파 2025. 6. 18.

클립보드의 행 개수에 따라 FarPoint Spread에 행을 추가하면서 데이터를 붙여넣는 샘플로직으로 정상동작하는지 구현 테스트 필요.
using System;
using System.Windows.Forms;
using FarPoint.Win.Spread;

namespace SpreadClipboardPaste
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void PasteWithRowAddition(FpSpread spread)
        {
            try
            {
                // 클립보드 내용 가져오기
                string clipboardText = Clipboard.GetText();
                if (string.IsNullOrEmpty(clipboardText))
                {
                    MessageBox.Show("클립보드에 데이터가 없습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 클립보드 데이터를 줄 단위로 나누기
                string[] rows = clipboardText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

                if (rows.Length == 0)
                {
                    MessageBox.Show("클립보드 데이터가 비어 있습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 활성 시트 가져오기
                var activeSheet = spread.ActiveSheet;

                // 선택된 셀이 없으면 첫 번째 셀로 설정
                int startRow = 0;
                int startColumn = 0;

                var selection = activeSheet.GetSelection(0);
                if (selection != null)
                {
                    startRow = selection.Row >= 0 ? selection.Row : 0;
                    startColumn = selection.Column >= 0 ? selection.Column : 0;
                }

                // 행 추가 및 데이터 삽입
                for (int i = 0; i < rows.Length; i++)
                {
                    string[] cells = rows[i].Split('\t');

                    // 필요하면 행 추가
                    if (startRow + i >= activeSheet.RowCount)
                    {
                        activeSheet.RowCount++;
                    }

                    for (int j = 0; j < cells.Length; j++)
                    {
                        // 열 범위 체크 및 데이터 삽입
                        if (startColumn + j < activeSheet.ColumnCount)
                        {
                            activeSheet.Cells[startRow + i, startColumn + j].Text = cells[j];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"오류 발생: {ex.Message}", "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            PasteWithRowAddition(fpSpread1); // fpSpread1은 FpSpread 컨트롤의 이름입니다.
        }
    }
}

댓글