我有一个奇怪的问题,我想知道是否有其他人遇到过这个问题。 我有一个Java servlet正在进行两个mySQL查询,然后使用查询返回的值创建一个Apache POI工作表。第一个查询是总计,第二个查询是总计的一部分。我正在计算一个百分比,它只是部分/总数* 100.
出于测试目的,我正在返回一个xml结构来证明这些值是我期望它们的值。 xml结构具有正确的值。在我输入它们进行测试后,我还要从POI工作表中检索单元格值,并确保存储在工作表中的值是正确的。他们也是。
问题是,一旦我将工作表写入Excel文件,第一行中表示总计的单元格显示的值与表示该部分的第二行中的单元格相同。表示百分比的第三行是1.第三个事实是最奇怪的,因为它意味着错误发生在百分比计算过程中,而不是在写入输出流时。就好像两个百分比计算同时进行,一个基于正确分割部分总数(显示在xml中),另一个基于部分自身分割产生1(显示在输出Excel表格中)。有人有类似的问题吗?
我在servlet中包含了doPost代码:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String outtype = "";
String query = "";
String querytot = "";
boolean bError = false;
HttpSession session = req.getSession(true);
StringBuffer sbResponse = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n");
ServletContext sc = getServletContext();
String sErrorMessage = "";
String sErrorCode = "";
StringBuffer dbStringBuf = new StringBuffer("");
outtype = getStringParameter(req, "outtype", "0");
querytot = getStringParameter(req, "querytot", "0");
query = getStringParameter(req, "query", "0");
try {
Connection con = datasource.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(querytot); //querying for the total value
rs.next();
Object obj = rs.getObject(1);
dbStringBuf.append(" <whole>\n"); //starting build of xml structure for testing here
Integer currRowintTot = 0;
String currRowTot = obj.toString();
currRowintTot = Integer.parseInt(currRowTot);
int tothold = currRowintTot;
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("mainsheet");
Row row = sheet1.createRow(1);
Cell labelcell = row.createCell(0);
Cell valuecell = row.createCell(1);
labelcell.setCellValue("Survey Val Whole");
valuecell.setCellValue(currRowTot);
dbStringBuf.append(" <result>"+currRowTotConv+"</result>\n");
dbStringBuf.append(" </whole>\n");
stmt.close();
stmt2 = con.createStatement();
ResultSet rspart = stmt2.executeQuery(query); //querying for the part value
rspart.next();
Object objpart = rspart.getObject(1);
String currRowPart = objpart.toString();
dbStringBuf.append(" <part>\n");
currRowintPart = Integer.parseInt(currRowPart);
int parthold = currRowintPart;
dbStringBuf.append(" <result>"+currRowPart+"</result>\n");
Row row2 = sheet1.createRow(2);
Cell labelcellw = row2.createCell(0);
Cell valuecellw = row2.createCell(1);
labelcellw.setCellValue("Survey Value Part");
valuecellw.setCellValue(currRowPart);
dbStringBuf.append(" </part>\n");
dbStringBuf.append(" <pct>\n");
float partfl = (float)parthold;
float wholefl = (float)tothold;
Integer pctval = Math.round( (partfl/wholefl)*100 );
Row row3 = sheet1.createRow(3);
Cell labelcellp = row3.createCell(0);
Cell valuecellp = row3.createCell(1);
labelcellp.setCellValue("Survey Value Pct");
valuecellp.setCellValue(pctval);
dbStringBuf.append(" <result>"+pctval+"</result>\n");
dbStringBuf.append(" </pct>\n");
stmt2.close();
con.close();
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); //checking to make sure value entered in cell is what I'm expecting
CellReference cellReference = new CellReference("B2");
Row rowtest = sheet1.getRow(cellReference.getRow());
Cell celltest = rowtest.getCell(cellReference.getCol());
if (celltest!=null) {
dbStringBuf.append(" <test>\n");
dbStringBuf.append(" <result>celltest not null/result>\n");
dbStringBuf.append(" </test>\n");
dbStringBuf.append(" <test>\n");
dbStringBuf.append(" <result>"+celltest.getStringCellValue()+"</result>\n");
dbStringBuf.append(" </test>\n");
}
else{
dbStringBuf.append(" <test>\n");
dbStringBuf.append(" <result>sorry, null</result>\n");
dbStringBuf.append(" </test>\n");
}
if(outtype.equals("excel")){ //flag from client page determines that we should export Excel file
res.setContentType("application/ms-excel");
res.setHeader("Expires:", "0"); // eliminates browser caching
res.setHeader("Content-Disposition", "attachment; filename=testxls.xls");
OutputStream outStream = res.getOutputStream();
wb.write(outStream);
outStream.close();
}
} catch(SQLException e) {
bError = true;
sErrorMessage = "SQLException " + e.getMessage()+" data:"+datatracker+" context:"+contexttracker;
sErrorCode = "102";
} finally {
if (bError) {
sbResponse.append(" ");appendTag("status","ERROR",sbResponse);sbResponse.append("\n");
sbResponse.append(" ");appendTag("errorCode",sErrorCode,sbResponse);sbResponse.append("\n");
sbResponse.append(" ");appendTag("message",sErrorMessage,sbResponse);sbResponse.append("\n");
} else {
sbResponse.append(" ");
appendTag("status","OK",sbResponse);
sbResponse.append("\n");
sbResponse.append( dbStringBuf );
}
}
if(outtype.equals("xml")){ //flag from client page determines that we should return xml
res.setContentType("text/plain"); // CHANGE BACK TO text/xml
out= res.getWriter();
sbResponse.append("</response>");
out.println( sbResponse.toString() );
}
}