java poi传输文件_如何通过apache poi接收excel文件上传并读出内容保存到数据库?

先获取文件上传到服务器,再从服务器取到文件名,然后用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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值