Java完成操作excel表格。本站提示廣大學習愛好者:(Java完成操作excel表格)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成操作excel表格正文
比來先生安排了個義務,用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;
}