免费论文网 首页

excel导入数据库

时间:2017-05-31 06:03 来源:免费论文网

篇一:Excel导入,将数据导入到数据库

Java实现上传Excel文件,并读取

数据插入到数据库中

1、 第一步当然是搭环境了,新建一个Web项目,然后将需要

的jar包加入,在这里只列出导入Excel文件数据所需的jar包,其中包括:poi-3.0-rc4-20070503.jar、poi-contrib-3.0-rc4-20070503.jar

poi-scratchpad-3.0-rc4-20070503.jar,需要的包就这3个了 2、 然后就是写代码了,当然是先前台然后写后台代码 3、 前台页面代码:

<form name="f0" enctype="multipart/form-data" method="post" method=”upload/upload”>

<table>

<tr align="center">

<td>

<input id="file" name="file" type="File" style="width: 200px;"><input type="submit" id="upload" value="上传"

class="buttonC4"/></td>

</tr> </table> </form>

4、 后台代码:

//必须要说的是,导入Excel文件数据到数据库,Excel文件的格式必须都是文本格式的,如果不是文本格式的话,导入的时候读取文件时会出错,切记切记(ps:具体的可以不可以是其他的格式,这我就不是太清楚了,我反正试了,不行,如果有高手还可以有其他的办法,请给小弟分享一下,谢谢QQ:495537636)

public String Upload() throws Exception {

//这里需要强调一下,Excel文件有不止一个sheet页,所以这里根据你自己的具体情况而定,如果只有一个,只要是sheet0就行了,只要记住开始是从sheet0,然后往后排就行了,以下例子是3个sheet页try{ // // // // // // // ); // // // // // // //

FileOutputStream fos=new

FileInputStream fis=new FileInputStream(file); byte[] buffer=new byte[1024]; int len=0;

while((len=fis.read(buffer))>0){ }

fos.write(buffer,0,len);

FileOutputStream(realpath+"\\"+this.getFileFileName());

String path =

System.out.println(">>>>>path="+path); String path1 = request.getContextPath(); System.out.println(">>>>path1>>>>>"+path1); String realpath = path+"upload";

System.out.println(">>>>>realpath="+realpath); String propDir =

File file=this.getFile();

ServletActionContext.getServletContext().getRealPath("/");

PropertyUtil.getProperty("project.upload.dir"); //

System.out.println(">>>>>>="+realpath+"\\"+this.getFileFileName()

// // // // //{

this.context.put("message", "上传成功!"); Anv06 anv06 = null; Anv07 anv07 = null; Anv08 anv08 = null;

String readpath = realpath+"\\"+this.getFileFileName(); POIFSFileSystem ps = new POIFSFileSystem(new HSSFWorkbook workBook = new HSSFWorkbook(ps); HSSFRow row = null; int rowNum = 0; //这里是第一个sheet页

HSSFSheet sheet0 = workBook.getSheetAt(0);// 获得SHEET0(索引) for (rowNum = 2; rowNum <= sheet0.getLastRowNum(); rowNum++)

String ywhj1 = ""; String ywhj2 = ""; String ywhj3 = ""; String ywhj4 = "";

row = sheet0.getRow((short) rowNum); if (row != null) {

// 获取当前行的colNum位置的单元格

//这里是你的Excel文件中有多少列你就写多少了,也是从0开始的 HSSFCell cell0 = row.getCell((short) 0); HSSFCell cell1 = row.getCell((short) 1); HSSFCell cell2 = row.getCell((short) 2); HSSFCell cell3 = row.getCell((short) 3); HSSFCell cell4 = row.getCell((short) 4); HSSFCell cell5 = row.getCell((short) 5); HSSFCell cell6 = row.getCell((short) 6); HSSFCell cell7 = row.getCell((short) 7); HSSFCell cell8 = row.getCell((short) 8); HSSFCell cell9 = row.getCell((short) 9); HSSFCell cell10 = row.getCell((short) 10); HSSFCell cell11 = row.getCell((short) 11); HSSFCell cell12 = row.getCell((short) 12); HSSFCell cell13 = row.getCell((short) 13); HSSFCell cell14 = row.getCell((short) 14); HSSFCell cell15 = row.getCell((short) 15); HSSFCell cell16 = row.getCell((short) 16); HSSFCell cell17 = row.getCell((short) 17); HSSFCell cell18 = row.getCell((short) 18); HSSFCell cell19 = row.getCell((short) 19); HSSFCell cell20 = row.getCell((short) 20); HSSFCell cell21 = row.getCell((short) 21);

FileInputStream(file));

cell0).trim();

cell1).trim();

cell2).trim();

HSSFCell cell22 = row.getCell((short) 22); HSSFCell cell23 = row.getCell((short) 23); HSSFCell cell24 = row.getCell((short) 24); anv06 = new Anv06();

