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);
}
}
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);
}
}