我正在写一个网站,允许用户上传excel文件,然后将内容转换为MYSQL语句。
代码:
public void convertToSQL(ActionRequest actionRequest,ActionResponse actionResponse) throws Exception
{
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = this.getDbConnection(conn);
conn.setAutoCommit(false);
UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
String uploadedTemplateId = uploadPortletRequest.getParameter("templateId");
System.out.println("uploadedTemplateId:"+uploadedTemplateId);
File uploadedFile = uploadPortletRequest.getFile("uploadedFile", true);
System.out.println("uploadedDocument:" + uploadedFile.getName());
String test2="insert akpi_db.dbo.tpl_estimate_gdh (hosp) values ('testgetfile')";
pstmt = conn.prepareCall(test2);
pstmt.executeUpdate();
Workbook wb1 = new Workbook(uploadedFile.getAbsolutePath());
String test1="insert akpi_db.dbo.tpl_estimate_gdh (hosp) values ('testgetpath')";
pstmt = conn.prepareCall(test1);
pstmt.executeUpdate();
int pageNum = wb1.getWorksheets().getCount();
test1="insert akpi_db.dbo.tpl_estimate_gdh (hosp) values ('getpageNum')";
pstmt = conn.prepareCall(test1);
pstmt.executeUpdate();
for (int i=0;i< pageNum;i++){
Worksheet ws1 = wb1.getWorksheets().get(i);
Cells cells1 = ws1.getCells();
int row = getPrintAreaMaxRow(ws1);
int col = getPrintAreaMaxColumn(ws1);
final String as_at_date_id = "convert(varchar(8), getdate(), 112)";
final String process_dtm = "getdate()";
for (int j=6;j<row;j++){
for (int k=0;k<col;k=k+2){
Cell fin_year_cell = cells1.get(1,1);
Cell hosp_cell = cells1.get(j,0);
String[] parts = fin_year_cell.getStringValue().split("/");
int fin_year = Integer.parseInt(parts[0]);
String hosp = hosp_cell.getStringValue().replaceAll("\\s+", "");
Cell gdh_places_cell = cells1.get(j,k);
Cell gdh_attns_cell = cells1.get(j,k+1);
String gdh_places = gdh_places_cell.getStringValue();
String gdh_attns = gdh_attns_cell.getStringValue();
Cell cal_cell = cells1.get(4,k);
String[] date = cal_cell.getStringValue().split("-"); //date[1] = cal_year
int cal_month = 0,fin_month=0;
if (date[0] == "Jan"){
fin_month=10;
cal_month=1;
}else if (date[0] == "Feb"){
fin_month=11;
cal_month=2;
}else if (date[0] == "Mar"){
fin_month=12;
cal_month=3;
}else if (date[0] == "Apr"){
fin_month=1;
cal_month=4;
}else if (date[0] == "May"){
fin_month=2;
cal_month=5;
}else if (date[0] == "Jun"){
fin_month=3;
cal_month=6;
}else if (date[0] == "Jul"){
fin_month=4;
cal_month=7;
}else if (date[0] == "Aug"){
fin_month=5;
cal_month=8;
}else if (date[0] == "Sep"){
fin_month=6;
cal_month=9;
}else if (date[0] == "Oct"){
fin_month=7;
cal_month=10;
}else if (date[0] == "Nov"){
fin_month=8;
cal_month=11;
}else if (date[0] == "Dec"){
fin_month=9;
cal_month=12;
}
if ( cal_month!=0 && fin_month!=0){
String sql ="insert into akpi_db.dbo.tpl_estimate_gdh (hosp,cal_year,cal_month,fin_year,fin_month,gdh_place,gdh_attn,as_at_date_id,process_dtm)"+
"VALUES(?,?,?,?,?,?,?,?,?)";
String test="insert akpi_db.dbo.tpl_estimate_gdh (hosp) values ('testconvert')";
pstmt = conn.prepareCall(test);
/* pstmt.setString(1, hosp);
pstmt.setString(2, date[1]);
pstmt.setInt(3, cal_month);
pstmt.setInt(4, fin_year);
pstmt.setInt(5, fin_month);
pstmt.setString(6, gdh_places);
pstmt.setString(7, gdh_attns);
pstmt.setString(8, as_at_date_id);
pstmt.setString(9, process_dtm);*/
pstmt.executeUpdate();
}
}
}
}
pstmt.close();
conn.commit();
}catch(Exception e){
e.printStackTrace();
}finally {
if (conn != null) {
try {
conn.setAutoCommit(true);
conn.close();
} catch(SQLException excep) {
System.out.println("Connection cannot be closed.");
}
}
}
}
但是我发现我的代码不起作用,并且在尝试调试之后。错误发生在此行
int pageNum = wb1.getWorksheets().getCount();
问题是,在日志文件中,它没有抛出任何异常。在用于测试的数据库中,它仅插入“ testgetfile”和“ testgetpath” “ getpageNum”和“ testconvert”尚未插入数据库,因此我猜错误发生在上述句子上。
即使我尝试将多页或单页excel上传到网络,但它们都不起作用
但是这句话错了吗?编译器和服务器没有给我任何错误异常。
答案 0 :(得分:0)
请与示例文件共享一个示例演示应用程序(可运行)。我们将重现该问题,并在分析后提供反馈。
注意:我是Aspose的支持开发人员/传播者。