if (cell0 != null) {// 案件号CNV601 第1列

String str = swith(cell0.getCellType(), anv06.setCnv601(str);

}

if(cell1 != null){//案件题名CNV104 第2列

String str = swith(cell1.getCellType(), anv06.setCnv104(str);

}

if(cell2 != null){//案件类型CNV102 第3列

String str = swith(cell2.getCellType(), anv06.setCnv102(str);

}

if(cell3 != null){//业务环节1CNV103 第4列 ywhj1 = swith(cell3.getCellType(), cell3).trim(); anv06.setYwhj1(ywhj1); }else{

ywhj1 = ""; }

if(cell4 != null){//业务环节2 第5列 ywhj2 = swith(cell4.getCellType(), cell4).trim(); anv06.setYwhj2(ywhj2); }else{

ywhj2 = ""; }

if(cell5 != null){//业务环节3 第6列 ywhj3 = swith(cell5.getCellType(), cell5).trim(); anv06.setYwhj3(ywhj3); }else{

ywhj3 = ""; }

if(cell6 != null){//业务环节4 第7列 ywhj4 = swith(cell6.getCellType(), cell6).trim(); anv06.setYwhj4(ywhj4); }else{

ywhj3 = ""; }

if(cell7 != null){//业务代码类型CNV105

第8列

String str = swith(cell7.getCellType(), cell7).trim();

anv06.setCnv105(str);

}

if(cell8 != null){//业务代码 CNV106 String str = swith(cell8.getCellType(), cell8).trim();

anv06.setCnv106(str);

}

if(cell9 != null){//保管期限 CNV114 String str = swith(cell9.getCellType(), cell9).trim();

anv06.setCnv114(str);}

if(cell10 != null){//页数CNV107

String str = swith(cell10.getCellType(), cell10).trim();

anv06.setCnv107(str);

}

if(cell11 != null){//建档日期CNV110 String str = swith(cell11.getCellType(), cell11).trim();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");

Date date = formatter.parse(str); anv06.setCnv110(date);

}

if(cell12 != null){//建档人CNV109 String str = swith(cell12.getCellType(), cell12).trim();

anv06.setCnv109(str);

}

if(cell13 != null){//案卷号CNV602 String str = swith(cell13.getCellType(), cell13).trim();

anv06.setCnv602(str);}

if(cell14 != null){//备注CNV108 String str = swith(cell14.getCellType(), cell14).trim();

anv06.setCnv108(str);}

if(cell15 != null){//组织机构代码CNV122

第9列

第10列第11列

第12列第13列第14列第15列第16列

篇二:怎样把Excel文件资料导入SQL Server2008的数据库的表中

怎样把Excel文件资料导入SQL Server2008的数据库的表中

1. Excel导入SQLserver的表中前,保证Excel的表中列的顺序和数据库表的列的顺序保持一致,如果该列没有值,可以为空白。

2.打开SQL企业管理器(Microsoft SQL Server Management Studio )

建议用SA身边登陆,如果Windows NT用户具有同等权限也可以

3.登陆服务后,展开数据库服务器,会在最底下找到“管理”选项,单击右键,选择“导入数据(I)”选项

4.单击数据导入选项后,会出现导入向导画面,直接单击“下一步”

5.选择数据如下图

1)在选择数据源时,请选择 Microsoft Excel,因为我们的主题就是Excel导入SQL server

2)浏览要导入的Excel文件路径

3)选择您要导入的Excel版本

4)单击“下一步”

4)数据源类型的选择列表

5).Excel版本选择列表

6.选择导入的目标

1).选择导入的目标服务器的名称,没有显示可以手工输入的

2).身份验证,建议使用用户:sa 密码:**********

3).选择要导入数据的数据库(Database)

4).单击“下一步”

7.指定表复制查询,因为本文讲述的是Excel的导入不要写SQL查询,默认选择即可,单击“下一步”

8.选择源表和源视图

1)本文讲述的是从Excel导入,请选择对应的源表 Sheet 1$

2)选择目标源,请选择您要导入的数据表的名称

3)如果有需要预览源数据,请单击右下边的“预览(P)”

4).数据源的预览效果,如果发现数据源与表的结构不一致时,请及时调整Excel 的格式。

9.查看数据类型映射

这一步基本是系统显示目标源的数据结构和类型 其次选择出错的处理方案,失败,忽略 单击“下一步”

10.保持并运行,是否要立刻运行。

篇三:Excel导入SQL Server数据库时部分内容消失的问题

你是否碰到这个问题呢?Excel里明明有内容,导入SQL Server 数据库中时,有些内容去无缘无故消失了呢?下面我来教你如何搞定它。

原因:引起这个问题的原因是格式不对,可是不管你如何设置单元格的格式,有些数据就是无动如钟。

解决办法:打开EXCEL ,选中数据列,进入“数据”页片,找到“分列”。如图:

下一步,下一步,哈哈,到第三步停下!这是关键,选中“文本”,完成。保存EXCEL文件,再导入数据库看看!是不是OK啦!什么?不明白,看下图吧!还不明白?打我电话吧!


excel导入数据库
由:免费论文网互联网用户整理提供,链接地址:
http://m.csmayi.cn/meiwen/43134.html
转载请保留,谢谢!
相关阅读
最近更新
推荐专题