html文件代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="${base}/financing/css/index.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="${base}/financing/js/jquery.min.js"></script>
<script type="text/javascript" src="${base}/financing/js/common.js"></script>
<script type="text/javascript" src="${base}/financing/js/search.js"></script>
<script type="text/javascript" src="${base}/financing/js/product.js"></script>
<script type="text/javascript" src="${base}/financing/js/index.js"></script>
</head>
<body> <form method="POST" enctype="multipart/form-data" id="form" action="${base}/upfile/poistudy.action">
<input type="file" id="upfile" name="upfile">
<input type="button" name="checkSAPmain" id="checkSAPmain" value="导入数据" onclick="submit();" Class="listLink" >
</form>
</body>
<script>
function submit(){
var form = document.getElementById("form");
form.submit();
}
</script>
</html>

控制类(Controller文件poistudyAction):

package cn.fulong.cgn.web.action.poistudy;

import java.io.IOException;
import java.io.InputStream;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import cn.fulong.common.web.action.BaseAction;
import cn.fulong.entity.InfoVo;
import cn.fulong.entity.SysUser;
import cn.fulong.util.ImportExcelUtil; @Controller
@RequestMapping("/upfile")
public class poistudyAction extends BaseAction { /**
* 解析excel
* @throws Exception
*/
@RequestMapping("/kkkk")
public String kkk(Model model,HttpServletRequest request) throws Exception{
System.out.println("123456789");
return "/poistudy/upfile";
} @RequestMapping(value="/poistudy",method={RequestMethod.GET,RequestMethod.POST})
public String top(Model model,HttpServletRequest request) throws Exception{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
System.out.println("通过传统方式form表单提交方式导入excel文件!"); InputStream in =null;
List<List<Object>> listob = null;
MultipartFile file = multipartRequest.getFile("upfile");
if(file.isEmpty()){
throw new Exception("文件不存在!");
}
in = file.getInputStream();
listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
in.close(); //该处可调用service相应方法进行数据保存到数据库中,现只对数据输出
for (int i = 0; i < listob.size(); i++) {
List<Object> lo = listob.get(i);
InfoVo vo = new InfoVo();
vo.setCode(String.valueOf(lo.get(0)));
vo.setName(String.valueOf(lo.get(1)));
vo.setDate(String.valueOf(lo.get(2)));
vo.setMoney(String.valueOf(lo.get(3))); System.out.println("打印信息-->机构:"+vo.getCode()+" 名称:"+vo.getName()+" 时间:"+vo.getDate()+" 资产:"+vo.getMoney());
}
return "/poistudy/upfile";
} }

实体类(InfoVo):

package cn.fulong.entity;

public class InfoVo {  

    private String code;
private String name;
private String date;
private String money; public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}

工具类(ImportExcelUtil)

package cn.fulong.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook; public class ImportExcelUtil { private final static String excel2003L =".xls"; //2003- 版本的excel
private final static String excel2007U =".xlsx"; //2007+ 版本的excel /**
* 描述:获取IO流中的数据,组装成List<List<Object>>对象
* @param in,fileName
* @return
* @throws IOException
*/
public List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{
List<List<Object>> list = null; //创建Excel工作薄
Workbook work = this.getWorkbook(in,fileName);
if(null == work){
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null; list = new ArrayList<List<Object>>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if(sheet==null){continue;} //遍历当前sheet中的所有行
System.out.println(sheet.getFirstRowNum());
System.out.println(sheet.getLastRowNum());
for (int j = sheet.getFirstRowNum(); j <=sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if(row==null||row.getFirstCellNum()==j){continue;} //遍历所有的列
List<Object> li = new ArrayList<Object>();
for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
cell = row.getCell(y);
li.add(this.getCellValue(cell));
}
list.add(li);
}
}
// work.close();
return list;
} /**
* 描述:根据文件后缀,自适应上传文件的版本
* @param inStr,fileName
* @return
* @throws Exception
*/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2003L.equals(fileType)){
wb = new HSSFWorkbook(inStr); //2003-
}else if(excel2007U.equals(fileType)){
wb = new XSSFWorkbook(inStr); //2007+
}else{
throw new Exception("解析的文件格式有误!");
}
return wb;
} /**
* 描述:对表格中数值进行格式化
* @param cell
* @return
*/
public Object getCellValue(Cell cell){
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字 switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString())){
value = df.format(cell.getNumericCellValue());
}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
value = sdf.format(cell.getDateCellValue());
}else{
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
} }

使用的是Spring MVC 但是其他的MVC框架和Spring MVC差不多的.

POI所用的jar包:http://pan.baidu.com/s/1eRYC6o6

