코드와 프로젝트 올리겠습니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
namespace ExcelToJsonConverter2
{
static class Program
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
|
Main 함수 입니다.
MainForm이름을 가진 WinForm객체 생성 말고는 없습니다.
Winform을 사용해 프로젝트를 만드시면 이게 기본 화면일겁니다.
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
namespace ExcelToJsonConverter2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void FindPathButton_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
string pathName = dialog.FileName;
string fileName = dialog.SafeFileName;
//파일을 가져왔을 때만.
if (!string.IsNullOrEmpty(pathName))
{
FIleManager.FilePath = pathName;
FIleManager.FileName = fileName;
}
}
private void CovertButton_Click(object sender, EventArgs e)
{
//파일 위치 입력 안했을 때.
if (string.IsNullOrEmpty(FIleManager.FilePath))
{
return;
}
else
{
FIleManager.SetNewFileName();
}
//같은게 있을 때 경고창.
if (FIleManager.IsExist())
{
{
return;
}
}
ExcelReader excelReader = new ExcelReader();
this.ResultTextBox.Text += (string.Format("변수 갯수 : {0}", excelReader.Range.Columns.Count) + Environment.NewLine);
JArray dataArray = excelReader.GetJsonArray();
FIleManager.SaveJSonFile(dataArray);
}
private void DirectoryOpenButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FIleManager.FileName))
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
//위치 열어주기.
else
{
string filePath = FIleManager.FilePath;
string dirPath = filePath.Substring(0, filePath.LastIndexOf('\\'));
Process.Start(dirPath);
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
}
private void OpenFindButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FIleManager.FileName))
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
//위치 열어주기.
else
{
Process.Start(FIleManager.FilePath);
}
}
private void OpenJsonButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(FIleManager.NewFileName))
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
//위치 열어주기.
else
{
Process.Start(FIleManager.NewFileName);
}
}
}
}
|
MainForm Winform의 버튼들 입니다.
버튼을 눌렀을 때 실행될 메서드가 정의되어 있습니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;
namespace ExcelToJsonConverter2
{
public class ExcelReader
{
public Excel.Application Application;
public Excel.Workbooks Workbooks;
public Excel.Workbook Workbook;
public Excel.Sheets Sheets;
public Excel.Worksheet Sheet;
public Excel.Range Cells;
public Excel.Range Range;
public ExcelReader()
{
this.Application = new Excel.Application();
this.Workbooks = Application.Workbooks;
this.Sheets = this.Workbook.Worksheets;
this.Range = this.Sheet.UsedRange;
}
public bool CheckHasType()
{
string[] typeArray = { "int", "float", "double", "string",
"short", "long", "char", "bool", "uint", "byte"};
foreach (string type in typeArray)
{
return true;
}
return false;
}
public JArray GetJsonArray()
{
JArray rtnArray = new JArray();
//데이터 이름 뽑아내기.
List<string> nameList = new List<string>();
{
nameList.Add(Range.Cells[1, i].Value.ToString());
}
//모든 컬럼을 돌며 JArray로 변환.
int startRow = (this.CheckHasType())? 3 : 2;
{
var jObject = new JObject();
{
}
Console.WriteLine(jObject.ToString());
}
return rtnArray;
}
public void Free()
{
//저장할지 물어보는거 취소.
this.Application.DisplayAlerts = false;
Marshal.ReleaseComObject(this.Range);
Marshal.ReleaseComObject(this.Cells);
Marshal.ReleaseComObject(this.Sheet);
Marshal.ReleaseComObject(this.Sheets);
Marshal.ReleaseComObject(this.Workbook);
Marshal.ReleaseComObject(this.Workbooks);
Marshal.ReleaseComObject(this.Application);
}
}
}
|
ExcelReader 클래스 입니다.
1. 엑셀 파일 읽고 초기화 하는 생성자.
2. 엑셀 파일에 데이터 타입을 입력했는지 확인하는 메서드.
3. 엑셀 파일을 Json으로 변환하기 위한 JArray로 뽑아내는 메서드
4. 불러온 엑셀을 종료하고 메모리 해제하는 메서드.
이렇게 구성되어 있습니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json.Linq;
namespace ExcelToJsonConverter2
{
public class FIleManager
{
public static string @FilePath { get; set; }
public static string @NewFileName { get; set; }
public static string @FileName { get; set; }
public static void SetNewFileName()
{
//파일 경로가 있을때.
if (!string.IsNullOrEmpty(FIleManager.FilePath))
{
string path = FIleManager.FilePath.Substring(0, FilePath.LastIndexOf('\\') + 1);
//새로운파일 이름 입력 안했을 때.
if (string.IsNullOrEmpty(FIleManager.NewFileName))
{
string newName = FilePath.Substring(FilePath.LastIndexOf('\\') + 1, FileName.Split('.')[0].Length) + @".json";
NewFileName = path + newName;
}
//입력 했을 때.
else
{
if (NewFileName.Contains(".json"))
{
//json입력.
NewFileName = path + NewFileName;
}
else
{
NewFileName = path + NewFileName + @".json";
}
}
}
}
public static bool IsExist()
{
return File.Exists(NewFileName);
}
public static void SaveJSonFile(JArray jsonDatas)
{
File.WriteAllText(NewFileName, jsonDatas.ToString());
}
}
}
|
FileManager
1. 새 파일 이름과 경로를 결정하는 함수.
2. 파일이 존재하는지 확인하는 함수.
3. Json파일로 저장하는 함수.
C# Select, Where, OrderBy, List.Find(All) (0) | 2020.05.02 |
---|---|
MyJsonConverter ver3.0.0 프로젝트 (0) | 2020.04.28 |
Excel 파일 Json으로 바꿔주는 프로그램 Ver 2.0 (0) | 2020.04.23 |
Excel파일을 json파일로 변환하는 프로그램 ver 1.0. (0) | 2020.04.22 |
C# List Sort예제. (0) | 2020.04.19 |