我有一个奇怪的问题,想知道是否有其他人遇到过这个问题。我有一个 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() );
}
}