如何写论文?写好论文?免费论文网提供各类免费论文写作素材!
当前位置:免费论文网 > 美文好词 > 优质好文 > 把excel导入数据库

把excel导入数据库

来源:免费论文网 | 时间:2017-05-11 07:04 | 移动端:把excel导入数据库

篇一:图解把Excel数据导入到SQL Server 2008

实验环境:WIN XP , MS Office Excel 2007 , Sql Server 2008

准备数据: Excel文档 :D:\test\重复出修率.xlsx

方法一:

第一步: 打开SSMS:

【开始】→【所有程序】→【Microsoft Sql Server 2008】→【Sql Server Management Studio】

第二步: 连接数据库:

在’对象资源管理器’中点击【连接】→【数据库引擎】登陆服务器

(P.S:如果没有看到’对象资源管理器’,点击左上角的【视图】→【对象管理器】)

第三步: 登陆成功后,右击你要导入数据的数据库名,我这里数据库名是’TEST’

右击【TEST】→【任务】→【导入数据】

这时候会出现一个向导:点击【下一步】

在弹出的窗口中,【数据源】选择:Microsoft Excel ;

【Excel文件路径】就选择你本地电脑存放Excel文档的路径,我这里是D:\test\重复出修率.xlsx

【Excel版本】选择Excel 2007 (可以根据你office的版本,选择其他如Excel 2003等等。) 选中【首行包含列名称】框,最后点击【下一步】

“首行包含列名称”是指Excel中的首行数据插入数据库后将作为新建表的列名.

在弹出窗口中,【目标】可以采取默认的,【服务器名称】也是一样

可以选择身份验证,最后选择你要插入数据到哪个数据库中。

如我这里选的是’TEST’ ,选好后点击【下一步】

如果就把Excel数据插入一个新表,就选择【复制一个或多个表或视图的数据】

如果想把Excel数据插入到已存在的一张表中,则选择下面的【编写查询以指定要传输的数据】

我这里是选择上面一个,然后点击【下一步】

接着会弹出Excel表中的sheet表名字,如果你没有修改过sheet表名字,则可能显示的是’shee1$’, ’shee2$’, ’shee3$’等等.

然后你选中你想要导入的哪张表的数据,我这里导入的是【’aug$’】

选中后,右边的【目标】栏位中会出现内容,

dbo是指数据库的架构名, aug$只是新建的表名,如果你想修改表明,双击就可以表明进入编辑状态就可以修改了。我这里修改的表名称为:

aug_info

篇二: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数据导入到postgresql数据库中

如何将Excel数据(*.xls)导入到ostgresSQL数据库中

一、Excel数据(*.xls)数据文件:

二、导入步骤:

1、根据Excel文件另存为".csv”格式文件; 2、根据Excel数据的属性创建表

-- Table: public.texipos -- DROP TABLE public.texipos; CREATE TABLE public.texipos (

number0 integer, simid character varying(20), lat numeric, lon numeric, speed integer, angle integer, time0 bigint, state text, rn integer, c_time character varying(100) ) WITH (OIDS=FALSE );

ALTER TABLE public.texiposOWNER TO postgres;

创建表如图下所示

3、打开psql.exe命令行工具

4、数据导入

在命令行中输入:copy 数据表名称 form '*.csv文件路径' with delimater ',';

回车;

数据导入的结果如下图所示:


把excel导入数据库》由:免费论文网互联网用户整理提供;
链接地址:http://www.csmayi.cn/meiwen/36257.html
转载请保留,谢谢!
相关文章