上传excel文件

7 篇文章 0 订阅
3 篇文章 0 订阅
package cn.owntt.web.servlet;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


import cn.owntt.entity.Account;
import cn.owntt.service.IAccountService;
import cn.owntt.service.impl.AccountServiceImpl;
import cn.owntt.tool.DBPropertyReader;


public class ExcelServlet extends HttpServlet{

IAccountService accountService=new AccountServiceImpl();
String hardPath=null;

@Override
public void init(ServletConfig config) throws ServletException {
hardPath=config.getServletContext().getRealPath("/");
}


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Account> accounts=null;
Account account=null;
String filePath=request.getParameter("path");
File excelFile=new File(hardPath+filePath);
List<Integer> rows=null;
//如果文件存在
if(excelFile.exists()){
accounts=new ArrayList<Account>();
InputStream in=new FileInputStream(excelFile);
try {
//通过工作簿工厂利用输入流转换获得工作簿对象
Workbook workbook=WorkbookFactory.create(in);
//获得第一页
Sheet sheet=workbook.getSheetAt(0);
//利用行的迭代器遍历所有数据行
Iterator itRow=sheet.rowIterator();
while (itRow.hasNext()) {
//获取行对象
Row row=(Row) itRow.next();
//排除第一行的表头
if(row.getRowNum()>0){
account=new Account();

//利用列的迭代器遍历所有数据列
Iterator itCell=row.cellIterator();
while (itCell.hasNext()) {
Cell cell=(Cell) itCell.next();
Object val=null;
//根据单元格数据类型获取内容
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING://字符串
val=cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC://数值
val=cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN://布尔
val=cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA://公式
val=cell.getCellFormula();
break;
default:
break;
}
switch (cell.getColumnIndex()) {
case 0:
if (val instanceof Double) {
account.setUsername(((Double) val).toString());
}else{
account.setUsername((String) val);
}
break;
case 1:
account.setSex(((String)val).equals("男")?"1":"0");
break;
case 2:
account.setAge(((Double)val).intValue());
break;
default:
break;
}

}
account.setPassword(DBPropertyReader.get("default_pwd"));
accounts.add(account);
}
}
rows=accountService.batchSave(accounts);

} catch (InvalidFormatException e) {

e.printStackTrace();
}
}
request.setAttribute("rows", rows);
request.getRequestDispatcher("account_manager.do?m=list").forward(request,response);
}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值