Nome da coluna da tabela mysql Num1, num2, num3, num4, num5, num6 tabela nome Excel
Método de upload
pacote com.web.connection; importar java.io.fileInputStream; importar java.io.filenotfoundException; importar java.io.ioexception; importar org.apache.commons.logging.log; importação; importação; orgache.commons.Logging.LogFactory; org.apache.poi.hssf.usermodel.hssfcell; importar org.apache.poi.hssf.usermodel.hssfrow; importar org.apache.poi.hssf.usermodel.hsfsheet; importgache.poi.hssf.hsf.hsfworks; org.apache.poi.xssf.usermodel.xssfcell; importar org.apache.poi.xssf.usermodel.xssfrow; importar org.apache.poi.xssf.usermodel.xssfsheet; importação..Poi.XSSF.XSSF.ElTex.1SSSF.ElTex.1SSSF.ElTex.1SSSF.ElTex.1SSF.ExTeat.ExTeat.ExTeat.ExTex.1SSSF.ElTex.1SSF.ElTex.1SSF.ElTex.1SSF.St.ExTerod; {// Registre as informações de saída do log estático de log = logFactory.getLog (testexcel.class); //, pegue o caminho para o documento do Excel //. Xlsx File com xssfworkbook .xlx use hssfworkbook public static string filepath = "d: //demoExcel.xlsx"; Arquivo de livro de trabalho do Excel XSSFWorkbook WookBook = novo XSSFWorkbook (new FileInputStream (FilePath)); // No documento do Excel, o índice padrão da primeira planilha é 0 // sua instrução é: shotSfSheet Sheet = Workbook.getSheet (0); xssfheet sheet = shokbook.sheet (shetsheet = work.getsheet (0); xssfheet sheet = shook.heet.Sheet = Sheet = Work.getSheets; Sheet.getphysicalNumberOfRows (); // transweep a linha para (int i = 0; i <linhas; i ++) {// Leia a célula esquerda superior xssfrow line = Sheet.getRow (i); // a linha não está vazia se (linha! ""; // coluna de tranquilidade para (int j = 0; j <células; j ++) {// obtenha o valor da coluna XSSFCell Cell = row.getCell (j); if (célula! = Null) {switch (Cell.getCeltype ()) {case hsSfCell.CellEllPe_formE_ULUM: += Cell.getNumericCellValue () +","; Break; case hssfcell.cell_type_string: value += Cell.getStringCellValue () +","; Break; inadnat): Value += "0"; Break;}}} // insert em mySql Datae String [] Testentity (); entity.setNum1 (val [0]); entity.setNum2 (val [1]); entity.setnum3 (val [2]); entity.setNum4 (val [3]); entity.setnum5 (val [4]); entidade.setNum6 (Val [5]); a = method.add (entidade); if (a> 0) {System.out.println ("inserir bem -sucedido");} else {System.out.println ("insert falhou");}}}}} Catch (iOxcept (ioxception e) {E.PrintStackTrace ();Onde a testação é uma classe de entidade que armazena os dados consultados na tabela Excel.
pacote com.web.connection; public class testntity {private string num1; private string num2; private string num3; private string num4; private string num5; private string num6; public testntity () {} public string getNum1 () {return num1;} public void Setnum1 (string num1) (this.num1; setnum2 (string num2) {this.num2 = num2;} public string getNum3 () {return num3;} public void setNum3 (string num3) {this.num3 = num3;} public string getNum4 () {return num4;} public void Setnum4 (string; num5;} public void setNum5 (string num5) {this.num5 = num5;} public string getNum6 () {return num6;} public void setNum6 (string num6) {this.num6 = num6;}}TestMethod é uma declaração SQL que insere dados na tabela MySQL
pacote com.web.connection; importar java.sql.connection; importar java.sql.preparedStatement; importar java.sql.sqLexception; public class TestMethod {public int (testntity te) {conexão con-1nection.getConnection (); Excel (num1, num2, num3, num4, num5, num6) valores (?, ,? ,? ,?) "; tente {pstmt = con.preparestatement (sql); pstmt.SetString (1, te.getnum1 ()); te.getnum3 ()); pstmt.SetString (4, te.getnum4 ()); pstmt.SetString (5, te.getnum5 ()); pstmt.setString (6, te.getnum6 ()); count = count.exT. (); }*/} Catch (sqLexception e) {// TODO Blocke.printStackTrace ();} finalmente {try {pstmt.close ();} catch (sqLexception e) {// TODO-GENERATEADO BLOCKE.PRONTSTACTTRACE ();Resumir
O exposto acima é o conteúdo inteiro deste artigo sobre o upload do conteúdo do Excel para o código da instância do MySQL por Java. Espero que seja útil para todos. Amigos interessados podem continuar se referindo a outros tópicos relacionados neste site. Se houver alguma falha, deixe uma mensagem para apontá -la. Obrigado amigos pelo seu apoio para este site!