1. excel_form.jsp
<%@page contentType="text/html;charset=euc-kr"%>
<html>
<script>
function checkForm() {
if (upload.file1.value == "") {
alert("ÆÄÀÏÀ» ¾÷·ÎµåÇØÁÖ¼¼¿ä.");
return false;
} else if(!checkFileType(upload.file1.value)) {
alert("¿¢¼¿ÆÄÀϸ¸ ¾÷·Îµå ÇØÁÖ¼¼¿ä.");
return false;
}
document.upload.submit();
}
function checkFileType(filePath){
var fileLen = filePath.length;
var fileFormat = filePath.substring(fileLen - 4);
fileFormatfileFormat = fileFormat.toLowerCase();
if (fileFormat == ".xls"){ return true;
} else{ return false; }
}
</script>
<body>
<form action="excel_insert.jsp" name="upload" method="POST" enctype="multipart/form-data">
<td><input type="file" name="file1" size="20" align="absmiddle" /> </td>
<td><a onclick="checkForm();" style="cursor:hand"> [Àü¼Û] </a></td>
</form>
</body>
</html>
2. excel_insert.jsp
<%@page contentType="text/html; charset=euc-kr" language="java" errorPage=""%>
<%@page import="java.util.*,java.io.*" %>
<%@page import="com.oreilly.servlet.MultipartRequest" %>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@page import="jxl.*" %>
<%@ page import="java.sql.*" %>
<%
String savePath = request.getRealPath("/")+"upload/tmp"; // ÀúÀåÇÒ µð·ºÅ丮
int sizeLimit = 30 * 1024 * 1024 ; // ¿ë·®Á¦ÇÑ
String formName = "";
String fileName = "";
Vector vFileName = new Vector();
Vector vFileSize = new Vector();
String[] aFileName = null;
String[] aFileSize = null;
long fileSize = 0;
MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit, "euc-kr", new DefaultFileRenamePolicy());
Enumeration formNames = multi.getFileNames();
while (formNames.hasMoreElements())
{
formName = (String)formNames.nextElement();
fileName = multi.getFilesystemName(formName);
if(fileName != null) // ÆÄÀÏÀÌ ¾÷·Îµå µÇ¸é
{
fileSize = multi.getFile(formName).length();
vFileName.addElement(fileName);
vFileSize.addElement(String.valueOf(fileSize));
}
}
aFileName = (String[])vFileName.toArray(new String[vFileName.size()]);
aFileSize = (String[])vFileSize.toArray(new String[vFileSize.size()]);
%>
<%
Workbook workbook = Workbook.getWorkbook(new File(savePath + "/" + fileName));
Sheet sheet = workbook.getSheet(0);
int col = sheet.getColumns(); // ½ÃÆ®ÀÇ Ä÷³ÀÇ ¼ö¸¦ ¹ÝȯÇÑ´Ù.
int row = sheet.getRows(); // ½ÃÆ®ÀÇ ÇàÀÇ ¼ö¸¦ ¹ÝȯÇÑ´Ù.
%>
<HTML>
<HEAD>
<TITLE>Excel Document Reader</TITLE>
</HEAD>
<BODY>
workbook = <%=workbook %> <br>
Çà ¼ö : <%=row %> <br>
Ä÷³ ¼ö : <%=col %> <br><br><br>
<table border="1">
<%
String [][] content = new String[row][col];
for (int i = 0 ; i < row ; i++)
{
for (int j = 0 ; j < col ; j++)
{
content[i][j] = sheet.getCell(j, i).getContents(); // ù¹ø° ÀÎÀÚ°¡ ¿ °ª, µÎ¹ø° ÀÎÀÚ°ªÀÌ Çà °ªÀÌ´Ù.!
}
}
out.println("text ¹æ½Ä" + "<br>");
for (int k = 0 ; k < content.length ; k++) //ÀÚµ¿ : Å×À̺í ÇüÅ°¡ ¾Æ´Ñ ¹æ½ÄÀ¸·Î Ãâ·Â(´Ü¼ø text·Î º¸ÀδÙ).
{
for (int l = 0 ; l < content[k].length ; l++)
{
out.println(content[k][l]);
}
out.println("<br>");
}
out.println("<br><br>");
out.println("Å×À̺íÇüÅÂÀÇ ¹æ½Ä");
for (int k = 0 ; k < content.length ; k++) // ÀÚµ¿ : Å×À̺í ÇüÅÂÀÇ ¹æ½ÄÀ¸·Î Ãâ·Â
{
out.println("<tr>");
for (int l = 0 ; l < content[k].length ; l++)
{
out.println("<td>");
out.println(content[k][l]);
out.println("</td>");
}
out.println("</tr>");
}
%>
</table>
<br><br><br>
<table border="1">
<%
Connection conn = null;
PreparedStatement pstmt = null;
try
{
// °³¹ß½Ç DB Á¢¼Ó
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@11.11.11.11:1521:ora9","id","password");
// Record¸¦ ÀÐ¾î ¹è¿·Î ÀúÀå
out.println("DB¿¡ ÀúÀåµÇ´Â µ¥ÀÌÅÍ");
for (int i = 0 ; i < row ; i++) // ¼öµ¿ : Å×À̺í ÇüÅÂÀÇ ¹æ½Ä
{
String aaa = sheet.getCell(0,i).getContents(); //ù¹ø° ÀÎÀÚ°¡ ¿ °ª, µÎ¹ø° ÀÎÀÚ°ªÀÌ Çà °ª..!
String bbb = sheet.getCell(1,i).getContents();
String ccc = sheet.getCell(2,i).getContents();
String sql="insert into PHONE_NUMBER(idx, id, grp) values(?,?,?) ";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,aaa);
pstmt.setString(2,bbb);
pstmt.setString(3,ccc);
pstmt.executeUpdate();
out.println("<tr>"); // Å×À̺í ÇüÅÂÀÇ ¹æ½ÄÀ¸·Î Ãâ·Â
out.println("<td>"+aaa+"</td>");
out.println("<td>"+bbb+"</td>");
out.println("<td>"+ccc+"</td>");
out.println("</tr>");
} // end of for
/* ÁÖ¼® ½ÃÀÛ : ¹è¿À» ÀÌ¿ëÇؼ ÀÚµ¿À¸·Î DB¿¡ ÀúÀåÇÏ´Â ·ÎÁ÷(Å×ÀÌºí ±¸Á¶°¡ °íÁ¤ÀûÀ̶ó¼ ÀÌ ¹æ½Äº¸´Ù À§ ¼öµ¿ÀûÀÎ ¹æ¹ýÀÌ ´õ ÁÁÀ»°Í °°´Ù.....!!!!)
// Record¸¦ ÀÐ¾î ¹è¿·Î ÀúÀå
String [][] content_ = new String[row][col];
for (int i = 0 ; i < row ; i++)
{
for (int j = 0 ; j < col ; j++)
{
content_[i][j] = sheet.getCell(j, i).getContents(); //ù¹ø° ÀÎÀÚ°¡ ¿ °ª, µÎ¹ø° ÀÎÀÚ°ªÀÌ Çà °ª..!
}
}
String sql="insert into PHONE_NUMBER(idx, id, grp) values(?,?,?) ";
pstmt = conn.prepareStatement(sql);
for (int k = 0 ; k < content_.length ; k++)
{
for (int l = 0 ; l < content_[k].length ; l++)
{
pstmt.setString(l+1,content_[k][l]);
}
pstmt.executeUpdate();
}
ÁÖ¼® ³¡ */
}
catch(Exception e)
{
e.printStackTrace();
conn.rollback();
%>
<script language=javascript>
alert("ó¸®Áß ¿À·ù°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù\nÀá½ÃÈÄ ´Ù½Ã ½ÃµµÇϼ¼¿ä!!");
history.back();
</script>
<%
}
finally
{
if(conn != null)
{
conn.commit();
conn.close();
}
}
%>
</table>
</BODY>
</HTML>
|