一、寫入 Excel 文件:
- /**
- *
- * <p>Discription:[寫入Excel]</p>
- * @param fileName
- * @param list
- * @author:[LJ]
- * @update:[2012-4-9] [LJ][創建]
- */
- public static void writeExcel(String fileName, List list)
- {
- WritableWorkbook wwb = null;
- try
- {
- //首先使用Workbook類的工廠方法創建一個可寫入的工作薄(Workbook)對象
- wwb = Workbook.createWorkbook(new File(fileName));
- }
- catch (IOException e)
- {
- System.out.println("創建工作簿失敗!");
- }
- if(wwb != null)
- {
- //創建一個可寫入的工作表
- //Workbook的createSheet方法有兩個參數,第一個是工作表的名稱,第二個是工作表在工作薄中的位置
- WritableSheet ws = wwb.createSheet("mySheet1", 0);
- for(int i = 0,n = list.size();i < n;i++)
- {
- EmpBean emp = (EmpBean)list.get(i);
- //這裏需要注意的是,在Excel中,第一個參數表示列,第二個表示行,第三個表示單元格內容
- Label labelEmpNo = new Label(0, i, emp.getEmpNo());
- Label labelEname = new Label(1, i, emp.getEname());
- Label labelJob = new Label(2, i, emp.getJob());
- Label labelMgr = new Label(3, i, emp.getMgr());
- Label labelHireDate = new Label(4, i, emp.getHireDate());
- Label labelSal = new Label(5, i, emp.getSal());
- Label labelComm = new Label(6, i, emp.getComm());
- Label labelDeptNo = new Label(6, i, emp.getDeptNo());
- try
- {
- //將生成的單元格添加到工作表中
- ws.addCell(labelEmpNo);
- ws.addCell(labelEname);
- ws.addCell(labelJob);
- ws.addCell(labelMgr);
- ws.addCell(labelHireDate);
- ws.addCell(labelSal);
- ws.addCell(labelComm);
- ws.addCell(labelDeptNo);
- }
- catch (RowsExceededException e)
- {
- e.printStackTrace();
- }
- catch (WriteException e)
- {
- e.printStackTrace();
- }
- }
- try
- {
- //從內存中寫入文件中
- wwb.write();
- //關閉資源,釋放內存
- wwb.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- catch (WriteException e)
- {
- e.printStackTrace();
- }
- }
- }
二、讀取 Excel 文件:
- /**
- *
- * <p>Discription:[讀取Excel]</p>
- * @param file
- * @return
- * @author:[LJ]
- * @update:[2012-4-9] [LJ][創建]
- */
- public static String readExcel(File file)
- {
- StringBuffer sb = new StringBuffer();
- Workbook wb = null;
- try
- {
- //構造Workbook(工作薄)對象
- wb=Workbook.getWorkbook(file);
- }
- catch (BiffException e)
- {
- e.printStackTrace();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- if(wb==null)
- {
- return null;
- }
- //獲得了Workbook對象之後,就可以通過它得到Sheet(工作表)對象了
- Sheet[] sheet = wb.getSheets();
- if(sheet!=null&&sheet.length>0)
- {
- //對每個工作表進行循環
- for(int i=0;i<sheet.length;i++)
- {
- //得到當前工作表的行數
- int rowNum = sheet[i].getRows();
- for(int j=0;j<rowNum;j++)
- {
- //得到當前行的所有單元格
- Cell[] cells = sheet[i].getRow(j);
- if(cells!=null&&cells.length>0)
- {
- //對每個單元格進行循環
- for(int k=0;k<cells.length;k++)
- {
- //讀取當前單元格的值
- String cellValue = cells[k].getContents();
- sb.append(cellValue+",");
- }
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append(";");
- }
- sb.deleteCharAt(sb.length()-1);
- sb.append("。");
- }
- }
- //最後關閉資源,釋放內存
- wb.close();
- return sb.toString();
- }
三、準備數據:
- /**
- *
- * <p>Discription:[獲取數據]</p>
- * @return
- * @author:[LJ]
- * @update:[2012-4-9] [LJ][創建]
- */
- public static List<EmpBean> getEmp()
- {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- String sql = "select * from emp";
- List<EmpBean> empList = new ArrayList<EmpBean>();
- try
- {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- //獲取字段名稱
- ResultSetMetaData metaData = rs.getMetaData();
- for (int i = 1; i <= metaData.getColumnCount(); ++i)
- {
- EmpBean emp = new EmpBean();
- emp.setEmpNo(metaData.getColumnName(i));
- emp.setEname(metaData.getColumnName(++i));
- emp.setJob(metaData.getColumnName(++i));
- emp.setMgr(metaData.getColumnName(++i));
- emp.setHireDate(metaData.getColumnName(++i));
- emp.setSal(metaData.getColumnName(++i));
- emp.setComm(metaData.getColumnName(++i));
- emp.setDeptNo(metaData.getColumnName(++i));
- empList.add(emp);
- //StringBuffer str = new StringBuffer();
- //獲取字段類型名稱和精度
- //str.append(metaData.getColumnTypeName(i)+"("+metaData.getPrecision(i)+")");
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- while(rs.next())
- {
- EmpBean emp = new EmpBean();
- emp.setEmpNo(rs.getString("EMPNO"));
- emp.setEname(rs.getString("ENAME"));
- emp.setJob(rs.getString("JOB"));
- emp.setMgr(rs.getString("MGR"));
- emp.setHireDate(sdf.format(rs.getDate("HIREDATE")));
- emp.setSal(rs.getString("SAL"));
- emp.setComm(rs.getString("COMM"));
- emp.setDeptNo(rs.getString("DEPTNO"));
- empList.add(emp);
- }
- }
- catch (Exception e)
- {
- System.out.println("查詢失敗!");
- }
- finally
- {
- try
- {
- if(rs!=null)
- {
- rs.close();
- }
- if(stmt!=null)
- {
- stmt.close();
- }
- if(conn!=null)
- {
- conn.close();
- }
- }
- catch (SQLException e)
- {
- System.out.println("關閉失敗!");
- }
- }
- return empList;
- }
四、測試主函數:
- /**
- * <p>Discription:[main函數]</p>
- * @param args
- * @author:[LJ]
- * @update:[2012-4-7] [LJ][創建]
- */
- public static void main(String[] args)
- {
- //獲取數據
- List<EmpBean> list = getEmp();
- //將數據寫入Excel文件
- writeExcel("D:\\myExcel.xls", list);
- //讀出Excel文件的內容
- readExcel(new File("D:\\myExcel.xls"));
- }
PS : 讀寫 Excel 文件需要引入 jxl.jar 包,下載地址:
http://www.andykhan.com/jexcelapi/download.html