我的源码框架是Strust1+Oracle数据库
想开发一个导入Excel数据到Oracle数据库表中的功能
通过页面上传本地Excel文件,判断Excel中的三列信息不能为空,点击上传,保存到数据库表中
------解决方案--------------------
package com.jlp.directMail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
import jxl.CellType;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.common.DBConnection;
import com.jlp.cardQuery.UploadForm;
public class EmsCodeImportAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
UploadForm uploadForm = (UploadForm) form;
FormFile xlsFile=uploadForm.getTheFile();
System.out.println(xlsFile.getFileName());
System.out.println(xlsFile.getContentType());
System.out.println(xlsFile.getFileSize());
if(!xlsFile.getContentType().equals("application/vnd.ms-excel"))
{
System.out.println("类型不对");
return mapping.findForward("import_emsCode_error");
}
else{
try{
if(!importData(xlsFile.getInputStream()))
{
return mapping.findForward("import_emsCode_error");
}
} catch (FileNotFoundException e){
e.printStackTrace();
return mapping.findForward("import_emsCode_error");
} catch (IOException e){
e.printStackTrace();
return mapping.findForward("import_emsCode_error");
}
return mapping.findForward("success_emsCodeImport");
}
}
private boolean importData(InputStream is)
{
/**//*连接数据库 */
DBConnection conn=new DBConnection();
Statement stmt = null;
/**//*获得xls数据,并导入*/
try{
jxl.Workbook rwb = Workbook.getWorkbook(is);
/**//*这里假设有多个sheets,若只有一个,则下面没必要用for,直接get(0)*/
int sheets = rwb.getNumberOfSheets();
for(int i=0;i<sheets;i++)
{
Sheet rs = rwb.getSheet(i);
System.out.println("Sheet Name:"+rs.getName());
int columnNum=rs.getColumns();
int rowNum=rs.getRows();
System.out.println("columnNum:"+columnNum);
PreparedStatement pstmDel =
conn.getConnection().
prepareStatement("delete from emsCodeImport_temp ");
pstmDel.executeUpdate();
for(int row=0;row<rowNum;row++)
{
PreparedStatement pstmInsert =
conn.getConnection().
prepareStatement("insert into emsCodeImport_temp " +
"values(?,?,?)");
for(int column=0;column<columnNum;column++)
{
Cell crl=rs.getCell(column,row);