在jsp页面中导入ajaxfileupload.js文件
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
jsp代码
<script type="text/javascript">
function saveImport(){
var file = 'fileInput'; //上传文件文本框ID
$.ajaxFileUpload({
url:'basics_importExcel.action',//用于文件上传的服务器端请求地址
secureuri:false,//一般设置为false
fileElementId:file,//文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: 'text/html',//返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
alert("导入成功!");
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert("导入失败!");
}
});
}
</script>
</head>
<body>
<div class="divTable">
<table align="center" style="width: 400px; text-align:center; height: 30px;">
<tr>
<td style="width: 100px;">选择文件</td>
<td style="width: 300px;">
<input type="file" name="file" id="fileInput"/>
</td>
</tr>
</table>
<div style="margin-top: 10px; text-align: center;">
<input type="button" class="butSave" value="保存" onclick="saveImport();">
<input type="button" class="butSave" value="取消" onclick="closeBG();">
</div>
</div>
</body>
</html>
Java代码
private File file;
//提交过来的file的名字
private String fileFileName;
public File getFile()
{
return file;
}
public void setFile(File file)
{
this.file = file;
}
public String getFileFileName()
{
return fileFileName;
}
public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}
public void importExcel()
{
try
{
InputStream in = new FileInputStream(file);
Workbook wb = openWorkbook(in, fileFileName);
Sheet sheet = (Sheet)wb.getSheetAt(0);
Row row = null;
Cell cell = null;
int totalRows = sheet.getPhysicalNumberOfRows(); //获取到Excel文件中的所有行数
int totalCells = sheet.getRow(0).getPhysicalNumberOfCells(); //获取到Excel文件中的所有的列
for(int r=2; r<=totalRows; r++)
{
row = sheet.getRow(r); // 读取左上端单元格
if(row!=null)
{
String[] cellValue = new String[totalCells];
for(int c = 0; c < totalCells; c++) //遍历列
{
cell = row.getCell(c); //获取到列的值
if(null != cell){
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell))
{
Date dd = cell.getDateCellValue();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cellValue[c] = df.format(dd);
}
else
{
cellValue[c] = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue[c] = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue[c] = cell.getBooleanCellValue() + "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
cellValue[c] = cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue[c] = "";
break;
default:
cellValue[c] = "0";
break;
}
}
}
String[] val = cellValue;
if(val[1] != null)
{
SuppliesBasal sp = suppliesBasalService.getByCode(val[1].trim());
Type typeNos = typeService.getByTypeNo(val[1].trim().substring(0,3)); //12位码的前三位同类型信息的编号
if(sp == null)
{
if(typeNos !=null)
{
SuppliesBasal supBasal = new SuppliesBasal();
supBasal.setSuppliesCode(val[1].trim());
supBasal.setSuppliesName(val[2]);
supBasal.setPecifications(val[3]);
supBasal.setReferPrice(Double.parseDouble(val[4]));
supBasal.setType(typeNos);
supBasal.setAmountUnit(val[5]);
supBasal.setPerAmount(Double.parseDouble(val[6]));
supBasal.setMeasuringUnit(val[7]);
supBasal.setIsFixedAssets(val[8]);
supBasal.setUseLife(Integer.parseInt(val[9]));
supBasal.setToolOrMaterials(val[10]);
supBasal.setSuppliesCategory(val[11]);
supBasal.setIsTest(val[12]);
supBasal.setTestCycle(Integer.parseInt(val[13]));
suppliesBasalService.save(supBasal);
}
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
*判断excel的版本
*/
static Workbook openWorkbook(InputStream in,String filename)throws IOException
{
Workbook wb = null;
if(filename.endsWith(".xlsx")){
wb = new XSSFWorkbook(in);//Excel 2007
} else {
wb = new HSSFWorkbook(in);//Excel 2003
}
return wb;
}