asp
엑셀(CSV) 파일 DB에 입력하기
duraboys
2010. 7. 9. 14:37
http://amoebabk.tistory.com/
해당 내용은 ASP에서 TABS Upload컴포넌트를 이용하여 서버에 파일을 올린 후 그 파일을 MS-SQL로 입력하는 간단한 소스 입니다. 엑셀 파일 대신 엑셀에서 저장 할 수 있는 서식있는 텍스트 파일(.csv) 파일을 이용하였습니다.
<!--#include file="dbconnect.inc"-->
<%
Set Upload = Server.CreateObject("TABS.Upload")
Upload.Start "업로딩 폴더", False
exFilename=Upload.Form("Upfile").FileName
Upload.Form("Upfile").Save
defaultPath="업로딩 폴더"&exFilename
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = fs.OpenTextFile(defaultPath,1)
Do While objFile.AtEndOfStream <> True
content=objFile.ReadLine '파일을 라인단위로 읽어들여 한줄씩 처리합니다. 한줄을 읽은 후에 objFile은 다음 줄을 가르키고 있으므로 추가적으로 다음줄을 읽으라는 명령을 안줘도 됩니다.
contarr=Split(content,",") 'CSV 에서는 서식있는 문서는 쉼표(,) 로 구분됩니다. Split 함수를 이용하여 배열화 시키면 컬럼의 갯수가 늘어나도 유연성 있게 사용할 수 있습니다.
sql="insert into member(mem_name,mem_address) values('"&contarr(0)&"','"&contarr(1)&"')"
db.execute sql
response.write contarr(0)&", "&contarr(1)&"<br>"
Loop
Response.write "완료"
Set objFile=Nothing
Set fs=Nothing
Set Upload=Nothing
%>
<!--#include file="dbconnect.inc"-->
<%
Set Upload = Server.CreateObject("TABS.Upload")
Upload.Start "업로딩 폴더", False
exFilename=Upload.Form("Upfile").FileName
Upload.Form("Upfile").Save
defaultPath="업로딩 폴더"&exFilename
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = fs.OpenTextFile(defaultPath,1)
Do While objFile.AtEndOfStream <> True
content=objFile.ReadLine '파일을 라인단위로 읽어들여 한줄씩 처리합니다. 한줄을 읽은 후에 objFile은 다음 줄을 가르키고 있으므로 추가적으로 다음줄을 읽으라는 명령을 안줘도 됩니다.
contarr=Split(content,",") 'CSV 에서는 서식있는 문서는 쉼표(,) 로 구분됩니다. Split 함수를 이용하여 배열화 시키면 컬럼의 갯수가 늘어나도 유연성 있게 사용할 수 있습니다.
sql="insert into member(mem_name,mem_address) values('"&contarr(0)&"','"&contarr(1)&"')"
db.execute sql
response.write contarr(0)&", "&contarr(1)&"<br>"
Loop
Response.write "완료"
Set objFile=Nothing
Set fs=Nothing
Set Upload=Nothing
%>