在我的项目中,我上传了excel工作表,我使用java检索,收到excel文件后,我只想创建一个数据库表。这些excel工作表包含标记列表(每个excel工作表包含单个主题标记列表),如学生姓名、内部、外部、结果。如果它是一个excel工作表,那么我可以很容易地在数据库中创建一个表。但是我必须从4个工作表创建一个表。
我只是尝试使用for循环列出每个excel文件并逐个获取数据,但这不是我的解决方案。
STUDENT NAME CIA ESE TOTAL
AJAY G 46 31 77
AJITH V 41 27 68
AJITH KUMAR 40 26 66
我的Java代码在这里:
String dirs[] = file.list();
for(String i:dirs) {
// here i can get the file one by one
fis = new FileInputStream(fullpath+"/"+i);
wb = WorkbookFactory.create(fis);
System.out.println(wb.getNumberOfSheets());
sh = wb.getSheet("Sheet1");
int noOfRows = sh.getLastRowNum();
int noOfCols = sh.getRow(7).getLastCellNum();
System.out.println("Rows : "+ noOfRows);
System.out.println("Cols : "+ noOfCols);
}
以上是我的excel工作表数据和所有工作表一样,不同的主题(主题名称是文件名,所以我可以把它)。
我希望在db中创建一个表,如下所示:
Table name : StudentResult
Stuname | Subject1 | Sub1CIA | Sub1ESE | Subject2 | Sub2CIA | Sub2ESE ...
依此类推(像这样获取剩余的excel工作表列。)
假设每个工作表都包含列标题,从所有工作表中收集列标题并制作一个唯一的集合。然后您可以使用此集合创建使用JDBC动态执行DDL的数据库表。
如果您想在for循环之后创建表,请在for-循环上方声明一个Set变量,并继续将每个Sheet中的列标题添加到其中。一旦控件退出for循环,您将拥有带有列标题的集合。这是它的伪代码
// Declare a set here
Set<String> dbColumns = new HashSet<String>();
String dirs[] = file.list();
for(String i:dirs) {
// For each sheet
// Get the heading row ( may be col 1 depending on the work sheet)
// Get the cells from this row
// Add the values to the dbColumns set
}
// Now dbColumns contains the list for columns to be created
// Iterate through this set and form the DDL Statement
// Execute the DDL using jdbc