ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using HotelPms.Share.Data;
using HotelPms.Share.Util;
using HotelPms.Share.Windows.Util;
using NPOI.SS.UserModel;
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 HotelPms.SourceFactory
{
    public partial class TableToExcel : Form
    {
        public TableToExcel()
        {
            InitializeComponent();
        }
 
        private void OpenExcel_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Application.StartupPath;
            if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; }
            txtExcelPath.Text = openFileDialog1.FileName;
 
            cmbSheet.Items.Clear();
            using (NPOIExcel excel = new NPOIExcel(txtExcelPath.Text))
            {
                for (int i = 0; i < excel.Workbook.NumberOfSheets; i++)
                {
                    cmbSheet.Items.Add(excel.Workbook.GetSheetAt(i).SheetName);
                }
            }
 
        }
 
        private void btnCopySheet_Click(object sender, EventArgs e)
        {
            using (NPOIExcel excel = new NPOIExcel(txtExcelPath.Text))
            {
                ISheet sheet = excel.Workbook.GetSheetAt(excel.Workbook.GetSheetIndex(cmbSheet.SelectedItem.ToString()));
                sheet.CopyTo(excel.Workbook, txtNewSheet.Text, true, true);   //2.5.2のNET5.0版にBUGがあって、keepFormulasを使えない 2.5.3でFixした
                //sheet.CopyTo(excel.Workbook, txtNewSheet.Text, true, false);
                excel.Save();
 
                MessageBox.Show("OK");
            }
            
        }
 
        private void btnOpenDB_Click(object sender, EventArgs e)
        {
            using (DataAccess dataAccess = new MsSqlNet(new DBConnectItem() { HostName = txtHost.Text, UserID = txtUser.Text, Password = txtPassword.Text, DBName = txtDB.Text }))
            {
                using (DataTable data = dataAccess.GetTableList())
                {
                    cmbTable.Items.Clear();
                    foreach(DataRow row in data.Rows)
                    {
                        cmbTable.Items.Add(row["Name"]);
                    }
 
                    if (cmbTable.Items.Count > 0) { cmbTable.SelectedIndex = 0; }
                }
            }
        }
 
        private void CopyPage(NPOIExcel excel, int beginRowdx, int pageRowCount)
        {
            int j = beginRowdx;
            for (int i = 0; i < pageRowCount; i++)
            {
                excel.Sheet.CopyRow(i, j);
                j++;
            }
        }
 
        private void SetTableToSheet(DataTable data, NPOIExcel excel, string sheetName)
        {
            excel.SetCurrentSheet(excel.Workbook.GetSheetIndex(sheetName));
 
            int pageRowCount = 50;
            int pageNo = 1;
 
            int rowIdx = 7;
            foreach (DataRow row in data.Rows)
            {
                if (rowIdx % pageRowCount == 0)
                {
                    //ページ最後の空行
                    pageNo++;
 
                    //ページコピー
                    if ((excel.Sheet.LastRowNum + 1) < pageNo * pageRowCount)
                    {
                        CopyPage(excel, rowIdx + 1, pageRowCount);
                    }
 
                    rowIdx += 7;
                }
 
                excel.SetCell($"G{rowIdx - 3}", sheetName);
                excel.SetCell($"AC{rowIdx - 2}", DateTime.Now.ToString("yyyy/MM/dd"));
 
                excel.SetCell($"D{rowIdx}", row["Field"].ToString());  //TableName
                int len = CConvert.ToInt(row["max_length"]);
                string type = row["type"].ToString().ToUpper();
                if (type == "NVARCHAR") { len /= 2; }
                else if (type == "INT" || type == "DATETIME" || type == "SMALLDATETIME" || type == "SMALLINT" || type == "TINYINT" || type == "BIT") { len = -1; }
                string lenText = (len == -1 ? string.Empty : $"({len})");
 
                excel.SetCell($"S{rowIdx}", $"{row["type"].ToString().ToUpper()}{lenText}");  //型(長さ)
                excel.SetCell($"X{rowIdx}", row["PKFlg"].ToString() == "1" ? "〇" : string.Empty);  //PK
                rowIdx++;  //次の行番号
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if(cmbSheet.SelectedIndex == -1)
            {
                MessageBox.Show("シートを選んでください。");
                cmbSheet.Focus();
                return; 
            }
 
            if (cmbTable.SelectedIndex == -1)
            {
                MessageBox.Show("テーブルを選んでください。");
                cmbSheet.Focus();
                return;
            }
 
            using (MsSqlNet dataAccess = new MsSqlNet(new DBConnectItem() { HostName = txtHost.Text, UserID = txtUser.Text, Password = txtPassword.Text, DBName = txtDB.Text }))
            {
                using (DataTable data = dataAccess.GetFieldList(cmbTable.SelectedItem.ToString()))
                {
                    using (NPOIExcel excel = new NPOIExcel(txtExcelPath.Text))
                    {
                        SetTableToSheet(data, excel, cmbSheet.SelectedItem.ToString());
                        excel.Save();
                        MessageBox.Show("OK");
                    }
                }
            }
        }
 
        private bool ExistsKey(string data, Dictionary<string, int> keyDict)
        {
            if (keyDict.Count == 0) { return true; }
            foreach(KeyValuePair<string, int> item in keyDict)
            {
                if (data.StartsWith(item.Key)) { return true; }
            }
            return false;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            Dictionary<string, int> keyDict = GetTableKey();
 
            using (NPOIExcel excel = new NPOIExcel(txtExcelPath.Text))
            {
                excel.SetCurrentSheet(excel.Workbook.GetSheetIndex("目次"));
                int rowIdx = 5;
                int i = 1;
                foreach (string item in cmbTable.Items)
                {
                    if (!ExistsKey(item, keyDict)) { continue; }
                    excel.SetCell($"D{rowIdx}", item);  //TableName
                    rowIdx++;
                    i++;
                    if (i == 52) 
                    {
                        rowIdx += 5;
                        i = 1;
                    }
                }
 
                excel.Save();
                MessageBox.Show("OK");
            }
        }
 
        private Dictionary<string, int> GetTableKey()
        {
            Dictionary<string, int> keyDict = new Dictionary<string, int>();
            if (txtTableConditon.Text.Length > 0)
            {
                string[] condList = txtTableConditon.Text.Split(new char[] { ',' });
                foreach (string key in condList)
                {
                    keyDict.Add(key, 0);
                }
            }
            return keyDict;
        }
 
 
        private void button3_Click(object sender, EventArgs e)
        {
            if (cmbTable.Items.Count == 0)
            {
                MessageBox.Show("テーブル一覧がない。");
                cmbSheet.Focus();
            }
 
            Dictionary<string, int> keyDict = GetTableKey();
 
            using (MsSqlNet dataAccess = new MsSqlNet(new DBConnectItem() { HostName = txtHost.Text, UserID = txtUser.Text, Password = txtPassword.Text, DBName = txtDB.Text }))
            {
                using (NPOIExcel excel = new NPOIExcel(txtExcelPath.Text))
                {
                    int i = 0;
                    foreach (string tableName in cmbTable.Items)
                    {
                        if (!ExistsKey(tableName, keyDict)) { continue; }
                        using (DataTable data = dataAccess.GetFieldList(tableName))
                        {
                            SetTableToSheet(data, excel, tableName);
                        }
                        i++;
                        lblStatus.Text = i.ToString();
                        lblStatus.Refresh();
                    }
                    excel.Save();
                    MessageBox.Show("OK");
                }
            }
        }
 
        private void txtExcelPath_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void cmbSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
    }
}