在ssis package中处理excel时遇到data missing

Problem:  when using Excel Source task to load excel data into database, if the data type in one column is different, it will appears Null into our database.

                             

 

Solution: 

View Code
using Microsoft.Office.Interop.Excel to process excel file

 

 

public System.Data.DataTable ReadExcel()

        {

            System.Data.DataTable dt = new System.Data.DataTable("tmp");

            Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass();

            app.Visible = false;

            Workbook workbook = app.Workbooks.Open("aa.xls", Missing.Value, Missing.Value,

                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

                Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

                Missing.Value, Missing.Value);

 

            var sheet = workbook.Sheets[1] as Worksheet;

            try

            {          

                var c = new DataColumn("a", typeof(int));

                c.AutoIncrement = true;

                dt.Columns.Add(c);

 

                for (int i = 1; i <= sheet.UsedRange.Rows.Count; i++)

                {

 

                    DataRow dr = dt.NewRow();

 

                    for (int j = 1; j <= sheet.UsedRange.Columns.Count; j++)

                    {

 

                        Range range = sheet.Rows[i, Missing.Value] as Range;

                        if (i == 1)

                        {

 

                            var header = (range.Cells[Missing.Value, j] as Range).Text.ToString();

                            var type = string.Empty;

                            if (j == 1 || j == 66)

                            {

 

                                dt.Columns.Add(header, typeof(DateTime));

                            }

                            else if (j == 65)

                            {

                                dt.Columns.Add(header, typeof(int));

                            }

                            else

                            {

                                dt.Columns.Add(header, typeof(string));

 

                            }

                        }

                        else

                        {

                            var v = (range.Cells[Missing.Value, j] as Range).Text.ToString();

                            if (j == 1 || j == 66)

                            {

                                if (string.IsNullOrEmpty(v))

                                {

                                    dr[j] = DBNull.Value;

                                }

                                else

                                {

                                    dr[j] = DateTime.Parse(v);

                                }

                            }

                            else if (j == 65)

                            {

 

                                if (string.IsNullOrEmpty(v))

                                {

                                    dr[j] = DBNull.Value;

                                }

                                else

                                {

                                    dr[j] = int.Parse(v);

                                }

                            }

 

                            else

                            {

                                dr[j] = v;

                            }

                        }

                    }

 

                    if (i > 1)

                    {

                        dt.Rows.Add(dr);

                    }

                }

 

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);   

            }

            return dt;

        }

 

        public void InsertIntoTable(System.Data.DataTable dt)

        {

            try

            {

                string strCon = "Integrated Security = ;Data Source=;Initial Catalog=;";

                SqlConnection connection = new SqlConnection(strCon);

                connection.Open();

                SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connection);

                sqlBulkCopy.DestinationTableName = "";

                sqlBulkCopy.WriteToServer(dt);

            }

            catch (Exception ex)

            {

                throw ex;

            }  

 

        }

    }

 

转载于:https://www.cnblogs.com/SunnySun03/archive/2013/01/28/2880380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值