Recentemente, o professor designou uma tarefa para usar o Java para simplesmente adicionar, excluir, modificar e verificar arquivos com o Sufixo do Excel XLSX. Felizmente, foi concluído e fez um resumo simples.
首先导入了一个poi.jar 网上有很多这个资源可以下载
XSSFSheet sheet=null;
XSSFWorkbook book=null;
1: Verifique (encontre a tabela do Excel no local especificado local, emitir -o no console)
public void print_excel () {// Obtenha o número de linhas da tabela do Excel int lastrownumber = Sheet.getLastrOnum (); XSSFROW ROW = Sheet .getRow (a); b);2: Altere (modifique o conteúdo de uma célula na tabela do Excel)
public void set_excelCell (int i, int j, string str) {// Obtenha informações da linha xssfrow ROW = Sheet.getRow (i-1); Obtenha o conteúdo da String de células modificadas = Excel_operation.GetexcelCellValue (Cell); +str);}Três: Adicionar (insira uma linha de índice na tabela Excel na posição especificada)
public void insert (int rowindex, string [] objs) {if (rowIndex == 0) {lança new ilegalargumentException ("não pode ser inserido na linha 0, a linha 0 é usada para definir!"); getLastrOnum () + 1) {TOME NOVA ILEGRANTEGEMENTE ("Ele só pode ser inserido após a última linha."); if (sheet.getphysicalNumberOfRows () <= 1) {referenceRowIndex = rowIndex - 1; // Não faça qualquer processamento} else {// mova uma folha.Shiftrws (RowIndex, Sheet.getLastrOnum (), 1, verdadeiro, false); .getRow (referência); TargetCell.SetCellStyle (referente.getCellStyle ());Quatro: Exclua (exclua o conteúdo da linha especificada)
// Exclua uma linha de dados (na tabela Excel, as linhas são calculadas a partir de 0) public void Delete (int rowIndex) {// A última linha que é excluída é se (rowindex == Sheetlastrownum ()) {Sheet.removowow (Sheet. GetRow (Sheet.getLastrwn ())); .Remov Erow (Sheet.getRow (Sheet.getLastrOnum () + 1));5: Determine o tipo de retorno (porque o conteúdo da tabela do Excel é diferente, incluindo tipos de caracteres, tipos inteiros etc., é necessário julgar seu tipo antes de produzir)
String estática privada getExcellValue (XSSFCell Cell) {String ret = ""; Cell.getCellType () == xssfcell.cell_type_string) {ret = Cell.getStringCellValue (); "" Cell.getNumericCellValue (); Tipo do valor de retorno é um tipo de exceção} else if (Cell.getCelltype () == xssfcell.cell_type_error) {ret = "" + Cell.getErrorCellValue (); caso contrário, if (célula: :::::::::::::::::::::::heone :::::::::::::::::::heo :::::::::::::::: :::::::::::::::::: .getCelltype () == xssfcell.cell_type_boolean) {ret = "" + celular.getBooleaCellValue (); caso contrário, if (Cell.getCellType () == XSSFCEL L.CELL_TYPE_BLANK) {RET = "";