excel导入问题
页面调用:
###############
调用为如下代码 :
function doQuery1() {
if(confirm("确定删除该企业信息吗?")){
document.kehuxinxiForm.action = "kehuxinxiupload.do?method=readExcel";
document.kehuxinxiForm.submit();
} }
#
###############
strut_config.xml:
------------------------------------
<form-beans>
<form-bean name="recexcelForm" type="com.dsweb.form.RecExcelType" />
</form-beans>
<action path="/kehuxinxiupload" type="com.dsweb.action.uploadkehuxinxi" name="recexcelForm" parameter="method" scope="request">
</action>
uploadkehuxinxi.java
--------------------
package com.dsweb.action;
import com.dsweb.form.RecExcelType;
import com.dsweb.util.DBUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.text.DecimalFormat;
public class uploadkehuxinxi
{
private String resType;
public uploadkehuxinxi(String resType)
{
this.resType = resType;
}
public void renameFile()
{
}
/**
* *
*
* @return
* @throws Exception
*/
//String filePath
public List readExcel() throws Exception
{ System.out .println("#####################");
List list = new ArrayList();
File file = new File("D:/aaa.xls");
if (!file.exists())
{
return null;
}
FileInputStream is = null;
try
{
is = new FileInputStream(file);
HSSFWorkbook workbook = new HSSFWorkbook(is);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCell cell = null;
int rowCount = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < rowCount; i++)
{
RecExcelType ret = new RecExcelType();
HSSFRow aRow = sheet.getRow(i);
//ret.setCode(resType);//资源类型;
cell = aRow.getCell((short) 0);
ret.setCode(caseType(cell)); //管理码
cell = aRow.getCell((short) 1);
ret.setIdentifier(caseType(cell)); //纳税识别码
cell = aRow.getCell((short) 2); //单位名称
ret.setCompany_name(caseType(cell));
cell = aRow.getCell((short) 2); //密码
ret.setPassword(caseType(cell));
list.add(ret);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (is != null)
{
is.close();
}
}
return list;
}
public void storeResource(List list,String cp_id,String cp_emp_id) throws Exception
{
if (list == null)
{
return;
}
else
{
Connection conn = DBUtil.getCurrentConnection();
PreparedStatement ps = null;
// ResultSet rs = null;
PreparedStatement psfolder = null;
// ImportUtil util = new ImportUtil();
// long[] primaryKey = util.getResId(list.size());
String sql = "insert into company_extend(code,idendifier," +
" password,company_name,tel1,mobile1,tel2,mobile2,open_sms1,open_sms2," +
" valid_date,create_time,address,status,postcode,last_mod_time,update_status" +
" ) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
// String sqlFolder = " insert into wap_re_folder_res (folder_id,res_id,order_index) " +
// " values(?,?,sq_wap_re_folder_res.nextval)";
boolean isFolder = false;
try
{
ps = conn.prepareStatement(sql);
// psfolder = conn.prepareStatement(sqlFolder);
for (int i = 0; i < list.size(); i++)
{
RecExcelType RecExcelType1 = (RecExcelType) list.get(i);
// ps.setLong(1, primaryKey[i]);
//ps.setInt(2, 1);
//ps.setInt(1, Integer.parseInt(resType));
ps.setString(1, RecExcelType1.getCode());
ps.setString(2, RecExcelType1.getIdentifier());
ps.setString(3, RecExcelType1.getCompany_name());
ps.setString(4, RecExcelType1.getPassword());
//ps.setString(7, "1");
//ps.setString(8, cp_id);
//ps.setString(9, cp_emp_id);
ps.addBatch();
// =============================================================================
// if (RecExcelType1.getCode() != null && RecExcelType1.getCode().trim().length() > 2)
// {
// psfolder.setString(1, recExcelType.getFolderId().trim());
// psfolder.setLong(2, primaryKey[i]);
// psfolder.addBatch();
// isFolder = true;
// }
// =============================================================================
}
ps.executeBatch();
if (isFolder)
{
psfolder.executeBatch();
}
conn.commit();
} catch (Exception e)
{
try
{
conn.rollback();
} catch (SQLException e1)
{
e1.printStackTrace();
}
}
finally
{
if (psfolder != null)
{
try
{
psfolder.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
if (ps != null)
{
try
{
ps.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}
public static String caseType(HSSFCell cell)
{
String str = "";
if (cell == null)
{
return str;
}
//要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
{
DecimalFormat df = new DecimalFormat("0.00");
String cstr = df.format(cell.getNumericCellValue());
// str = Double.toString();
String str2 = "";
if (cstr.indexOf(".") != -1)
{
str2 = cstr.substring(0, cstr.indexOf("."));
}
double tt = Double.parseDouble(str2);
double d = cell.getNumericCellValue();
if (tt == d)
{
str = str2;
}
else
{
str = cstr;
}
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK)
{
str = "";
}
else
{
str = cell.getStringCellValue();
}
return str;
}
}