很久沒有發(fā)BLOG,呵呵,是因?yàn)樽罱趲椭笥言谧鲆粋€(gè)項(xiàng)目,很忙。呵呵,我覺得生活就應(yīng)該是這樣,年輕時(shí)候的忙碌是為年老時(shí)候的悠閑吧。
主要涉及內(nèi)容及技術(shù):
javaexcel api
jasonreport及編輯工具ireport和數(shù)據(jù)庫技術(shù)。
導(dǎo)入及導(dǎo)出EXCEL解決思路:
使用javaexcel api導(dǎo)入普通的EXCEL表格,就是沒有合并單元格的規(guī)范數(shù)據(jù),如果導(dǎo)出有規(guī)則的EXCEL也使用JAVAEXECEL API,對(duì)中文支持比較好。
如果要導(dǎo)出報(bào)表可打印的EXCEL,使用ireport為工具圖形化畫出報(bào)表,并使用jasonreprt控制導(dǎo)出,展示給客戶端。
主要代碼有:
連接數(shù)據(jù)庫的方法:
java 代碼
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Hashtable;
- import java.util.Vector;
-
- import org.apache.log4j.Logger;
-
- public class DBOperate {
-
-
-
-
-
-
- public static String getSequence(String sequenceName) {
- String nextval = "";
- if(sequenceName == null || "".equals(sequenceName)) {
- System.out.println("sequenceName name is null!");
- return null;
- } else {
- sequenceName = sequenceName.toUpperCase();
- String sql = "select " + sequenceName + ".nextval nextid from user_sequences where sequence_name = '"
- + sequenceName + "'";
-
- Statement stmt = null;
- Connection conn = null;
- ResultSet rs = null;
-
- try {
- conn = TreatDB.getConnDB("");
- if (conn != null) {
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
-
- if(rs != null) {
- while(rs.next()) {
- nextval = rs.getString("nextid");
- }
- }
- }
- } catch (SQLException e) {
- System.out.println("SQLException : " + e);
- } finally {
- try {
- if(rs != null) rs.close();
- if(stmt != null) stmt.close();
- if(conn != null) conn.close();
- } catch (SQLException e) {
-
- }
- }
- return nextval;
- }
- }
-
-
-
-
-
- public static int updateSql(String sql) {
- String dbName = "";
- Connection conn = null;
- Statement stmt = null;
- int result = 0;
- try {
- conn = TreatDB.getConnDB(dbName);
- if (conn != null) {
- stmt = conn.createStatement();
-
- result = stmt.executeUpdate(sql);
- conn.commit();
- }
-
- } catch (SQLException e) {
- try {
- conn.rollback();
- } catch (SQLException e1) {
- System.out.println("update sql error: "+e);
-
- }
- System.out.println("update sql error: "+e);
- System.out.println("sql: " + sql);
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- if (conn != null)
- conn.close();
- } catch (Exception ex) {
-
- }
-
- }
- return result;
- }
-
- }
導(dǎo)入EXCEL的程序代碼:
java 代碼
-
-
-
-
-
-
-
- public String addJLXlsForm(InputStream is) {
-
- String errStr = "";
- String result = "";
-
- try {
- Workbook rwb = Workbook.getWorkbook(is);
-
-
- Sheet sheet = rwb.getSheet(0);
-
- int rsColumns = sheet.getColumns();
- System.out.println("rsColumns = " + rsColumns);
-
-
- int rsRows = sheet.getRows();
- System.out.println("rsRows = " + rsRows);
-
- if (rsColumns < 19) {
- errStr = "錯(cuò)誤原因:字段不全。";
- vErr.addElement(errStr);
- }
-
- int i = 0;
- int startRows = 1;
- int startColumn = 0;
- while (startRows < rsRows) {
- Hashtable ht = new Hashtable();
-
- String zj_id = DBOperate.getSequence("S_JIANLI");
-
-
- Cell tmp = sheet.getCell(0,startRows);
- String sfzh = tmp.getContents();
-
- tmp = sheet.getCell(1,startRows);
- String issueDate = tmp.getContents();
- String issueDate_new = "";
- if(issueDate != null && issueDate.length()>=6) issueDate_new = issueDate.substring(6) + issueDate.substring(3,5) + issueDate.substring(0,2);
-
- tmp = sheet.getCell(2,startRows);
- String approveDate = tmp.getContents();
- String approve_date_new = "";
- if(approveDate != null && approveDate.length()>=6) approve_date_new = approveDate.substring(6) + approveDate.substring(3,5) + approveDate.substring(0,2);
-
- tmp = sheet.getCell(3,startRows);
- String cerNo = tmp.getContents();
-
- tmp = sheet.getCell(4,startRows);
- String jianliMajor = tmp.getContents();
-
- tmp = sheet.getCell(5,startRows);
- String department = tmp.getContents();
-
- tmp = sheet.getCell(6,startRows);
- String jianliName = tmp.getContents();
-
- tmp = sheet.getCell(7,startRows);
- String jianliSex = tmp.getContents();
-
-
- tmp = sheet.getCell(8,startRows);
- String degree = tmp.getContents();
-
-
- tmp = sheet.getCell(9,startRows);
- String cooleage = tmp.getContents();
-
-
- tmp = sheet.getCell(10,startRows);
- String company = tmp.getContents();
-
-
- tmp = sheet.getCell(11,startRows);
- String tech_post = tmp.getContents();
-
-
- tmp = sheet.getCell(12,startRows);
- String address = tmp.getContents();
-
-
- tmp = sheet.getCell(13,startRows);
- String postcode = tmp.getContents();
-
- tmp = sheet.getCell(14,startRows);
- String jianliTel = tmp.getContents();
-
- tmp = sheet.getCell(15,startRows);
- String mobile = tmp.getContents();
-
- tmp = sheet.getCell(16,startRows);
- String email = tmp.getContents();
-
- tmp = sheet.getCell(17,startRows);
- String birthday = tmp.getContents();
- String birthday_new = "";
- if(birthday != null && birthday.length()>=6) birthday_new = birthday.substring(6) + birthday.substring(3,5) + birthday.substring(0,2);
-
-
- tmp = sheet.getCell(18,startRows);
- String jianli_type = tmp.getContents();
-
- String sql = "insert into jianli values('" + zj_id + "', '" + sfzh + "', to_date('" + issueDate_new + "','yyyymmdd'),to_date('"
- + approve_date_new + "','yyyymmdd'),'" + cerNo + "','" + jianliMajor + "','" + department + "','"
- + jianliName + "','" + jianliSex + "','" + degree + "','" + cooleage + "','"
- + company + "','" + tech_post + "','" + address + "','" + postcode + "','" + jianliTel
- + "','" + mobile + "','" + email + "',to_date('" + birthday_new + "','yyyymmdd'),'" + jianli_type + "',null,null,null,null,"+sfzh.substring(0, 10)+",null,null,null,null,null)";
-
- int a = DBOperate.updateSql(sql);
-
- System.out.println("成功導(dǎo)入第"+startRows+"條");
- if (a == 0) {
- errStr = errStr + "添加失敗:庫中已有此記錄,或有值不符合庫中設(shè)定的字段屬性!";
- } else if (a == 1) {
- result = "添加成功";
- } else {
- errStr = errStr + "添加失敗:連接數(shù)據(jù)庫失敗";
- }
-
-
-
- if (!"".equals(errStr)) {
- vErr.addElement("導(dǎo)入:" + "【" + startRows + "】" + "<br>(錯(cuò)誤原因:" + errStr);
- errStr += "導(dǎo)入:" + "【" + startRows + "】" + "<br>(關(guān)鍵字段為:姓名:"+jianliName+",身份證號(hào)為:"+sfzh+",監(jiān)理證書號(hào)為:"+cerNo+")<br>";
- } else {
- vOk.add(ht);
- }
-
- startRows++;
- }
-
- rwb.close();
- result = "成功導(dǎo)入:" + vOk.size()+"條記錄。<br> 錯(cuò)誤導(dǎo)入: " + vErr.size()+"條記錄,錯(cuò)誤記錄及原因如下:<br>"+errStr;
- System.out.println("vOK.size() = " + vOk.size());
- System.out.println("VErr.size() = " + vErr.size());
- return result;
- } catch (Exception e) {
- result = "在導(dǎo)入的過程當(dāng)中發(fā)生了錯(cuò)誤,其中錯(cuò)誤的原因是:" + e.toString();
- return result;
- }
- }