1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System.Data;
|
20
|
|
using System.Data.Common;
|
21
|
|
using System.Data.OleDb;
|
22
|
|
using System.IO;
|
23
|
|
using System.Text.RegularExpressions;
|
24
|
|
|
25
|
|
namespace Seasar.Extension.DataSets.Impl
|
26
|
|
{
|
27
|
|
public class XlsReader : IDataReader
|
28
|
|
{
|
29
|
|
private const int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
30
|
|
|
31
|
|
private Regex illigalColumnNamePattern = new Regex("F[0-9]+$", RegexOptions.Compiled);
|
32
|
|
|
33
|
|
private Regex workSheetPrefixPattern = new Regex(@"^#[0-9]+\s+.+$", RegexOptions.Compiled);
|
34
|
|
|
35
|
|
private DataSet dataSet_;
|
36
|
|
|
37
|
3
|
public XlsReader(string path)
|
38
|
|
{
|
39
|
3
|
string fullPath = Path.GetFullPath(path);
|
40
|
3
|
if (!File.Exists(fullPath))
|
41
|
|
{
|
42
|
0
|
throw new FileNotFoundException("xls file not found.", fullPath);
|
43
|
|
}
|
44
|
|
|
45
|
3
|
dataSet_ = CreateDataSet(fullPath);
|
46
|
|
}
|
47
|
|
|
48
|
9
|
public XlsReader(Stream stream)
|
49
|
|
{
|
50
|
9
|
string tempPath = Path.GetTempFileName();
|
51
|
9
|
using (Stream fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
|
52
|
|
{
|
53
|
9
|
byte[] b = new byte[DEFAULT_BUFFER_SIZE];
|
54
|
9
|
int n = 0;
|
55
|
53
|
while (0 < (n = stream.Read(b, 0, b.Length)))
|
56
|
|
{
|
57
|
44
|
fs.Write(b, 0, n);
|
58
|
|
}
|
59
|
|
}
|
60
|
|
|
61
|
9
|
dataSet_ = CreateDataSet(tempPath);
|
62
|
|
|
63
|
9
|
if (File.Exists(tempPath))
|
64
|
|
{
|
65
|
9
|
File.Delete(tempPath);
|
66
|
|
}
|
67
|
|
}
|
68
|
|
|
69
|
|
#region IDataReader メンバ
|
70
|
|
|
71
|
12
|
public DataSet Read()
|
72
|
|
{
|
73
|
12
|
return dataSet_;
|
74
|
|
}
|
75
|
|
|
76
|
|
#endregion
|
77
|
|
|
78
|
12
|
private DataSet CreateDataSet(string path)
|
79
|
|
{
|
80
|
12
|
string connectonString = string.Format(
|
81
|
|
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"",
|
82
|
|
path
|
83
|
|
);
|
84
|
|
|
85
|
12
|
DataSet ds = new DataSet();
|
86
|
12
|
using (OleDbConnection con = new OleDbConnection(connectonString))
|
87
|
|
{
|
88
|
12
|
if (con.State != ConnectionState.Open)
|
89
|
|
{
|
90
|
12
|
con.Open();
|
91
|
|
}
|
92
|
|
|
93
|
12
|
DataTable tableList = con.GetOleDbSchemaTable(
|
94
|
|
OleDbSchemaGuid.Tables_Info,
|
95
|
|
new object[] {null, null, null, "TABLE"}
|
96
|
|
);
|
97
|
|
|
98
|
12
|
foreach (DataRow row in tableList.Rows)
|
99
|
|
{
|
100
|
41
|
string tableName = (string) row["TABLE_NAME"];
|
101
|
41
|
if (!tableName.EndsWith("$") && !tableName.EndsWith("$'"))
|
102
|
|
{
|
103
|
2
|
continue;
|
104
|
|
}
|
105
|
|
|
106
|
39
|
string sql = string.Format("select * from [{0}]", tableName);
|
107
|
39
|
using (OleDbCommand cmd = new OleDbCommand(sql, con))
|
108
|
|
{
|
109
|
39
|
DbDataAdapter adapter = new OleDbDataAdapter(cmd);
|
110
|
39
|
adapter.AcceptChangesDuringFill = false;
|
111
|
39
|
DataTable table = new DataTable(tableName);
|
112
|
39
|
adapter.Fill(table);
|
113
|
39
|
SetupTable(table);
|
114
|
39
|
ds.Tables.Add(table);
|
115
|
|
|
116
|
|
}
|
117
|
|
}
|
118
|
|
}
|
119
|
|
|
120
|
12
|
return ds;
|
121
|
|
}
|
122
|
|
|
123
|
39
|
private void SetupTable(DataTable table)
|
124
|
|
{
|
125
|
39
|
string tableName = table.TableName.Trim();
|
126
|
39
|
if (tableName.EndsWith("$"))
|
127
|
|
{
|
128
|
31
|
tableName = tableName.Remove(tableName.Length - 1, 1);
|
129
|
|
}
|
130
|
39
|
if (tableName.EndsWith("$'"))
|
131
|
|
{
|
132
|
8
|
tableName = tableName.Substring(1, tableName.Length - 3);
|
133
|
|
}
|
134
|
39
|
if (workSheetPrefixPattern.IsMatch(tableName))
|
135
|
|
{
|
136
|
8
|
tableName = tableName.Split(new char[] {' ',' '})[1];
|
137
|
|
}
|
138
|
39
|
table.TableName = tableName;
|
139
|
|
|
140
|
39
|
int rowCount = table.Rows.Count;
|
141
|
39
|
if (rowCount > 0)
|
142
|
|
{
|
143
|
31
|
SetupColumns(table, table.Columns);
|
144
|
|
}
|
145
|
|
}
|
146
|
|
|
147
|
31
|
private void SetupColumns(DataTable table, DataColumnCollection columns)
|
148
|
|
{
|
149
|
180
|
for (int i = 0; i < columns.Count; i++)
|
150
|
|
{
|
151
|
149
|
DataColumn column = columns[i];
|
152
|
149
|
string columnName = column.ColumnName.Trim();
|
153
|
|
|
154
|
149
|
bool isRemove = false;
|
155
|
|
|
156
|
149
|
if (string.Empty == columnName)
|
157
|
|
{
|
158
|
0
|
isRemove = true;
|
159
|
|
}
|
160
|
|
|
161
|
149
|
if (illigalColumnNamePattern.IsMatch(columnName))
|
162
|
|
{
|
163
|
32
|
isRemove = true;
|
164
|
|
}
|
165
|
|
|
166
|
149
|
if (isRemove)
|
167
|
|
{
|
168
|
32
|
columns.Remove(column);
|
169
|
32
|
i--;
|
170
|
|
}
|
171
|
|
}
|
172
|
|
}
|
173
|
|
}
|
174
|
|
}
|
175
|
|
|