- 浏览: 96373 次
- 性别:
- 来自: 北京
最新评论
-
cywhfe:
受教了,多谢lz分享
StringUtils字符串操作处理 -
carlosfu:
这么牛啊,昨天发的 800多点击啊。
牛
多线程异步事件、任务自动调度 -
liuInsect:
没看出个什么问题啊、
多线程异步事件、任务自动调度 -
Coolala_cs:
nice!
多线程异步事件、任务自动调度 -
luxing44530:
akka actor?
多线程异步事件、任务自动调度
最近应朋友请求,做一个小程序读取excel数据到Mysql数据库中,项目分析如下
1,大批量数据,多个excel,几万行数据,插入数据库,其中涉及到主外键关联;
2,附带复制每条广告信息,打折信息对应的图片,视频复制到特定文件夹下,同时以全球唯一标识符重新命名,文件名插入带数据库中
3,项目所用jar包为poi-bin-3.8-beta3-20110606.tar.gz中的
4、不足之处,请各位指正,我继续完善
1,大批量数据,多个excel,几万行数据,插入数据库,其中涉及到主外键关联;
2,附带复制每条广告信息,打折信息对应的图片,视频复制到特定文件夹下,同时以全球唯一标识符重新命名,文件名插入带数据库中
3,项目所用jar包为poi-bin-3.8-beta3-20110606.tar.gz中的
4、不足之处,请各位指正,我继续完善
/** * */ package cn.edu.zzuli; /** * @author moon * */ import java.io.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class POITest { private static Connection conn = null; private static Statement stmt = null; private static boolean connectDB() { String url = ""; String username = "root"; String password = "123456"; // 加载驱动程序以连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); url = "jdbc:mysql://127.0.0.1:3306/huang"; conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); } // 捕获加载驱动程序异常 catch (ClassNotFoundException cnfex) { System.err.println("装载JDBC驱动程序失败。"); cnfex.printStackTrace(); return false; } // 捕获连接数据库异常 catch (SQLException e) { System.err.println("无法连接数据库"); e.printStackTrace(); return false; } return true; } /** * 复制图片组件 * * @param path 文件路径 * @param picdisname 文件名称 * @return */ public static String copyFileDis(String path, String picdisname) { String sj = UUID.randomUUID().toString(); String picfileLast = picdisname.substring(picdisname.lastIndexOf(".")); String fromFile = sj + "." + picfileLast; File oldpic = new File(path + "\\" + picdisname); if(!oldpic.exists()){ System.out.println("复制文件失败,原文件不存在"+picdisname); return null; } File mepic = new File("D:\\mediaResource\\" + fromFile); int byteread = 0; InputStream in = null; OutputStream out = null; try { in = new FileInputStream(oldpic); out = new FileOutputStream(mepic); byte[] buffer = new byte[1024]; while ((byteread = in.read(buffer)) != -1) { out.write(buffer, 0, byteread); } } catch (Exception e) { e.printStackTrace(); System.out.println("复制文件失败"); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return fromFile; } /** * 查询出来ads表中ads_id的id最大值 */ public static int findMaxAds() { int maxId = 0; String sql = "select max(ads_id) from ads"; try { ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { maxId = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return maxId; } /** * 从广告说明读取数据到数据库中 * * @param path文件路径 * @return * @throws ParseException */ public static boolean readExcelToDB(String path) throws ParseException { String picname = "logo.jpg"; String dispath = path; // 复制图片,并获得uuid文件名返回给readExcelToDB String uuidjpg = POITest.copyFileDis(dispath, picname); String temp = path + "\\广告说明.xls"; POIFSFileSystem fs = null; HSSFWorkbook wb = null; try { fs = new POIFSFileSystem(new FileInputStream(temp)); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); return false; } HSSFSheet sheet = wb.getSheetAt(0); // 读取excel的sheet,0表示读取第一个、1表示第二个..... HSSFRow row = null; HSSFCell cell = null; int ads_type_id = 0; String business = ""; String introduction = ""; String address = ""; String name = ""; String contact = ""; int rowNum, cellNum; int i; rowNum = sheet.getLastRowNum(); for (i = 1; i <= rowNum; i++) { row = sheet.getRow(i); cell = row.getCell((short) 0); if(null ==cell){ continue; }else{ business = cell.getStringCellValue(); } cell = row.getCell((short) 1); if(null==cell){ continue; }else{ ads_type_id = (int) cell.getNumericCellValue(); } cell = row.getCell((short) 2); if(null == cell){ continue; }else{ introduction = cell.getStringCellValue(); } cell = row.getCell((short) 3); if(null == cell){ continue; }else{ address = cell.getStringCellValue(); } cell = row.getCell((short) 4); if(null ==cell){ continue; }else{ contact = cell.getStringCellValue(); } String sql = "insert into ads(ads_type_id, business,introduction,address,contact,image) values(" + ads_type_id + ",'" + business + "','" + introduction + "','" + address + "','" + contact + "','" + uuidjpg + "')"; try { stmt.executeUpdate(sql); } catch (SQLException e1) { e1.printStackTrace(); return false; } if (POITest.discountNews(path) == true) { System.out.println("打折详情注入成功。"); } } return true; } /** * 读取打折信息到数据库,同时复制套餐图片、视频 * @param path文件路径 * @return * @throws ParseException */ public static boolean discountNews(String path) throws ParseException { // 首先复制图片,并获得uuid文件名返回给discountNews String temp = path + "\\打折详情\\打折详情.xls"; POIFSFileSystem fs = null; HSSFWorkbook wb = null; try { fs = new POIFSFileSystem(new FileInputStream(temp)); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); return false; } HSSFSheet sheet = wb.getSheetAt(0); // 读取excel的sheet,0表示读取第一个、1表示第二个..... HSSFRow row = null; HSSFCell cell = null; int ads_type_id = 0; String label = ""; String detail = ""; Date begin_time; Date end_time; Timestamp begin_time_for; Timestamp end_time_for; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); int rowNum, cellNum; int adsId = POITest.findMaxAds(); int i; rowNum = sheet.getLastRowNum(); for (i = 1; i <= rowNum; i++) { row = sheet.getRow(i); cell = row.getCell((short) 0); //判断为空如何处理 if(null == cell){ continue; }else{ label = cell.getStringCellValue(); } String picname = label + ".png"; String dispath = path + "\\打折详情\\"; String videoname = label+".flv"; String uuidjpg = POITest.copyFileDis(dispath, picname); String videouuidname = POITest.copyFileDis(dispath, videoname); cell = row.getCell((short) 1); if(null == cell){ continue; }else{ detail = cell.getStringCellValue(); } cell = row.getCell((short) 2); if (null == cell) { continue; } else { Double begin_time_int = cell.getNumericCellValue(); begin_time = HSSFDateUtil.getJavaDate(begin_time_int); String begin_time_string = format.format(begin_time); begin_time_for = Timestamp.valueOf(begin_time_string); } cell = row.getCell((short) 3); if (null == cell) { continue; } else { Double end_time_int = cell.getNumericCellValue(); end_time = HSSFDateUtil.getJavaDate(end_time_int); String end_time_string = format.format(begin_time); end_time_for = Timestamp.valueOf(end_time_string); } String sql = "insert into discount_inf(ads_Id,label, detail,print_times,click_times,icon,begin_time,end_time,video) values("+ adsId+ ",'"+ label+ "','" + detail+ "',"+ 0+ ","+ 0+ ",'"+ uuidjpg+ "','"+ begin_time_for+ "','"+ end_time_for + "','"+videouuidname+"')"; try { stmt.executeUpdate(sql); } catch (SQLException e1) { e1.printStackTrace(); return false; } } return true; } public static void main(String[] args) throws ParseException { if (connectDB() == true) { String path = "d:\\广告模板"; File file = new File(path); String[] fileList = file.list(); for (int i = 0; i < fileList.length; i++) { String temp = path + "\\" + fileList[i]; if (readExcelToDB(temp) == true) System.out.println("数据导入成功"); else System.out.println("数据导入失败"); } } } }
发表评论
-
hive常用资料整理
2013-09-22 10:28 659hive基本wiki FaceBook 镜像(被墙):h ... -
一步步构建大型网站架构
2013-04-12 13:01 749一步步构建大型网站架构 之前我简单向大家介绍了各个知名 ... -
ZooKeeper典型应用场景一览
2013-04-01 18:51 916值得注意的是,ZK并非 ... -
nginx常用命令
2012-12-10 16:26 797/home/nginx/conf/ n ... -
maven项目管理工具常见命令
2012-07-09 11:16 1414jar包查询:http://mvnrepository.co ... -
excel通用模板组件
2012-01-16 11:17 2100最近在公司实习时,项目中总遇到excel表的导出,就产生写一个 ... -
eclipse下插件svn的安装
2011-12-05 15:45 905在eclispe目录,如D:\Program Files\My ... -
闲话淘宝网和新浪微博架构
2011-11-01 10:53 1799如今分布式系统在国 ... -
迈向架构师的第一步
2011-10-28 16:47 697有一个多月没有写blog ... -
浅谈大型网站动态应用系统架构
2011-10-16 20:14 751动态应用,是相对于网站静态内容而言,是指以c/c++、ph ... -
JavaEE程序员必读图书大推荐
2011-09-21 17:48 592下面是我根据多年的阅读和实践经验,给您推荐的一些图书: ... -
分布式存储系统Hadoop、hypertable
2011-09-17 13:27 1269分布式系统(distributed system)是建立在网络 ... -
我在华为工作十年
2011-07-31 10:05 910学习前人的经历,从成 ... -
软件开发面试百问
2011-07-21 20:36 809文/Jurgen Appelo 译/李剑 想雇 ... -
平衡艺术-从菜鸟到架构师
2011-07-21 20:33 801胡喜 作为一个不是科班出身、没有正规学习过计算机知 ... -
话说程序员的职业生涯
2011-07-21 20:24 663IBM软件集团大中华区总 ...
相关推荐
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL查看数据库 在MYSQL中运行项目db目录下的sql文件,创建...
本案例通过poi对Excel操作,读取Excel数据并导入数据库,并根据配置文件使用tools.jar动态生成DynamicExcel类,使用方便。。。过几天有时间会对其扩展。。
Java读取txt数据入数据库然后读出使用POI创建excel
用poi实现读取excel,然后传入数据库,包括一些字符的转换(date,string,int)
使用了POI操作读取excel、mybatis连接数据库。。。。。。
1、POI_EXCEL包下分别有两个类,一个是读取excel内容,一个是想excel写入内容 2、cn.itcast包下的所有包,是为了实现从excel写入到数据库中,和从数据库写入到excel中 注意事项: 1、需要导入maven工程 2、使用...
这个是使用java语言 poi导入excel到数据库中,同时使用了xml对excel的每个单元格数据进行了非空验证。经测试,导入数据库成功。
项目需要读取Excel,将数据导入数据库保存,最终可以用报表读取数据库并显示,比较简单。本文介绍了使用POI读取Excel数据并导入DB2数据库的方法。
NULL 博文链接:https://longying2008.iteye.com/blog/1902450
java POI完整示例,POI将word转HTML,数据库倒出数据到Excel等
Excel导入数据库工具 一个自己开发的用于Exel导入数据的小工具,方便简洁。
/** 读取Excel表格数据 */ public List, String>> readExcel(String excelName, Integer sheetIndex, String startPoint, String endPoint) throws Exception { FileInputStream inputStream = null; ...
poi 方式导入 大量excel数据,将excelDir目录下文件考入f盘即可测试 poi 方式导入 大量excel数据,将excelDir目录下文件考入f盘即可测试
有时需要将excel文件的数据导入到数据库中,如何能快速而简单的将excel文件导入对不会的攻城狮们尤为重要,要干活向要jar包
注意:该资源属于写数据到Excel,而非读取Excel数据保存到数据库,保存到数据库的例子资源:http://download.csdn.net/download/a13432421434/9620001
这个demo主要是用来演示读取excel的数据存储到Mysql数据库l中
java用poi读取和创建excel 把excel中的数据导入或导出到数据库
jsp技术结合servler实现excel文件的导出,excel数据从servlet里读取,你可让其封装方法从数据库里读取。