[C# DevExpress] C# DevExpress SpreadSheetControl(스프레드시트) 컨트롤에 데이터 넣기


안녕하세요.

 

요즘 제가 한참 C#으로 DevExpress에서 제공하는 각종 컨트롤들을 어떻게 사용하는지 공부하는 중인데요.

 

오늘은 SpreadSheetContol(스프레드시트 컨트롤) 에 데이터를 넣는 방법에 대해서 알려드리려고 합니다.

 

그럼 바로 예제 코드를 통해서 어떻게 스프레드 시트에 데이터를 넣는지 알아 보도록 하겠습니다.

 

우선 먼저 윈폼 프로젝트를 생성해 주시고 아래와 같이 SpreadSheetControl을 배치해 주시기 바랍니다.


윈폼 프로젝트 생성 및 SpreadSheetControl 배치



위와 같이 배치를 마치 셨다면, 이제 더미 데이터를 하나 만들어서 실제 SpreadSheetControl에 데이터를 넣어 보도록 하겠습니다.


예제 코드


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

using DevExpress.Spreadsheet;

using DevExpress.Utils;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Views.Grid;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace DevTest

{

    public partial class Form1 : DevExpress.XtraEditors.XtraForm

    {

        public Form1()

        {

            InitializeComponent();

 

            // Shown 이벤트 선언

            this.Shown += DevForm_Shown;

        }

 

        /// <summary>

        /// 폼이 보여질 일어나는 이벤트 핸들러

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void DevForm_Shown(object sender, EventArgs e)

        {

            //DataTable  Sheet  넣기

            SetSpreadSheetData(GetData());

        }

 

        public DataTable GetData()

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("Name");

            dt.Columns.Add("Age");

            dt.Columns.Add("Grade");

            dt.Columns.Add("Score");

            dt.Columns.Add("Check");

 

            dt.Rows.Add(new string[] { "범범조조""28""2""100""True" });

            dt.Rows.Add(new string[] { "황선홍""56""3""10""True" });

            dt.Rows.Add(new string[] { "안정환""34""2""78""False" });

            dt.Rows.Add(new string[] { "설기현""33""1""54""False" });

            dt.Rows.Add(new string[] { "아이유""28""1""45""True" });

            dt.Rows.Add(new string[] { "박지성""23""4""10""False" });

 

            return dt;

        }

 

        private void SetSpreadSheetData(DataTable dt)

        {

            this.uiSpread_Main.BeginUpdate();

 

            Worksheet sheet = this.uiSpread_Main.Document.Worksheets[0];

            sheet.Name = "Student";//Sheet 이름 설정

 

            //컬럼 Row 추가

            for (int i = 0; i < dt.Columns.Count; i++)

            {

                Cell cell = sheet.Rows[0][i];

                SetCellStyle(cell, true9, Color.LightBlue);//Cell Style 설정

                cell.SetValue(dt.Columns[i].ToString());

            }

 

            //Data Row 추가

            for (int i = 0; i < dt.Rows.Count; i++)

            {

                for (int j = 0; j < dt.Columns.Count; j++)

                {

                    Cell cell = sheet.Rows[i + 1][j];

                    SetCellStyle(cell, false9, Color.White);//Cell Style 설정

                    cell.SetValue(dt.Rows[i][j]);

                }

            }

 

            //Data 크기에 맞게 Cell 사이즈 조정

            sheet.GetUsedRange().AutoFitColumns();

            sheet.GetUsedRange().AutoFitRows();

 

            this.uiSpread_Main.EndUpdate();

        }

 

        //Cell Style 설정

        private void SetCellStyle(Cell cell, bool isBold, int fontSize, Color fillColor)

        {

            cell.Font.Bold = isBold;

            cell.Font.Size = fontSize;

            cell.FillColor = fillColor;

            cell.Borders.SetAllBorders(Color.Black, BorderLineStyle.Thin);

        }

    }

}

 

Colored by Color Scripter

cs


실행 결과



위와 같이 SpreadSheetControl에 데이터가 제대로 입력된 것을 확인하실 수 있습니다.

 

감사합니다.^^

 

참고로 위 내용은 https://shanael.tistory.com/11 위 블로그를 참고하였습니다!


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY