최근 교사는 Java를 사용하여 Excel 접미사 XLSX를 단순히 추가, 삭제, 수정 및 확인하는 작업을 할당했습니다. 다행히도 완료되어 간단한 요약을했습니다.
먼저 Poi.jar를 가져 왔습니다.
xssfsheet sheet = null;
xssfworkbook book = null;
1 : 확인 (로컬 지정된 위치에서 Excel 테이블 찾기, 콘솔에서 출력).
public void print_excel () {// Excel 테이블의 행 수를 얻습니다. XSSFROW ROW = 시트. b); // 반환 된 유형에 따라 ret ret = excel_operation (셀);2 : 변경 (Excel 테이블에서 셀의 내용을 수정)
public void set_excelcell (int i, int j, string str) {// 실적 xssfrow row = getrow (i-1); 수정 된 셀 String의 내용을 얻으십시오. +str);}3 : 추가 (Excel 테이블의 목차를 지정된 위치에 삽입)
public void insert (int rowIndex, string [] objs) {if (rowindex == 0) {새로운 불법 행정 exception ( "0 행에 삽입 할 수 없으며 0 행은 정의하는 데 사용됩니다!} if (rowindex> sheet. getLastrownum () + 1) {새로운 불법 행위 (마지막 줄 후에 만 삽입 할 수 있습니다.} int referrowindex = -1; if (sheet.getPhysicalNumberOfrows () <= 1) {referrowindex = rowindex} else {referrowindex = rowindex -1; // 처리하지 않습니다} else {// 하나의 시트를 이동합니다. Shiftrows (RowIndex, sheet.getLastrownum (), true, false); .GetRow (참조 라인 셀 TargetCell); TargetCell.SetCellStyle (참조 셀카 셀 ());4 : 삭제 (지정된 라인의 내용 삭제)
// 데이터 행을 삭제 (Excel 테이블에서 행은 0부터 계산됩니다) public void delete (int rowindex) {// 삭제 된 마지막 행은 if (rowindex == sheet.getLastrownum ()) {sheet.removerow (시트. getlastownum ()); .remov erow (시트.5 : 리턴 유형을 결정합니다 (Excel 테이블의 내용은 문자 유형, 정수 유형 등을 포함하여 다르기 때문에 출력 전에 유형을 판단해야합니다).
개인 정적 문자열 getExcellValue (xssfcell 셀) {string ret = ""; cell.getCellType () == xssfcell.cell_type_string) {ret = cell.getStringCellValue (); "" + cell.getnumericcellValue (); // return 값의 유형이 expression 유형 일 때} (cell.getCellType () == xssfcell.cell_type_formula) {ret = cell.getcellf ormula (); 반환 값은 예외 유형입니다} else if (cell.getCellType () == xssfcell.cell_type_error) {ret = "" + cell.geterRorcellValue ()가 부울 유형 인 경우}; else if (셀 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :. else if (cell.getCellType () == XSSFCEL l.cell_type_Blank) {ret = "} {ex.printstacktrace ();