示例#1
0
 protected virtual void OnMultiplePages(MuliPageEventArgs e)
 {
     this.MultiplePages?.Invoke(this, e);
 }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <exception cref="FileNotFoundException"></exception>
        internal override Source[] ImportData(string source)
        {
            if (!File.Exists(source))
            {
                throw new FileNotFoundException("File not found.", source);
            }

            List <Source> sources = new List <Source>();

            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.ConnectionString = string.Format(CONN_STR, source);

                try
                {
                    conn.Open();

                    var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                    if (dt == null)
                    {
                        throw new Exception($"No sheets found in file '{source}'.");
                    }

                    List <string> sheetNames = new List <string>();

                    // Add the sheet name to the string array.
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow row       = dt.Rows[i];
                        var     sheetName = row["TABLE_NAME"].ToString();

                        if (sheetName.EndsWith("$") || sheetName.EndsWith("$'"))
                        {
                            sheetNames.Add(sheetName);
                        }
                    }

                    if (sheetNames.Count == 0)
                    {
                        throw new Exception($"No sheets found in file '{source}'.");
                    }

                    if (sheetNames.Count > 1)
                    {
                        var args = new MuliPageEventArgs(sheetNames);
                        this.OnMultiplePages(args);

                        if (args.Cancel)
                        {
                            return(null);
                        }

                        sheetNames = new List <string>(args.Pages);
                    }

                    using (OleDbCommand cmd = new OleDbCommand
                    {
                        Connection = conn
                    })
                    {
                        using (OleDbDataAdapter da = new OleDbDataAdapter())
                        {
                            foreach (var sheet in sheetNames)
                            {
                                cmd.CommandText = $"Select * from [{sheet}]";

                                dt = new DataTable();

                                da.SelectCommand = cmd;
                                da.Fill(dt);

                                dt.TableName = sheet;

                                sources.Add(new Source(dt));
                            }
                        }
                    }
                }
                finally
                {
                    conn.Close();
                }
            }

            return(sources.ToArray());
        }