我正在开发带有数据库的Web应用程序,因为应用程序用户可以将.csv文件上传到数据库,它在数据上传有问题的情况下工作。所以我需要知道如何将excel文件上传到代码
下面的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");
答案 0 :(得分:1)
这是使用Poi API解析excel文件的演示代码...
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();
}
}
}
希望它适合你...
答案 1 :(得分:0)
试试Excel2MySQL。它具有用于手动导入的简单用户界面,还包括用于自动上载的command line interface,并且可以直接读取任何excel文件。无需导出为csv文件。 Excel工作表名称确定目标MySQL表名称。该表可以自动创建或简单地附加到。所有字段也可以自动优化为正确的MySQL类型。该程序可以免费试用,我是它的作者。
-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)
以下是上传工作表名称为“address”的Excel文件的示例命令行:
excel2mysqlCLI.exe -f "c:\my excel.xls" -d mydb -u myid -p mypass -s address