java+jsp导入excel
来源:互联网 发布:星际淘宝网txt下载80 编辑:程序博客网 时间:2024/06/05 04:49
jsp
<button onclick="shouye();" style="float: right;">返回首页</button>
<form id="excelForm" enctype="multipart/form-data" method="post" style="">
<table id="mainForm" >
<tr>
<td> 上传文件:</td>
<td><input type="file" name="file" id="files" onchange="importExcel()" /></td>
</tr>
<tr>
<td colspan="2" style="padding-top:3em; text-align: center; padding-right: 140px;">
<input type="submit" value="导入excel" onclick="isnull()" /></td>
<td> </td>
</table>
</form>
javascript
<script type="text/javascript">
var filePath;
function importExcel(){
var filename=$("#files").val();
var filePath=filename.substring(11,filename.length);
$('#excelForm').attr('action','${pageContext.request.contextPath}/`````````_importExcel.action?filePath='+filePath);
}
function shouye(){
window.location.href='${pageContext.request.contextPath}/`````````.test2.action';
}
</script>
action
private InputStream inputStream;
private String filePath; //文件路劲(全名称)private File file;//文件
//提交过来的file的名字必须有
private String fileFileName;
//提交过来的file的MIME类型
private String fileContentType;
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
/**
* 导入excel
* @return
* @throws IOException
*/
public String test2() throws IOException{
ImportExcel im=new ImportExcel();
String root = ServletActionContext.getServletContext().getRealPath("/importExcel");
InputStream is = new FileInputStream(file);
File files=new File(root);
if (!files.exists()) {
files.mkdir();
}
OutputStream os = new FileOutputStream(new File(root, fileFileName));
byte[] buffer = new byte[500];
int length = 0;
try {
while(-1 != (length = is.read(buffer, 0, buffer.length)))
{
os.write(buffer);
}
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
im.read(lexiconService,(root+filePath),ClassifyAction.getTypeId());
inputStream =new ByteArrayInputStream("导入成功,且已成功入库".getBytes("utf-8"));
} catch (Exception e) {
try {
inputStream =new ByteArrayInputStream("导入失败".getBytes("utf-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return "importExcel";
}
ImportExcel (导入类)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javassist.compiler.Lex;
import javax.annotation.Resource;
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;
import org.bigdatacn.sfgk.filter.action.BaseAction;
import org.bigdatacn.sfgk.filter.service.LexiconService;
import org.bigdatacn.sfgk.filter.service.impl.LexiconServiceImpl;
import org.bigdatacn.sfgk.wlzbs.domain.Lexicon;
import org.bigdatacn.sfgk.wlzbs.domain.User;
import org.springframework.stereotype.Controller;
public class ImportExcel{
InputStream stream;
public void read(LexiconService lexiconService,String filePath,int typeId) throws IOException, ParseException {
String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
InputStream stream = new FileInputStream(filePath);
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(stream);
}else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(stream);
}else {
System.out.println("您输入的excel格式不正确");
}
Sheet sheet1 = wb.getSheetAt(0);
int num=0;
for (Row row : sheet1) {
if (num==0 || num==sheet1.getLastRowNum()) {
num++;
continue;
}
if (null== row.getCell(2) || row.getCell(2).toString()=="" ) {//若主要内容 没写 则不予通过
break;
}
实体类 le=new 实体();
le.setCreateDate(new SimpleDateFormat("yyyy-mm-dd hh:MM:ss").parse(row.getCell(1).toString()));
le.setContains(row.getCell(2).toString());
le.setType(typeId);
try {
System.out.println(lexiconService);
lexiconService.saveInfo(le);
} catch (Exception e) {
System.out.println("保存saveInfo失败");
}
}
}
}
maven的方式,在pom.xml中的配置
<!-- 加入POI核心依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
若是不用maven的方式,则需要导入jar包
- java+jsp导入excel
- jsp页面导入excel
- jsp导入excel
- Java数据导入Excel
- JAVA 数据导入Excel
- JAVA 导入导出Excel
- java excel导入导出
- java excel导入导出
- java导入Excel示例
- Java实现导入Excel
- java导入/导出excel
- JAVA excel 导入
- java导入导出excel
- Java数据导入Excel
- Java实现导入Excel
- java Excel导入
- java 导入excel
- EXCEL导入Java代码
- vi编辑只读文档无法保存的解决办法
- Java文件与IO(四)之其他流
- Android SingleTask 探究
- Box2d源码学习<二>内存管理之SOA的实现
- shader内置变量
- java+jsp导入excel
- VMware vShpere5 分布式网络交换机概念图
- NFA转换成DFA——汉字形式数字转换成整数数字
- 基于Netty5.0高级案例一之NettyWebsocket
- 加密解密的一个小测试
- 广播
- 【Leetcode】Delete Duplicate Emails
- 使用 CXF 做 webservice 简单例子
- Web - JSONP和同源策略漫谈