jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
namespace HW.Utility
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
 
    public sealed class ExcelOle : IDisposable
    {
        public const int EXCEL_MAXROW = 0x10000;
        private string excelPath;
        private string specifyTableName;
 
        public event RenderDataFromDataRowHandler OnFillDataRowRender;
 
        public ExcelOle() : this("")
        {
        }
 
        public ExcelOle(string excelPath)
        {
            this.excelPath = excelPath;
        }
 
        private string CleanStringForSql(object obj)
        {
            return this.CleanStringForSql(obj.ToString());
        }
 
        private string CleanStringForSql(string str)
        {
            return str.Replace("'", "''");
        }
 
        public void Dispose()
        {
            this.excelPath = null;
        }
 
        public void FillDataByList(DataTable dt)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                list.Add(dt.Columns[i].ColumnName);
            }
            this.FillDataByList(dt, list.ToArray());
        }
 
        public void FillDataByList(DataTable dt, string[] columns)
        {
            if ((columns == null) || (columns.Length == 0))
            {
                this.FillDataByList(dt);
            }
            if (dt.Rows.Count > 0x10000)
            {
                while (dt.Rows.Count >= 0x10000)
                {
                    dt.Rows.RemoveAt(dt.Rows.Count - 1);
                }
            }
            ArrayList alSql = new ArrayList();
            int length = columns.Length;
            foreach (DataRow row in dt.Rows)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("INSERT INTO {0} ({1}) VALUES (");
                for (int i = 0; i < length; i++)
                {
                    string str = "";
                    string columnName = columns[i];
                    if (this.OnFillDataRowRender != null)
                    {
                        str = this.OnFillDataRowRender(row, row[columnName], columnName, row.Table.Columns[columnName].DataType);
                    }
                    else if ((row[columnName] == null) || (row[columnName] is DBNull))
                    {
                        str = "";
                    }
                    else
                    {
                        str = row[columns[i]].ToString();
                    }
                    builder.Append("'" + str + "',");
                }
                builder.Remove(builder.Length - 1, 1);
                builder.Append(");");
                alSql.Add(builder.ToString());
            }
            this.RunSqlArray(alSql);
        }
 
        private void RunSqlArray(ArrayList alSql)
        {
            using (OleDbConnection connection = new OleDbConnection(this.ExcelConnString))
            {
                DataTable schemaTable;
                connection.Open();
                string specifyTableName = "";
                if (string.IsNullOrEmpty(this.specifyTableName))
                {
                    specifyTableName = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0][2].ToString().Trim();
                }
                else
                {
                    specifyTableName = this.specifyTableName;
                }
                specifyTableName = "[" + specifyTableName + "]";
                string str3 = "";
                OleDbCommand command = null;
                command = new OleDbCommand(string.Format("SELECT TOP 1 * FROM {0}", specifyTableName), connection);
                using (OleDbDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo))
                {
                    schemaTable = reader.GetSchemaTable();
                }
                foreach (DataRow row in schemaTable.Rows)
                {
                    str3 = str3 + "[" + row["ColumnName"].ToString() + "],";
                }
                str3 = str3.TrimEnd(new char[] { ',' });
                foreach (string str4 in alSql)
                {
                    new OleDbCommand(string.Format(str4, specifyTableName, str3), connection).ExecuteNonQuery();
                }
            }
        }
 
        public string SetSpecifyTableName(string specifyTableName)
        {
            this.specifyTableName = specifyTableName + "$";
            return this.specifyTableName;
        }
 
        private string ExcelConnString
        {
            get
            {
                return string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source ={0};Extended Properties='Excel 8.0;'", this.ExcelPath);
            }
        }
 
        public string ExcelPath
        {
            get
            {
                if (!(!string.IsNullOrEmpty(this.excelPath) && File.Exists(this.excelPath)))
                {
                    throw new Exception("Excel为空");
                }
                return this.excelPath;
            }
            set
            {
                this.excelPath = value;
            }
        }
    }
}