1
13693261870
2022-10-11 a034f7f703dc0328858c0fbcdcf19031d9d9152f
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
using Community.DAL;
using Community.Model.Build;
//using NPOI.CS;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Community.Excel.Common
{
    public class XlsTools
    {
        //public static readonly ExcelHelper _excel = new ExcelHelper();
 
        /// <summary>
        /// 读取文件至字节数组
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>字节数组</returns>
        public static byte[] FileToBytes(string fileName)
        {
            byte[] bytes = null;
            FileStream fs = null;
 
            try
            {
                FileInfo file = new FileInfo(fileName);
                if (file != null)
                {
                    fs = file.OpenRead();
                    bytes = new Byte[fs.Length];
                    fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
                    fs.Flush();
                }
            }
            catch
            {
                bytes = null;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
 
            return bytes;
        }
 
        //public static DataTable ReadXls(string xls)
        //{
        //    if (string.IsNullOrEmpty(xls) || !File.Exists(xls))
        //    {
        //        MessageBox.Show("Excel文件不存在,请重新选择!");
        //        return null;
        //    }
 
        //    byte[] bytes = XlsTools.FileToBytes(xls);
        //    if (null == bytes || bytes.Length == 0)
        //    {
        //        MessageBox.Show("无法读取Excel文件!");
        //        return null;
        //    }
 
        //    return _excel.ExcelToTable(bytes);
        //}
 
        public static DataTable ReadXls(string xls, int sheet)
        {
            if (string.IsNullOrEmpty(xls) || !File.Exists(xls))
            {
                MessageBox.Show("Excel文件不存在,请重新选择!");
                return null;
            }
 
            return ReadExcel.ReadXls(xls, sheet - 1);
        }
 
        public static List<House> ProcessData(string[] splits, string name, DataTable dt)
        {
            int bid = HouseDAL.GetBuildingId(name);
            if (bid == -1)
            {
                MessageBox.Show("没有读取到楼宇Id!");
                return null;
            }
 
            House h = null;
            List<House> list = new List<House>();
            for (int i = 0, c = dt.Rows.Count; i < c; i++)
            {
                DataRow dr = dt.Rows[i];
 
                string title = GetStr(dr, 1);
                if (title.IndexOf("房屋地址") > -1)
                {
                    h = new House(bid);
                    SetHouse(i + 1, h, dr, 2, splits);
                    list.Add(h);
                }
 
                if (title.IndexOf("房屋状态") > -1)
                {
                    SetStatus(h, dr, 2);
                }
 
                if (title.IndexOf("房主信息") > -1)
                {
                    SetHousehold(h, dr, 1);
                }
                if (title.IndexOf("房主家庭成员") > -1)
                {
                    SetHousehold(h, dr, 2);
                }
                if (title.IndexOf("承租人") > -1)
                {
                    SetHousehold(h, dr, 3);
                }
            }
 
            return list;
        }
 
        public static string GetStr(DataRow dr, int col)
        {
            if (col >= dr.Table.Columns.Count) return string.Empty;
 
            object obj = dr[col];
            if (obj == null || obj == DBNull.Value)
            {
                return string.Empty;
            }
 
            return obj.ToString().Trim();
        }
 
        public static void SetHouse(int i, House h, DataRow dr, int col, string[] splits)
        {
            h.FullName = GetStr(dr, col);
 
            try
            {
                string[] strs = h.FullName.Split(splits, StringSplitOptions.RemoveEmptyEntries);
 
                h.BuildNum = strs[0];
                if (strs.Length > 1) h.Unit = strs[1];
                if (strs.Length > 2) h.HouseNum = strs[2];
            }
            catch //(Exception ex)
            {
                MessageBox.Show("行号:" + i + "\r\n" + h.FullName + ":门牌号异常!");
            }
        }
 
        // 自住\出租\闲置
        public static void SetStatus(House h, DataRow dr, int col)
        {
            string str = GetStr(dr, col);
            if (str.Length == 0)
            {
                h.HouseState = 1;
                return;
            }
 
            string status = str.Substring(0, 2);
            switch (status)
            {
                case "自住":
                    h.HouseState = 1;
                    break;
                case "出租":
                    h.HouseState = 2;
                    break;
                case "闲置":
                    h.HouseState = 3;
                    break;
            }
            //h.HouseStatus = str.Substring(0, 2);
            h.Remark = str.Replace(status, "").Replace("(", "").Replace(")", "").Trim();
        }
 
        public static void SetHousehold(House h, DataRow dr, int houseType)
        {
            string name = GetStr(dr, 2);
            if (string.IsNullOrEmpty(name)) return;
 
            Household hh = new Household();
            hh.Name = name;
            hh.HoldType = houseType;
            hh.Gender = GetStr(dr, 3);
            hh.Nation = GetStr(dr, 4);
            hh.Political = GetStr(dr, 5);
            hh.IdNum = GetStr(dr, 6).ToUpper();
            hh.Addr = GetStr(dr, 7);
            hh.Phone = GetStr(dr, 8);
            hh.Remark = GetStr(dr, 9);
 
            h.Holds.Add(hh);
        }
 
        public static int InsertHouse(List<House> list)
        {
            try
            {
                int rs = 0;
                foreach (House h in list)
                {
                    h.Id = HouseDAL.GetHouseMaxId();
                    int count = HouseDAL.InsertHouse(h);
                    if (count == 0) continue;
 
                    foreach (Household hh in h.Holds)
                    {
                        hh.House = h.Id;
                        count = HouseDAL.InsertHousehold(hh);
                    }
 
                    rs++;
                }
 
                return rs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
 
            return 0;
        }
    }
}