POI插件使用读取office文件的更多相关文章

  1. Java实现word文档在线预览,读取office文件

    想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览. 一.使用openof ...

  2. .NET读取Office文件内容(word、excel、ppt)

    引用命名空间 using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; using Excel = Micros ...

  3. 使用Apache下poi创建和读取excel文件

    一:使用apache下poi创建excel文档 @Test /* * 使用Apache poi创建excel文件 */ public void testCreateExcel() { // 1:创建一 ...

  4. 在线读取office 文件(Word excel 等)

    https://view.officeapps.live.com/op/view.aspx?src=http://www.xxx.com/uploadfile/app/11.xls src 后面的网址 ...

  5. java 解析office文件 大全

    原文地址:http://ansjsun.iteye.com/blog/791142 读取OFFICE文件纯文本 package org.css.resource.businesssoft.search ...

  6. Office文件的实质是什么

    Office文件的实质是什么 一.总结 一句话总结:对于一个Microsoft Office文件,其实质是一个Windows复合二进制文件(Windows Compound Binary File), ...

  7. Office文件的奥秘——.NET平台下不借助Office实现Word、Powerpoint等文件的解析

    Office文件的奥秘——.NET平台下不借助Office实现Word.Powerpoint等文件的解析 分类: 技术 2013-07-26 15:38 852人阅读 评论(0) 收藏 举报 Offi ...

  8. Office系列(1)---将Office文件(Word、PPT、Excel)转换为PDF文件

    需求: 将Office文件作为文章并在网页上预览,主要为(Word.PPT.Excel)3种类型文件. 研究了一下,找到了两种解决方案 直接调用微软的在线预览功能实现(预览前提:预览资源必须可以直接通 ...

  9. 分别用Java和JS读取Properties文件内容

    项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...

随机推荐

  1. c#中关于sealed修饰类的性能提升的测试

    在clr var c#一书中,作者描述当用sealed修饰类时,可以提高系统性能而且建议大家也养成用sealed来修饰类的习惯.由于对性能二字比较敏感,所以本文先测试一下用sealed分别修饰和不修饰 ...

  2. 随机函数Surprising

    之前写了个用来抽取1-54号的随机函数,发现30-40出现的情况很大,就在果壳上提问了一下//听取了某个大神的建议循环了10000次之后惊喜的发现这样写出现了一大堆相同的数字! 之后有个很神大牛解答了 ...

  3. 企业架构研究总结(29)——TOGAF架构内容框架之概述及架构工作产品分类

    在TOGAF 9之前的版本中,TOGAF的重点主要集中在企业架构开发方法方面,用于指导其使用者如何在各自的组织中对企业架构进行创建和维护,而对于企业架构的具体内容并没有相关的论述,因而针对早期TOGA ...

  4. DevExpress 学习使用之 Skin

    新建了一个XtraForm,设置属性里的皮肤属性却不能实现,群里朋友发过来一个简单的换肤项目 1. 先觉条件似乎是窗体应该继承“public partial class XtraForm1 : Dev ...

  5. 数组和Hash表

    数组和Hash表 当显示多条结果时,存储在变量中非常智能,变量类型会自动转换为一个数组. 在下面的例子中,使用GetType()可以看到$a变量已经不是我们常见的string或int类型,而是Obje ...

  6. Aliexpress API 授权流程整理

    Aliexpress API 授权流程整理   前言 我零零总总用了好几个月的时间,写了一个自用的小程序,从 Aliexpress 上抓取订单的小程序.刚开始写的时候,该API还没有开放,而且没有订单 ...

  7. 谈谈.NET程序集(一)

    谈谈.NET程序集(一) The Assembly in .NET by 唐小崇 http://www.cnblogs.com/tangchong 在.NET出现之前, Windows的程序有一些非常 ...

  8. google 浏览器使用技巧(一)

    google 浏览器使用技巧(一) google 浏览器使用技巧 1. 调整地址栏的宽度 当安装多个插件的时候,默认插件会隐藏,所以使用起来很麻烦.在chrome 浏览器的配置中没有找到相应的配置.一 ...

  9. [修]开启MySQL远程访问权限 允许远程连接

    原文地址:http://www.cnblogs.com/XL-Liang/archive/2012/05/03/2481310.html 这个地址也许更有帮助:http://www.cppblog.c ...

  10. git的初步使用---本地代码库的使用和提交

    git的初步使用---本地代码库的使用和提交 git是一个好东西,但对于新手来说,这个工具并不好使用,因为它里面涉及到很多东西,而这些东西新手一时间是无法理解的.不幸的是,本人就是新手一枚,所以,这里 ...