Java實現操作excel表格_java

來源:互聯網
上載者:User

  最近老師布置了個任務,用Java對excel尾碼名為xlsx的檔案進行簡單的增,刪,改,查操作;雖說是個簡單的程式,可作為剛接觸的我來說還是有些磕磕碰碰。不過好在還是完成了,進行一個簡單的總結。

  首先匯入了一個poi.jar 網上有很多這個資源可以下載

XSSFSheet sheet=null;
XSSFWorkbook book=null;

一:查  (尋找本地指定位置的excel表格,在控制台輸出) 

public void print_excel(){  //擷取excel表格的行數  int lastrownumber = sheet.getLastRowNum();  String ret=" ";  //擷取資料  for(a=0;a<lastrownumber;a++){    XSSFRow row=sheet.getRow(a);    //擷取excel表格的列數    int lastcellnum=row.getLastCellNum();    for(b=0;b<lastcellnum;b++){      XSSFCell cell =row.getCell(b);      //判斷cell返回的類型並賦值給ret      ret=excel_operation.getExcelCellValue(cell);      System.out.print(ret+" ");    }  System.out.println();  } }

二:改 (修改excel表格中某一儲存格的內容)

public void set_excelcell(int i,int j,String str){  //擷取行的資訊  XSSFRow row=sheet.getRow(i-1);  //擷取列的資訊  XSSFCell cell =row.getCell(j-1);  //擷取被修改儲存格的內容  String string = excel_operation.getExcelCellValue(cell);  //修改儲存格的內容為str  cell.setCellValue(str);  System.out.println("已將"+string+"改為"+str);}

三:增 (在excel表格中插入一行內容到指定位置)

public void insert(int rowIndex, String[] objs) {   if(rowIndex == 0) {     throw new IllegalArgumentException("不能插在第0行,第0行是用來定義的!");   }   if(rowIndex > sheet.getLastRowNum() + 1) {     throw new IllegalArgumentException("最多隻能插入在最後一行的後面。");   }   int referRowIndex = -1; //參考行的行號。   if(sheet.getPhysicalNumberOfRows() <= 1) {     referRowIndex = rowIndex - 1;   } else {     referRowIndex = rowIndex - 1;   if(rowIndex == sheet.getLastRowNum() + 1) { //是插入最後一行     //不做任何處理   } else {     //往下移動一位     sheet.shiftRows(rowIndex, sheet.getLastRowNum(), 1, true, false);     }   }   Row targetRow = sheet.createRow(rowIndex);   Row referRow = sheet.getRow(referRowIndex); // 參考行   Cell targetCell, referCell;   for (int i = 0; i < objs.length; i++) {   targetCell = targetRow.createCell(i);   referCell = referRow.getCell(i);   targetCell.setCellStyle(referCell.getCellStyle());   targetCell.setCellType(referCell.getCellType());   targetCell.setCellValue(objs[i]);// 設定值   } }

四: 刪 (刪除指定行的內容)

// 刪除一行資料(Excel表中,行是從0起算的)   public void delete(int rowIndex) {  //刪除的是最後一行   if(rowIndex == sheet.getLastRowNum()) {     sheet.removeRow(sheet.getRow(sheet.getLastRowNum()));  //刪除的不是最後一行   } else {     sheet.shiftRows(rowIndex + 1, sheet.getLastRowNum(), -1, true, false);     sheet.removeRow(sheet.getRow(sheet.getLastRowNum() + 1));   } }

五: 判斷傳回型別  (因為excel表格中的內容不同,有字元型的,有整數型的等等,必須進行判斷其類型才能進行輸出)

private static String getExcelCellValue(XSSFCell cell) {  String ret=" ";  try {    //當傳回值的類型為空白返回空格    if (cell == null) {      ret = " ";     //當傳回值的類型為字串類型    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {      ret = cell.getStringCellValue();    //當傳回值的類型為數實值型別     } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {      ret = "" + cell.getNumericCellValue();    //當傳回值的類型為運算式類型    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {      ret = cell.getCellFormula();    //當傳回值的類型為異常類型    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {      ret = " " + cell.getErrorCellValue();    //當傳回值的類型為布爾類型     } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {      ret = " " + cell.getBooleanCellValue();    //當傳回值的類型為空白的時候    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {    ret = " ";    }  } catch (Exception ex) {    ex.printStackTrace();  ret = " ";  }  return ret;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.