如何将excel数据上传到mysql表中

我正在开发Web应用程序与数据库中的应用程序用户可以将.csvfile upload到数据库它工作一段时间它有数据上传的问题。 所以我需要知道如何将excelfile upload到Mysql表格下面的代码

conv = new Connectivity(); con = conv.setConnection(); st = con.createStatement(); query = "LOAD DATA LOCAL INFILE \"" + filename2 + "\" INTO TABLE " + tablename + " FIELDS TERMINATED BY ',' IGNORE 1 LINES"; st.executeUpdate(query); PrintWriter obj1 = response.getWriter(); obj1.println("Row (1) inserted"); HttpSession sval = request.getSession(); sval.setAttribute("UpdatedCorret", "yes"); 

这是使用Poi APIparsingexcel文件的演示代码…

 File source = jFileChooser.getSelectedFile(); InputStream input = new BufferedInputStream(new FileInputStream( source.getCanonicalPath())); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Iterator<?> rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); Iterator<?> cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); try { System.out.print(new BigDecimal(cell .getNumericCellValue()).toPlainString()); } catch (IllegalStateException ex) { System.out.print(cell.getStringCellValue()); } catch (Exception ex) { ex.printStackTrace(); } } } 

希望对你有帮助…

试试Excel2MySQL 。 它有一个简单的手动导入用户界面,它还包括一个用于自动上传的命令行界面 ,可以直接读取任何Excel文件。 不需要导出为csv文件。 Excel sheetname确定目标MySQL表名。 该表可以自动创build或简单地附加到。 所有字段也可以自动优化到适当的MySQLtypes。 该程序是免费试用,我是它的作者。

 -h, --help, display help message and exit -f file, Excel file to convert (include full path) (REQUIRED) -s sheet, Excel sheet to convert (omit to convert all) -z host, mysql server hostname or IP address (omit will default to localhost) -d database, mysql database name (REQUIRED) -u user, mysql user name (REQUIRED) -p password, mysql password -x port, mysql port number (omit will default to 3306) -r, replace existing table (omit to append to existing table) -n, first row is not a header (omit to indicate first row contains header) -a, allow spaces in table & field names (omit to change spaces to underscores) -m, use myisam database storage engine (omit to use innodb) -k, keep blank rows & columns (omit to remove) -c, Unmerge merged cells -v, define all fields as varchar type on tables (omit to optimize field types) 

这里是一个示例命令行上传Excel表格名称为“地址”的Excel文件:

 excel2mysqlCLI.exe -f "c:\my excel.xls" -d mydb -u myid -p mypass -s address