iT邦幫忙

0

請問以asp程式撈取CSV檔內容, 並儲存至excel檔內

linyt 2010-03-18 16:56:3712848 瀏覽

各位高手大大們有無寫過使用ASP程式撈取csv檔之內容值, 並把數值儲存到excel檔內,
是寫了一部份程式, 但還是一直有bug!! 麻煩各位高手幫幫忙, 謝謝! (或是有其他寫法也能提供的話也可以)

<meta http-equiv="Content-Type" content="text/html; charset=big5">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>撈取csv檔</title>

<% 
   '連結 DataBase
   Set DBConnection = Server.CreateObject("ADODB.Connection")
%>

<%
'底下三行固定
  a = Request.TotalBytes
  b = Request.BinaryRead(a)
  Set obj = Server.CreateObject("basp21")

' 底下接收檔案
  f1 = obj.FormFileName(b,"CSVfile")
  newf1 = split(Mid(f1,InstrRev(f1,"\")+1),".")

' 檔案長度
  fsize = obj.FormFileSize(b,"CSVfile")   

'檔案名稱
  fname = Mid(f1,InstrRev(f1,"\")+1)    

'存檔,儲存位置為硬碟上之絕對路徑。將上傳檔案存在c:\aaa資料夾
  l1 = obj.FormSaveAs(b,"CSVfile","C:\aaa\" & fname) 

  If l1>0 then    ' 若成功,傳回大於0之值
'   rs.addnew
'   rs("file1")=fname   '檔名
   response.write "Success !! ~~<BR>Upload File Name&nbsp:&nbsp" & fname &"&nbsp&nbsp&nbsp&nbsp&nbsp"& "&nbsp File Size&nbsp:&nbsp" & fsize &"<BR>"
  Else
    response.write "Bad~~~Upload Error!!"   
  End if

' 取得文字檔在硬碟上的真實位置
 txtfile = "C:\aaa\" & fname     
 Response.Write "txtfile = "
 Response.Write txtfile & "<BR><BR>" 
 
'如果確實上傳檔案的話,這個檔案的大小(fsize)將會大於 0 才對
 If fsize>0 then
 temp_SQL = ""
 
'解析 CSV 檔之內容,並組成 SQL 字串 
  Set fs=CreateObject("Scripting.filesystemobject")
  Set fm=fs.OpenTextfile(txtfile,1,true)

  do while not fm.AtEndOfStream
' 將文字列讀出來
    txt=fm.ReadLine    
    Response.write txt & "<br>"
 loop
  
   Set rscheck = Server.CreateObject("ADODB.Recordset")
   Do While Not (rscheck.EOF)
     sqlnew = "insert into sharepath (share_name,share_path) values (" & txt & ")"
     Response.Write sqlnew & "<BR><BR>" 
     Conn.Execute(sqlnew)
     rscheck.MoveNext
   Loop 
  else   
 End If
  fm.close
%>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
mybeldandy
iT邦研究生 5 級 ‧ 2010-03-18 17:23:03
最佳解答

SQL要塞的字串要用兩個單引號框起來
你根本沒有處理過就直接把CSV的一行貼上根本不行
方法
把CSV中的,變更為','
然後把SQL中的" & txt & "改成'" & txt & "'
這樣勉強可以用

shunyuan iT邦研究生 1 級 ‧ 2010-03-19 00:45:32 檢舉

高手

2
sula3065408
iT邦研究生 1 級 ‧ 2010-03-22 09:30:54

果然用ASP寫很累人,用有支援Regular expression的語言寫,大概兩三行就搞定了。

我要發表回答

立即登入回答