先获取文件上传到服务器,再从服务器取到文件名,然后用poi读取表格内容,循环一条条的插入到数据库就可以了
附上我的工具类 给你看,不懂的可以回复我
package com.egintra.frame.util;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @ClassName: ExcleUtil
* @Description: (导入Excel)
* @author zhaokejin
* @date 2015年11月2日 上午9:06:43
*
*/
public class ExcleUtil {
private static String type = "0_);[Red]\\(0\\)";
@SuppressWarnings({ "unused", "deprecation" })
public static String[][] readExcel(String filePath, String dateType,
String numFormat) {
String[][] s = null;
DecimalFormat df = new DecimalFormat("0");// 格式化 number String 字符
SimpleDateFormat sdf = new SimpleDateFormat(dateType);// 格式化日期字符串"yyyy-MM-dd HH:mm:ss"
DecimalFormat nf = new DecimalFormat(numFormat);// 格式化数字
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
filePath));
HSSFSheet sheet = workbook.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();
s = new String[rows][];
if (rows > 0) {
// 获取总列数`
// int cells = sheet.getRow(0).getPhysicalNumberOfCells();
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();
String[] cellsvalue = new String[cells + 1];
for (short c = 0; c < cells; c++) {
String value = "";
HSSFCell cell = row.getCell(c);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = String.valueOf(Math.round(cell
.getNumericCellValue()));
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if ("@".equals(cell.getCellStyle()
.getDataFormatString())) {
value = df.format(cell
.getNumericCellValue());
} else if ("General".equals(cell.getCellStyle()
.getDataFormatString())) {
value = nf.format(cell
.getNumericCellValue());
} else if (type.equals(cell.getCellStyle()
.getDataFormatString())) {
value = String.valueOf(cell
.getNumericCellValue());
} else {
value = sdf.format(HSSFDateUtil
.getJavaDate(cell
.getNumericCellValue()));
}
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
default:
break;
}
if (cell == null) {
value = "";
}
}
cellsvalue[c] = value;
if (value.endsWith(".0")) {
cellsvalue[c] = value.substring(0,
value.length() - 2);
}
}
s[r] = cellsvalue;
}
}
} catch (Exception ex) {
//自动生成 catch 块
ex.printStackTrace();
}
return s;
}
}