SSIS通过C#将Excel数据导入SQL Server

时间:2012-08-02 12:34:12

标签: c# sql-server excel ssis

当我偶然发现这篇文章时,我刚开始使用SSIS和通过C#处理Excel文件:

Link

本文介绍了如何通过SSIS将Excel数据加载到SQL Server数据库中。

但是,不是某些select语句,我需要从特定行(例如第9行)开始提取所有行。现在我的问题是,为了让我设置起始行而不需要使用任何头信息,我需要更改/擦除哪些代码?

public SourceFileDetails ScanRows(DataSet excelDataSet, string ExtractionStatement, string FindColumn)
{
        if (excelDataSet.Tables.Count < 1)
            return (SourceFileDetails)null;

        string ExtractFields = this.ReplaceString(this.ReplaceString(ExtractionStatement, "SELECT", ""), "From [Data$]", "");
        List<string> FindStrings = ExtractFields.Split(',').Select(s => s.Trim().Replace("[", "").Replace("]", "").Replace("#", ".").Replace("(", "").Replace(")", "")).ToList();

        foreach (DataTable dt in excelDataSet.Tables)
        {
            List<ItemValueHolder> FoundAddress = new List<ItemValueHolder>();
            int iDuplicates = 0;

            foreach (DataRow dr in dt.Rows)
            {
                int iItemColumnIndex = 0;
                foreach (var fieldValue in dr.ItemArray)
                {
                    object cellData = fieldValue;
                    string sCellData = cellData.ToString().Replace("#", ".").Replace("[", "").Replace("]", "").Replace("(", "").Replace(")", "");

                    if (cellData != null)

                        if (FindStrings.Exists(delegate(string k) { return k.ToLower() == sCellData.ToString().ToLower(); }))
                        {
                            if (!FoundAddress.Exists(delegate(ItemValueHolder t) { return t.Item.ToLower() == sCellData.ToLower(); }))
                                FoundAddress.Add(new ItemValueHolder(sCellData, dt.Rows.IndexOf(dr), iItemColumnIndex));
                            else
                                iDuplicates++;
                        }
                    iItemColumnIndex++;
                }
                int iTotalFields = FindStrings.Count - iDuplicates;

                if (100 * FoundAddress.Count / (float)iTotalFields >= 90.00)
                {
                    var query = FoundAddress.GroupBy(
                item => item.ItemValue,
                (itemvalue, items) => new
                {
                    Key = (int)itemvalue,
                    Count = items.Count(),
                });


                    int dataStartAddress = (from p in query
                                            where p.Count == (query.Max(it => it.Count))
                                            select p.Key).Max();
                    dataStartAddress += 1;
                    string SheetName = "[" + dt.TableName + "$A" + dataStartAddress.ToString() + ":IV]";
                    string _selectCommand = this.ReplaceString(ExtractionStatement, "[Data$]", SheetName);
                    string reportDate = "";
                    if (!string.IsNullOrEmpty(FindColumn))
                        try
                        {

                            int ColIndex = (from p in FoundAddress
                                            where p.Item.ToLower() == FindColumn.ToLower()
                                            select p.ColumnIndex).First();
                            DataRow drRowReport = dt.Rows[dataStartAddress];
                            reportDate = drRowReport[ColIndex].ToString();

                        }
                        catch (Exception e)
                        {
                            //log 
                        }
                    string NewFields = GetNewFieldsAtSource(FindStrings, dr);
                    return new SourceFileDetails("", "", "", _selectCommand, reportDate, NewFields);
                }
            }
        }
        return (SourceFileDetails)null;
    } 

非常感谢!

0 个答案:

没有答案