1 |
43ceb5b3
|
taeseongkim
|
using SqlConnectionDialog;
|
2 |
|
|
using System;
|
3 |
|
|
using System.Collections.Generic;
|
4 |
|
|
using System.Linq;
|
5 |
|
|
using System.Text;
|
6 |
|
|
using System.Threading.Tasks;
|
7 |
|
|
using System.Data.SqlClient;
|
8 |
|
|
using Microsoft.Office.Interop.Excel;
|
9 |
|
|
using System.Windows.Forms;
|
10 |
|
|
using System.Drawing;
|
11 |
|
|
using System.IO;
|
12 |
|
|
using System.IO.Compression;
|
13 |
|
|
|
14 |
|
|
namespace ID2ManagerImport
|
15 |
|
|
{
|
16 |
|
|
public static class DataManager
|
17 |
|
|
{
|
18 |
|
|
static string tableName = "ImportExcel";
|
19 |
|
|
|
20 |
|
|
public static string ConnectionDialog()
|
21 |
|
|
{
|
22 |
|
|
var factory = new ConnectionStringFactory();
|
23 |
|
|
var str = factory.OpenDialog();
|
24 |
|
|
|
25 |
|
|
return str;
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
public static bool CreateTabile(string connectionString)
|
30 |
|
|
{
|
31 |
|
|
bool result = false;
|
32 |
|
|
|
33 |
|
|
try
|
34 |
|
|
{
|
35 |
|
|
using (SqlConnection connection = new SqlConnection(connectionString))
|
36 |
|
|
{
|
37 |
|
|
connection.Open();
|
38 |
|
|
|
39 |
|
|
string checkTableQuery = $"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{tableName}'";
|
40 |
|
|
|
41 |
|
|
// 쿼리 실행하여 테이블 존재 여부 확인
|
42 |
|
|
SqlCommand checkTableCommand = new SqlCommand(checkTableQuery, connection);
|
43 |
|
|
|
44 |
|
|
int tableCount = (int)checkTableCommand.ExecuteScalar();
|
45 |
|
|
|
46 |
|
|
if (tableCount == 0)
|
47 |
|
|
{
|
48 |
|
|
// CREATE TABLE 문 작성
|
49 |
|
|
string createTableQuery = $"CREATE TABLE {tableName} ( "
|
50 |
|
|
+ " FILE_NAME VARCHAR(MAX),"
|
51 |
|
|
+ " WORKSHEET_NAME VARCHAR(MAX),"
|
52 |
|
|
+ " COUMMN_INDEX INT,"
|
53 |
|
|
+ " ROW_INDEX INT,"
|
54 |
|
|
+ " VALUE VARCHAR(MAX),"
|
55 |
|
|
+ " TopLeftCell VARCHAR(MAX),"
|
56 |
|
|
+ " OBJECT_TYPE INT"
|
57 |
|
|
+ " )";
|
58 |
|
|
|
59 |
|
|
// CREATE TABLE 쿼리 실행
|
60 |
|
|
SqlCommand createTableCommand = new SqlCommand(createTableQuery, connection);
|
61 |
|
|
createTableCommand.ExecuteNonQuery();
|
62 |
|
|
|
63 |
|
|
connection.Close();
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
result = true;
|
67 |
|
|
}
|
68 |
|
|
}
|
69 |
|
|
catch (Exception)
|
70 |
|
|
{
|
71 |
|
|
throw;
|
72 |
|
|
}
|
73 |
|
|
return result;
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
public static void DeleteData(SqlConnection connection,string FileName, string worksheetName)
|
77 |
|
|
{
|
78 |
|
|
|
79 |
|
|
string delQuery = $"DELETE FROM {tableName} WHERE FILE_NAME = '{FileName}' AND WORKSHEET_NAME = '{worksheetName}'";
|
80 |
|
|
|
81 |
|
|
// SQL 쿼리 실행
|
82 |
|
|
SqlCommand command = new SqlCommand(delQuery, connection);
|
83 |
|
|
command.ExecuteNonQuery();
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
public static bool Import(string FileName, Microsoft.Office.Interop.Excel.Worksheet worksheet)
|
87 |
|
|
{
|
88 |
|
|
bool result = true;
|
89 |
|
|
|
90 |
|
|
try
|
91 |
|
|
{
|
92 |
|
|
var connectionStr = ConnectionDialog();
|
93 |
|
|
|
94 |
|
|
if (connectionStr != null)
|
95 |
|
|
{
|
96 |
0688f998
|
taeseongkim
|
//Globals.ThisAddIn.Application.Cursor = Microsoft.Office.Interop.Excel.XlMousePointer.xlWait;
|
97 |
|
|
|
98 |
43ceb5b3
|
taeseongkim
|
if (CreateTabile(connectionStr))
|
99 |
|
|
{
|
100 |
|
|
|
101 |
|
|
using (SqlConnection connection = new SqlConnection(connectionStr))
|
102 |
|
|
{
|
103 |
|
|
connection.Open();
|
104 |
|
|
|
105 |
|
|
DeleteData(connection, FileName, worksheet.Name);
|
106 |
|
|
|
107 |
|
|
Range range = worksheet.UsedRange;
|
108 |
|
|
|
109 |
|
|
// 행 반복
|
110 |
|
|
for (int row = 1; row <= range.Rows.Count; row++)
|
111 |
|
|
{
|
112 |
|
|
// 열 반복
|
113 |
|
|
for (int column = 1; column <= range.Columns.Count; column++)
|
114 |
|
|
{
|
115 |
|
|
string cellAddress = range.Cells[row, column].Address;
|
116 |
|
|
var cellValue = range.Cells[row, column].Value;
|
117 |
|
|
//Range cell = range.Cells[row, column];
|
118 |
|
|
//XlCellType cellType = cell.Cells[row, column].CellType;
|
119 |
|
|
//// 현재 셀 값 가져오기
|
120 |
|
|
//string cellValue = cell.Cells[row, column].Value2.ToString();
|
121 |
|
|
if (cellValue != null)
|
122 |
|
|
{
|
123 |
|
|
// SQL INSERT 쿼리 작성
|
124 |
|
|
string query = $"INSERT INTO {tableName} (FILE_NAME,WORKSHEET_NAME,COUMMN_INDEX,ROW_INDEX,VALUE,TopLeftCell,OBJECT_TYPE) "
|
125 |
|
|
+ $" VALUES ('{FileName}','{worksheet.Name}',{column},{row},'{cellValue}','{cellAddress}',0)";
|
126 |
|
|
|
127 |
|
|
// SQL 쿼리 실행
|
128 |
|
|
SqlCommand command = new SqlCommand(query, connection);
|
129 |
|
|
command.ExecuteNonQuery();
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
ImportImage(connection, FileName, worksheet);
|
135 |
|
|
|
136 |
|
|
connection.Close();
|
137 |
|
|
result = true;
|
138 |
|
|
}
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
catch (Exception)
|
143 |
|
|
{
|
144 |
|
|
|
145 |
|
|
throw;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
return result;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
private static int GetColumnIndex(string CellAddress)
|
152 |
|
|
{
|
153 |
|
|
int columnIndex = 0;
|
154 |
|
|
|
155 |
|
|
if (CellAddress.TrimStart('$').Split('$').Length > 0)
|
156 |
|
|
{
|
157 |
|
|
string columnAddress = CellAddress.TrimStart('$').Split('$')[0];
|
158 |
|
|
|
159 |
|
|
foreach (char c in columnAddress)
|
160 |
|
|
{
|
161 |
|
|
columnIndex = columnIndex * 26 + (int)c - 64;
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
return columnIndex;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
private static int GetRowIndex(string CellAddress)
|
169 |
|
|
{
|
170 |
|
|
int RowIndex = 0;
|
171 |
|
|
|
172 |
|
|
if (CellAddress.TrimStart('$').Split('$').Length > 1)
|
173 |
|
|
{
|
174 |
|
|
string columnAddress = CellAddress.TrimStart('$').Split('$')[1];
|
175 |
|
|
RowIndex = Convert.ToInt32(columnAddress);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
return RowIndex;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
public static bool ImportImage(SqlConnection connection, string FILE_NAME, Worksheet worksheet)
|
183 |
|
|
{
|
184 |
|
|
bool result = true;
|
185 |
|
|
|
186 |
|
|
try
|
187 |
|
|
{
|
188 |
|
|
|
189 |
|
|
const Microsoft.Office.Core.MsoShapeType pictureShapeType = Microsoft.Office.Core.MsoShapeType.msoPicture;
|
190 |
|
|
|
191 |
|
|
string query = $"INSERT INTO {tableName} (FILE_NAME,WORKSHEET_NAME,COUMMN_INDEX,ROW_INDEX,TopLeftCell,VALUE,OBJECT_TYPE) "
|
192 |
|
|
+ $" VALUES (@FileName,@worksheetName,@ColumnIndex,@RowIndex,@TopLeftCell,@cellValue,@ObjectType)";
|
193 |
|
|
|
194 |
|
|
List<Microsoft.Office.Interop.Excel.Shape> shapes = new List<Shape>();
|
195 |
|
|
|
196 |
|
|
foreach (Shape shape in worksheet.Shapes)
|
197 |
|
|
{
|
198 |
|
|
shapes.Add(shape);
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
foreach (Shape shape in shapes)
|
202 |
|
|
{
|
203 |
|
|
if (shape.Type == pictureShapeType)
|
204 |
|
|
{
|
205 |
|
|
string location = shape.TopLeftCell.Address; // 이미지의 위치 정보
|
206 |
|
|
string base64String = null;
|
207 |
|
|
|
208 |
|
|
try
|
209 |
|
|
{
|
210 |
e3147bed
|
taeseongkim
|
|
211 |
43ceb5b3
|
taeseongkim
|
float originalHeight = shape.Height;
|
212 |
|
|
float originalWidth = shape.Width;
|
213 |
e3147bed
|
taeseongkim
|
|
214 |
43ceb5b3
|
taeseongkim
|
shape.ScaleWidth(1.0f, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);
|
215 |
e3147bed
|
taeseongkim
|
shape.ScaleHeight(1.0f, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);
|
216 |
43ceb5b3
|
taeseongkim
|
|
217 |
|
|
// 이미지를 가져올 Shape이 그림인 경우 처리
|
218 |
|
|
shape.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
|
219 |
|
|
|
220 |
|
|
if (Clipboard.ContainsImage())
|
221 |
|
|
{
|
222 |
|
|
// 클립보드에서 이미지 가져오기
|
223 |
|
|
using (Image clipboardImage = Clipboard.GetImage())
|
224 |
|
|
{
|
225 |
|
|
// 이미지 크기를 원래 크기로 되돌리기
|
226 |
|
|
|
227 |
|
|
// 이미지를 byte 배열로 변환
|
228 |
|
|
byte[] imageBytes;
|
229 |
|
|
using (MemoryStream ms = new MemoryStream())
|
230 |
|
|
{
|
231 |
|
|
clipboardImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
232 |
|
|
imageBytes = ms.ToArray();
|
233 |
|
|
|
234 |
|
|
// 이미지를 Base64 문자열로 변환
|
235 |
|
|
base64String = CompressString(Convert.ToBase64String(imageBytes));
|
236 |
|
|
imageBytes = null;
|
237 |
|
|
}
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
shape.ScaleWidth(originalWidth / shape.Width, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);
|
242 |
e3147bed
|
taeseongkim
|
shape.ScaleHeight(originalHeight / shape.Height, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);
|
243 |
|
|
|
244 |
43ceb5b3
|
taeseongkim
|
Clipboard.Clear();
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
catch (Exception ex)
|
248 |
|
|
{
|
249 |
|
|
System.Diagnostics.Debug.WriteLine(ex);
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
// 이미지를 MSSQL에 저장
|
253 |
|
|
SqlCommand insertCommand = new SqlCommand(query, connection);
|
254 |
|
|
insertCommand.Parameters.AddWithValue("@FileName", FILE_NAME);
|
255 |
|
|
insertCommand.Parameters.AddWithValue("@worksheetName", worksheet.Name);
|
256 |
|
|
insertCommand.Parameters.AddWithValue("@ColumnIndex", GetColumnIndex(location));
|
257 |
|
|
insertCommand.Parameters.AddWithValue("@RowIndex", GetRowIndex(location));
|
258 |
|
|
insertCommand.Parameters.AddWithValue("@TopLeftCell", location);
|
259 |
|
|
insertCommand.Parameters.AddWithValue("@cellValue", base64String);
|
260 |
|
|
insertCommand.Parameters.AddWithValue("@ObjectType", 1);
|
261 |
|
|
|
262 |
|
|
insertCommand.ExecuteNonQuery();
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
shapes.Clear();
|
267 |
|
|
|
268 |
|
|
result = true;
|
269 |
|
|
}
|
270 |
|
|
catch (Exception)
|
271 |
|
|
{
|
272 |
|
|
throw;
|
273 |
|
|
}
|
274 |
|
|
|
275 |
|
|
return result;
|
276 |
|
|
}
|
277 |
|
|
|
278 |
|
|
public static string CompressString(string str)
|
279 |
|
|
{
|
280 |
|
|
string result = null;
|
281 |
|
|
|
282 |
|
|
using (MemoryStream outStream = new MemoryStream())
|
283 |
|
|
{
|
284 |
|
|
using (GZipStream gzipStream = new GZipStream(outStream, CompressionMode.Compress))
|
285 |
|
|
using (MemoryStream srcStream = new MemoryStream(Encoding.UTF8.GetBytes(str)))
|
286 |
|
|
srcStream.CopyTo(gzipStream);
|
287 |
|
|
|
288 |
|
|
result = Convert.ToBase64String(outStream.ToArray());
|
289 |
|
|
}
|
290 |
|
|
|
291 |
|
|
return result;
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
public static bool Export(Microsoft.Office.Interop.Excel.Worksheet worksheet)
|
295 |
|
|
{
|
296 |
|
|
bool result = true;
|
297 |
|
|
|
298 |
|
|
try
|
299 |
|
|
{
|
300 |
|
|
var connectionStr = ConnectionDialog();
|
301 |
|
|
|
302 |
|
|
if (connectionStr != null)
|
303 |
|
|
{
|
304 |
|
|
if (CreateTabile(connectionStr))
|
305 |
|
|
{
|
306 |
|
|
|
307 |
|
|
using (SqlConnection connection = new SqlConnection(connectionStr))
|
308 |
|
|
{
|
309 |
|
|
connection.Open();
|
310 |
|
|
Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;
|
311 |
|
|
|
312 |
|
|
// SQL INSERT 쿼리 작성
|
313 |
|
|
string query = $"select TopLeftCell,VALUE from {tableName}";
|
314 |
|
|
|
315 |
|
|
// SQL 쿼리 실행
|
316 |
|
|
SqlCommand command = new SqlCommand(query, connection);
|
317 |
|
|
var read = command.ExecuteReader();
|
318 |
|
|
|
319 |
|
|
while (read.Read())
|
320 |
|
|
{
|
321 |
|
|
MessageBox.Show($"{read.GetString(0)} ; {read.GetString(1)}");
|
322 |
|
|
break;
|
323 |
|
|
}
|
324 |
|
|
|
325 |
|
|
connection.Close();
|
326 |
|
|
result = true;
|
327 |
|
|
}
|
328 |
|
|
}
|
329 |
|
|
}
|
330 |
|
|
}
|
331 |
|
|
catch (Exception)
|
332 |
|
|
{
|
333 |
|
|
|
334 |
|
|
throw;
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
return result;
|
338 |
|
|
}
|
339 |
|
|
}
|
340 |
|
|
} |