导入exce表格中的数据l到数据库
因为我的项目是JavaWeb的,所有是通过浏览器导入数据库到服务器端的数据库,这里我们采用struts来帮助我们完成。
1:首先定义一个文件上传的jsp页面。把我们的数据先上传到服务器端。
<form action="excelUpload.action" method="post" enctype="multipart/form-data">
Your excel file: <input type="file" name="file">
<input type="submit" value="开始导入">
</form>
2:在struts.xml配置我们的action喽。
<package name="excel" extends="struts-default">
<action name="excelUpload" class="action.ExcelUpload">
<result name="success" type="redirect">/excelUploadSuccess.jsp</result>
<result name="input">/excelUpload.jsp</result>
</action> </package>
3:当然是书写我们的action了。
package action; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat; import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import model.Person; import org.apache.struts2.ServletActionContext; import service.PersonService;
import service.impl.PersonServiceImpl; import com.opensymphony.xwork2.ActionSupport; //将excel导入到数据库,对于日期类型的有问题,只要在excel中要求日期的类型为2012-12-12就可以了,一定不能是2012/12/12
public class ExcelUpload extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;//后面3个成员变量的命名是有规律的,不能随便起名字
private String root; public String getRoot() {
return root;
}
public void setRoot(String root) {
this.root = root;
}
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;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
} @Override
public String execute() throws Exception {
uploadExcel2PC(); //上传excel文件到服务器的电脑上 String[][] str = getDataFromExcel();//读取excel文件,并且把数据读到一个二维数组里面去。 //printData(str); //打印一下数据。测试用 writeData2DB(str);//把数据写到数据库。 return SUCCESS;
} private void printData(String[][] str) {
for (int j = 0; j < str.length; j++) {
for (int i = 0; i < str[1].length; i++) {
System.out.print(str[j][i] + " ");
}
System.out.println();
}
} private void uploadExcel2PC() throws FileNotFoundException, IOException {
root=ServletActionContext.getRequest().getRealPath("/upload");
new File(root).mkdirs(); InputStream is=new FileInputStream(file); //目标文件,存到目录里面
File destFile=new File(root,fileFileName); OutputStream os=new FileOutputStream(destFile); byte[] buffer=new byte[400]; int length=0; while(-1!=(length=is.read(buffer))){
os.write(buffer,0,length);
} is.close();
os.close();
} public String[][] getDataFromExcel() throws BiffException, IOException{
File file = new File(root + "/" + fileFileName); Workbook book = Workbook.getWorkbook(file); //读取excel文件
Sheet sheet = book.getSheet(0); //这里是获取第一个工作表格
int rows = sheet.getRows(); //获取总的行数
int cols = sheet.getColumns(); //获取总的列数
String[][] str = new String[rows][cols]; //定义一个二维数组 for(int i=0;i<str.length;i++){ //读取单元格内容并存放到二维数组中 默认从第一行第一列读取
for(int j=0;j<str[i].length;j++){
Cell cell = sheet.getCell(j,i);
str[i][j] = cell.getContents();
}
} if (file.exists()) {
file.delete();
} return str;
} private void writeData2DB(String[][] str) {
for (int j = 1; j < str.length; j++) { //从第二行开始读取
Person person=new Person(); person.setUsername(str[j][1]);
person.setPassword(str[j][2]);
person.setAge(Integer.parseInt(str[j][3]));
person.setRegisterDate(StringToDate(str[j][4])); PersonService personService=new PersonServiceImpl();
personService.savePerson(person);
}
} public java.sql.Date StringToDate(String dateStr){
DateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date=null; try {
date = dd.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
} java.sql.Date date2 = new java.sql.Date(date.getTime()); //java.util.date --->java.sql.date return date2;
}
}
4:大功告成。当然在这里我没有写保存数据到数据库的代码。这不是这个帖子的重点。相信大家都会。
导入exce表格中的数据l到数据库的更多相关文章
- oracle数据库中导入Excel表格中的数据
1.点击[工具]-->[ODBC 导入器],如图: 2.在导入器里选择第一个[来自ODBC的数据],用户名/系统DSN-->填写[Excel Files],输入用户名和密码,点击 [连接] ...
- MySQL中导入Excel表格中的数据
在数据库中建立好响应的数据库.表(参考excel表格中列中的名字和内容): 将excel表格另存为txt文件,选择“文本文件(制表符分割)”: 打开相应的txt文件,只留下要导入的数据(windows ...
- 使用Sqoop,最终导入到hive中的数据和原数据库中数据不一致解决办法
Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL , ...
- 如何使用免费控件将Word表格中的数据导入到Excel中
我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...
- python读取excel表格中的数据
使用python语言实现Excel 表格中的数据读取,需要用到xlrd.py模块,实现程序如下: import xlrd #导入xlrd模块 class ExcelData(): def __init ...
- 利用java反射机制实现读取excel表格中的数据
如果直接把excel表格中的数据导入数据库,首先应该将excel中的数据读取出来. 为了实现代码重用,所以使用了Object,而最终的结果是要获取一个list如List<User>.Lis ...
- 如何轻松的把图片导入execl表格中
在项目中有时候会遇到往数据库中导数据的时候,往往需要把图片也一起导入execl表格中,那怎么才能把图片一块导入至execl中呢?那么今天我们就来看看怎么实现吧! 如何实现?今天我们就来用jxl和poi ...
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- jQuery Ajax遍历表格,填充数据,将表格中的数据一条一条拼成Jason数组
$.ajax({ url: baseURL + "InvoiceSale/OnQuotaInvoiceSale", //点击核销单号时,点击核销时,交互的页面 ...
随机推荐
- HTTP 415错误 Unsupported Content-Type
报如下错误: { "badMediaType": { "message": "Unsupported Content-Type", &quo ...
- FastReport.Net使用:[17]线(Line)控件使用
FastReport中,线(Line)控件怎么用?怎么画一条美观的线? 认识Line控件 1.线(Line)控件包含于形状(Shape)控件中,有5个可选项,一个标准线和四个对角线,其实都是同一种线, ...
- SPOJ694 DISUBSTR --- 后缀数组 / 后缀自动机
SPOJ694 DISUBSTR 题目描述: Given a string, we need to find the total number of its distinct substrings. ...
- SPOJ1811 && SPOJ1812
SPOJ1811 && SPOJ1812 LCS && LCS2 非常神奇的两道题... 题目大意: 给定n个字符串,求最长公共子串 做法1: 后缀数组: 把字符串连起 ...
- bzoj1036 count 树链剖分或LCT
这道题很久以前用树链剖分写的,最近在学LCT ,就用LCT再写了一遍,也有一些收获. 因为这道题点权可以是负数,所以在update时就要注意一下,因为平时我的0节点表示空,它的点权为0,这样可以处理点 ...
- JSONObject 和 JSONArray 的区别和用法
JSONObject 和 JSONArray 的数据表现形式不同: JSONObject的数据是用 { } 来表示的,例如: { "id" : "1", &q ...
- ajax请求数据填充表格———使用art-template模板提高效率
一.为什么要用art-template模板 在实习的一次项目中,因为需求中展示表格的字段有很多个,后端返回的也是json数据,这时候如果还是使用之前的字符串拼接,这样会开发得比较慢,而且容易出错,而且 ...
- iOS 画圆
_demoView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:_de ...
- iOS开发经验总结——基础工程
iOS开发经验总结--依赖库 这篇博客,我想说一下开发中经常遇到的一个问题,虚拟个场景描述一下的话,应该是这样的. 项目经理:今天我们正式开始一个新项目,iOSer你负责把苹果端的APP完成,有没有问 ...
- rabbitmq 连接測试
1.假设写错了host (如:factory.setHost("locathost"); )报错: Exception in thread "main" jav ...