前端代码:
<form action="http://你的设置的名字.free.idcfengye.com/weixin/addExcel"
method="post" enctype="multipart/form-data" name="fileForm">
<input type="text" id="filename" style="display: none" />
<input type="text" id="file_err" style="display: none" />
文件代号:<input type = "text" name = "gid" id = "gid" value = "" /><br/>
<input type="hidden" id ="ma" name = "ma">
Excel文件(.xls或.xlsx)<input type="file" id="file" name="myfile" onchange="" style="display: block" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
<button type = "submit" class ="but" " >提交</button>
</form>
后端代码:
addExcel:(这里我是读取Excel然后存入数据库)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
System.out.println("post");
// 请求是否为multipar请求
if (!ServletFileUpload.isMultipartContent(request)) {
throw new RuntimeException("当前请求不支持文件上传");
} else {
int ma = 0;
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
String groupid = null;
int count = 0;
for (FileItem item : items) {
if (item.isFormField()) {// 普通表单项
String fieldName = item.getFieldName();
String fieldValue = item.getString();
if(count == 0) {
groupid = fieldValue;
}
if(count == 1) {
ma = Integer.valueOf(fieldValue);
}
System.out.println(fieldName+"="+fieldValue);
count++;
} else {
String fileName = item.getName();// 文件名
System.out.println(fileName);
String str = fileName;
String[] strs = str.split("\\.");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
InputStream is = item.getInputStream();
String path = this.getServletContext().getRealPath("/Excel");
System.out.println("path"+path);
File descFile = new File(path,fileName);
OutputStream os = new FileOutputStream(descFile);
int len = -1;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
// System.out.println("执行循环");
}
readExcel re = new readExcel();
path = path + "\\" + fileName;
File file1 = new File(path,fileName);
List<Student> list = null;
if(strs[1].equals("xls")) {
list = re.readXls(path);
}else if(strs[1].equals("xlsx")) {
list = re.readXlsx(path);
}
addHuaMingCe addHua = new addHuaMingCe();
System.out.println("groupid" + "=" + groupid);
for (Student stu : list) {
System.out.println("Sevlet" + stu.getStuNum());
System.out.println(stu.getName());
System.out.println(stu.getCla());
addHua.addHuaMing(groupid, stu.getStuNum(), stu.getName(), stu.getCla());
}
System.out.println("存储完成");
os.close();
is.close();
if(ma != 0) {
System.out.println("ma"+ma);
response.sendRedirect("http://.free.idcfengye.com/weixin/setSuccess.html?qiandaoma="+ma);
}else {
response.sendRedirect("http://.free.idcfengye.com/weixin/setFail.html");
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getDay(){
readExcel:
上面部分是读取.xls文件,下面部分是读取.xlsx文件。
public class readExcel {//
public List<Student> readXls(String path) {
List<Student> list = new ArrayList<>();
InputStream inp;
try {
inp = new FileInputStream(path);
HSSFWorkbook workbook = new HSSFWorkbook(inp);
int size = workbook.getNumberOfSheets();
System.out.println("工作表数:" + size);
HSSFSheet sheet;
for (int i = 0; i < size; i++) {
sheet = workbook.getSheetAt(i);
System.out.println("读取当前工作表名" + sheet.getSheetName());
int rowNumber = sheet.getPhysicalNumberOfRows();
System.out.println("有效行数:" + rowNumber);
if (rowNumber == 0) {
continue;
} else {
for (int rowIndex = 0; rowIndex < rowNumber; rowIndex++) {
System.out.println("正在读取第" + (rowIndex + 1) + "行");
if (rowIndex == 0) {
continue;
}
HSSFRow row = sheet.getRow(rowIndex);
Student stu = new Student();
for (int cellIndex = 0; cellIndex < 3; cellIndex++) {
HSSFCell cell = row.getCell(cellIndex);
if(cellIndex == 0) {
stu.setName(cell.getStringCellValue());
System.out.println(cell.getStringCellValue());
}
if(cellIndex == 1) {
stu.setStuNum((int)cell.getNumericCellValue());
System.out.println((int)cell.getNumericCellValue());
}
if(cellIndex == 2) {
stu.setCla(cell.getStringCellValue());
System.out.println(cell.getStringCellValue());
}
}
list.add(stu);
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
// public List<Student> readXlsx(File file) {
public List<Student> readXlsx(String path) {
List<Student> list = new ArrayList<>();
InputStream inp;
XSSFWorkbook workbook;
try {
inp = new FileInputStream(path);
workbook = new XSSFWorkbook(inp);
int size = workbook.getNumberOfSheets();
System.out.println("工作表数:" + size);
XSSFSheet sheet;
for (int i = 0; i < size; i++) {
sheet = workbook.getSheetAt(i);
System.out.println("读取当前工作表名" + sheet.getSheetName());
int rowNumber = sheet.getPhysicalNumberOfRows();
System.out.println("有效行数:" + rowNumber);
if (rowNumber == 0) {
continue;
} else {
for (int rowIndex = 0; rowIndex < rowNumber; rowIndex++) {
System.out.println("正在读取第" + (rowIndex + 1) + "行");
if (rowIndex == 0) {
continue;
}
XSSFRow row = sheet.getRow(rowIndex);
Student stu = new Student();
for (int cellIndex = 0; cellIndex < 3; cellIndex++) {
XSSFCell cell = row.getCell(cellIndex);
if(cellIndex == 0) {
stu.setName(cell.getStringCellValue());
System.out.println(cell.getStringCellValue());
}
if(cellIndex == 1) {
stu.setStuNum((int)cell.getNumericCellValue());
System.out.println((int)cell.getNumericCellValue());
}
if(cellIndex == 2) {
stu.setCla(cell.getStringCellValue());
System.out.println(cell.getStringCellValue());
}
}
list.add(stu);
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}