java中使用JXL對Excel文件進行數據的寫入、導出操作

最近做項目需要用到批量導入數據庫的功能,最後選擇了比較常規的方式,使用了jxl組建來操作excel文件,這裏做下小小的記錄以便今後查閱。

1、讀取Excel中的文件

     Excel中有文件,工作簿,和單元格的概念,這裏都有別的不多說了直接上代碼。

package com.wsp.importFromExcel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class UploadAction{
 
 public List readDeptInfoVO(File file) throws Exception {
  List listdeps = new ArrayList();
  StringBuffer sb = new StringBuffer();

//創建excel文件的引用
  Workbook wb = null;
  try {
   // 獲取Excel文件對象

   wb = Workbook.getWorkbook(file);
   if (wb != null) {
    // 獲取了Excel文件就可以獲取Excel文件中的sheet工作簿了

    Sheet[] sheets = wb.getSheets();
    if (sheets != null && sheets.length != 0) {
     // 獲取該工作表內的行數
     int rows = sheets[0].getRows();//sheets[0]表示第1個工作簿
     // 遍歷行
     for (int j = 1; j < rows; j++) {
      // 獲取當前行的所有單元格
      Cell[] cells = sheets[0].getRow(j);
      if (cells != null && cells.length != 0) {
       System.out.println("Excel中的內容=cells[0]=="+cells[0].getContents().trim());
       System.out.println("Excel中的內容=cells[1]=="+cells[1].getContents().trim());
       System.out.println("Excel中的內容=cells[2]=="+cells[2].getContents().trim());
       System.out.println("Excel中的內容=cells[3]=="+cells[3].getContents().trim());
       System.out.println("Excel中的內容=cells[4]=="+cells[4].getContents().trim());
       System.out.println("Excel中的內容=cells[5]=="+cells[5].getContents().trim());
       System.out.println("Excel中的內容=cells[6]=="+cells[6].getContents().trim());
       System.out.println("Excel中的內容=cells[7]=="+cells[7].getContents().trim());
       System.out.println("Excel中的內容=cells[8]=="+cells[8].getContents().trim());
       System.out.println("Excel中的內容=cells[9]=="+cells[9].getContents().trim());
      }
     }

    }
   }
  } catch (Exception e) {
   System.out.println(e.getMessage());
  } finally {
   wb.close();
  }
  return listdeps;
 }
 public static void main(String args[]) {
  File file=new File("F:"+File.separator+"import.xls");
  try {
   file.createNewFile();
   new UploadAction().readDeptInfoVO(file);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
 

 

2、想Excel中寫入內容

package com.wsp.importFromExcel;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ImportAction {
 public List writeDeptInfoVO(File file) throws Exception  {
  List listdeps = new ArrayList();
  StringBuffer sb = new StringBuffer();
  // 創建用於寫入內容的Excel文件的引用
  WritableWorkbook wb=null;
  try {
   // 獲取Excel文件
   wb=Workbook.createWorkbook(file);
   if (wb != null) {
    // 通過Excel文件獲取第一個工作簿sheet
    WritableSheet sheets=wb.createSheet("sheet1", 0);
     //構建Excel表的表頭
       //Label label=new Label(i,j);//i表示列的位置,j表示行的位置
     Label label1=new Label(0, 0, "用戶名稱");//第j行,第1列中的內容
     sheets.addCell(label1);
     Label label2=new Label(1, 0, "用戶手機");//第j行,第2列中的內容
     sheets.addCell(label2);
     Label label3=new Label(2, 0, "用戶郵箱");//第j行,第0列中的內容
     sheets.addCell(label3);
     Label label4=new Label(3, 0, "用戶地址");//第j行,第3列中的內容
     sheets.addCell(label4);
     Label label5=new Label(4, 0, "用戶生日");//第j行,第4列中的內容
     sheets.addCell(label5);
     // rows表示需要寫入Excel文件的行數
     int rows = 5;
     for (int j = 1; j < rows; j++) {//j表示行數,i表示列數
      Label label11=new Label(0, j, "用戶名稱");//第j行,第0列中的內容
      sheets.addCell(label11);
      Label label21=new Label(1, j, "用戶手機");//第j行,第0列中的內容
      sheets.addCell(label21);
      Label label31=new Label(2, j, "用戶郵箱");//第j行,第0列中的內容
      sheets.addCell(label31);
      Label label41=new Label(3, j, "用戶地址");//第j行,第0列中的內容
      sheets.addCell(label41);
      Label label51=new Label(4, j, "用戶生日");//第j行,第0列中的內容
      sheets.addCell(label51);
     }
   }
  } catch (Exception e) {
   System.out.println(e.getMessage());
  } finally {
   wb.write();
   wb.close();
  }
  return listdeps;
 }
 public static void main(String args[]) {
  File file=new File("F:"+File.separator+"import2.xls");
  try {
   file.createNewFile();
   new ImportAction().writeDeptInfoVO(file);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章