實現過程當中遇到巨多的bug, 初步估計至少20個異常,可是最後仍是實現了. 具體的過程不細說了, 把大概須要注意的幾點說一下java
1 基本步驟是: java代碼讀取excel表中的數據, dorado中配置mysql的鏈接, 設計對應excel的表, 設計映射表, mysql
其中我遇到的一些坑: mysql數據庫不支持序列化, 而聯合主鍵又得有主鍵, 至今不知道怎麼處理聯合主鍵, 我是直接添加了一個id做爲主鍵spring
保存的時候用了hibernate的save接口, 可是沒法保存到數據庫中, 緣由有幾點: 配置文件沒有<prop key="hibernate.connection.autocommit">true</prop>sql
或者是沒有寫@Transaction註解, 就沒提交數據庫
配置mysql的時候,必定要仔細, 還要導入相應的jar包, 通常classNotFound就是jar包沒有導入的問題apache
數據庫設計的時候, 字段不能有關鍵字 如 descsession
導入mysql方言的時候hibernate.dialect=org.hibernate.dialect.MySQL5Dialect其中的y是小寫, 寫成大寫就涼了app
2 個人基本代碼以下數據庫設計
實體類, excel表格中除了沒有id, 別的字段都有, 我開始以name和datee做爲主鍵, 走了不少彎路url
@Entity
@Table(name="EXDATA")
public class ExcelEntity {
//private static final long serialVersionUID = 6076304611179489259L;
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name = "dept")
private String dept;
@Column(name="datee")
private String datee;
@Column(name = "result")
private String result;
@Column(name = "lateTime")
private String lateTime;
@Column(name = "beforeTime")
private String beforeTime;
@Column(name = "goWork")
private String goWork;
@Column(name = "afterWork")
private String afterWork;
@Column(name = "descc")
private String descc;
@Column(name = "sign")
private String sign;
@Column(name = "deptLeader")
private String deptLeader;
@Column(name = "govLeader")
private String govLeader;
繼承自hibernateD的dao類
@Repository
public class ExcelDao extends HibernateDao<ExcelEntity,Integer>{
}
實現類
@Component
public class ExcelDemo {
// 注入dao對象
@Resource
public ExcelDao excelDao;
@Transactional
@Expose
public void getAllByExcel(){
List<ExcelEntity> list=new ArrayList<ExcelEntity>();
try{
// 獲取excel表格對象
Workbook rwb=Workbook.getWorkbook(new File("E:\\創新產品事業部12月未正常打卡記錄.xls"));
Sheet rs=rwb.getSheet(0);
int clos=rs.getColumns();//獲得全部的列
int rows=rs.getRows();//獲得全部的行
int index=0;
for (int i =2; i < rows; i++) {//除去表頭,應該從第三行開始
for (int j = 0; j < clos; j++) {
//第一個是列數,第二個是行數
String name=rs.getCell(j++, i).getContents();
String dept=rs.getCell(j++, i).getContents();
String datee=rs.getCell(j++, i).getContents();
String result=rs.getCell(j++, i).getContents();
String lateTime=rs.getCell(j++, i).getContents();
String beforeTime=rs.getCell(j++, i).getContents();
String goWork=rs.getCell(j++, i).getContents();
String afterWork=rs.getCell(j++, i).getContents();
String descc=rs.getCell(j++, i).getContents();
String sign=rs.getCell(j++, i).getContents();
String deptLeader=rs.getCell(j++, i).getContents();
String govLeader=rs.getCell(j++, i).getContents();
// 將一行數據封裝到實體中
ExcelEntity entity=new ExcelEntity();
entity.setAfterWork(afterWork);
entity.setBeforeTime(beforeTime);
entity.setDept(dept);
entity.setDeptLeader(deptLeader);
entity.setdescc(descc);
entity.setGovLeader(govLeader);
entity.setGoWork(goWork);
entity.setLateTime(lateTime);
entity.setResult(result);
entity.setSign(sign);
entity.setName(name);
entity.setDatee(datee);
entity.setId(index++);
list.add(entity);
}
}
}catch(Exception e){
e.printStackTrace();
}
//保存操做
for (ExcelEntity excelEntity : list) {
System.out.println(excelEntity);
excelDao.save(excelEntity);
System.out.println("save執行完畢");
}
}
}
最後是一些配置文件, 雖然用的少,可是真的重要 app-context.xml, 還有一個配置文件, 是configure.propertiesl就是存放一些屬性供app-context的變量使用,比較簡單
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="sample" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url"
value="jdbc:mysql:${jdbc.url}?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="${jdbc.userName}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="entityInterceptor" ref="dorado.unByteCodeProxyInterceptor" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.showSql}</prop>
<prop key="hibernate.format_sql">${hibernate.formatSql}</prop>
<prop key="hibernate.connection.autocommit">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com/bstek/dorado/sample/entity</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<context:component-scan base-package="com.bstek.dorado.sample" />
</beans>
隨便附上excel導出的代碼
@Expose
public void getAllBySql(){
/**
* 從數據庫讀取數據
*/
List<ExcelEntity> list=new ArrayList<ExcelEntity>();
list=excelDao.getAll();
// 將數據保存到excel中
//開始寫入excel,建立模型文件頭
String[] titleA = {"id","姓名","部門","日期","考勤結果","遲到時間","早退時間","上班","下班","說明","簽字","領導","老闆"};
//導入的excel的位置
File file = new File("e:/TestFile.xls");
if(file.exists()){
//若是文件存在就刪除
file.delete();
}
try{
file.createNewFile();
//建立工做簿
WritableWorkbook workbookA = Workbook.createWorkbook(file);
WritableSheet sheetA = workbookA.createSheet("sheet1", 0);
Label labelA = null;
//設置列名
for (int i = 0; i < titleA.length; i++) {
labelA = new Label(i,0,titleA[i]);//第0行
sheetA.addCell(labelA);
}
// 設置單元格屬性
WritableCellFormat wc = new WritableCellFormat();
// 設置居中
wc.setAlignment(Alignment.CENTRE);
// 設置邊框線
wc.setBorder(Border.ALL, BorderLineStyle.THIN);
//插入數據
int i=1;//從第二行開始
for (ExcelEntity excelEntity : list) {
int j=0;//從第1列開始
Integer id=excelEntity.getId();
//向特定格子寫入數據
Label label = new Label(j++, i,id.toString(),wc);
sheetA.addCell(label);
Label label01 = new Label(j++, i,excelEntity.getName(),wc);
sheetA.addCell(label01);
Label label02 = new Label(j++, i,excelEntity.getDept(),wc);
sheetA.addCell(label02);
Label label03 = new Label(j++, i,excelEntity.getDatee(),wc);
sheetA.addCell(label03);
Label label04 = new Label(j++, i,excelEntity.getResult(),wc);
sheetA.addCell(label04);
Label label05 = new Label(j++, i,excelEntity.getLateTime(),wc);
sheetA.addCell(label05);
Label label06 = new Label(j++, i,excelEntity.getBeforeTime(),wc);
sheetA.addCell(label06);
Label label07 = new Label(j++, i,excelEntity.getGoWork(),wc);
sheetA.addCell(label07);
Label label09 = new Label(j++, i,excelEntity.getAfterWork(),wc);
sheetA.addCell(label09);
Label label08 = new Label(j++, i,excelEntity.getdescc(),wc);
sheetA.addCell(label08);
i++;
}
workbookA.write();
workbookA.close();
} catch (Exception e) { e.printStackTrace(); System.out.println("文件寫入失敗,報異常..."); }