导入时读取Excel中的最后一行 - SQL数据库

时间:2013-05-20 09:45:30

标签: sql database excel import

  

我有以下代码将Excel数据导入表并更新一些参数,但是我无法读取所有行,只读取最后一行,我希望能够读取所有行并更新他们在表格中的价值

DECLARE c CURSOR FOR select  Barcode,MSISDN,POS,[SIM Card Number],[Date of Sale] FROM
OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel     12.0;Database=C:\ActivatedCards.xlsx;HDR=YES', 'SELECT Barcode,MSISDN,POS,[SIM Card     Number],[Date of Sale] FROM [sheet1$]')
declare @Barcode as bigint
declare @MSISDN as bigint
declare @POS as nvarchar(50)
declare @SIMCardNb as bigint
declare @ActivatedDate as date

begin

open c 

fetch next from c into @Barcode,@MSISDN,@POS,@SIMCardNb,@ActivatedDate


WHILE @@FETCH_STATUS = 0
begin
select @Barcode=Barcode,@MSISDN=MSISDN,@POS=POS,@SIMCardNb=[SIM Card     Number],@ActivatedDate=[Date of Sale] FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel  12.0;Database=C:\ActivatedCards.xlsx;HDR=YES', 'SELECT Barcode,MSISDN,POS,[SIM Card  Number],[Date of Sale] FROM [sheet1$]')
where Barcode <> ''

Update Cards 
set 
MSISDN=@MSISDN ,
POSSold=@POS,
Activated=1,
SIMCardNb=@SIMCardNb,
ActivatedDate=@ActivatedDate,
ImportDate=GETDATE(),
CollectionDeadlineDate=DateAdd(Day,30,ImportDate)
where Cards.BarCode=@Barcode


fetch next from c into @Barcode,@MSISDN,@POS,@SIMCardNb,@ActivatedDate
end 


CLOSE c
DEALLOCATE c

1 个答案:

答案 0 :(得分:0)

使用NPOI api读/写xls,doc,ppt文件。内置api的微软Microsoft.ACE.OLEDB.12.0'不适合这项任务。

易于使用,非常高效的例子可以在下面找到。

http://npoi.codeplex.com/