0
我想用不同的表格在數據庫中創建不同的表格,通過使用一些連接我的代碼正在工作但記錄在Excel表格中打印兩次我不知道我已經檢查過所有東西請人幫助我在這裏... 這裏是我的代碼..從數據庫中生成excel表格
public class ExcelFileGen extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
HttpSession session = request.getSession(true);
String eid = (String) session.getAttribute("eid");
try {
String filename = "D:\\DailyWork.xls";
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFRow rowhead = sheet.createRow((short) 0);
rowhead.createCell(0).setCellValue("date");
rowhead.createCell(1).setCellValue("description");
rowhead.createCell(2).setCellValue("Support HRS");
rowhead.createCell(3).setCellValue("Development HRS");
rowhead.createCell(4).setCellValue("training HRS");
rowhead.createCell(5).setCellValue("Research HRS");
rowhead.createCell(6).setCellValue("Meeting HRS");
rowhead.createCell(7).setCellValue("leave hrs");
rowhead.createCell(8).setCellValue("Project Name");
rowhead.createCell(9).setCellValue("activity");
rowhead.createCell(10).setCellValue("eid");
rowhead.createCell(11).setCellValue("intime");
rowhead.createCell(12).setCellValue("outtime");
Connection con = ConnectionManager.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select u.date, u.description, u.field1, u.field2, u.field3, u.field4, u.field5, u.field6, u.field7, u.activity, u.eid, d.intime, d.outtime, d.eid from updatework AS u, fulltime as d where d.eid = u.eid AND u.eid='"
+ eid + "'");
int i = 1;
while (rs.next())
{
HSSFRow row = sheet.createRow((short) i);
row.createCell(0).setCellValue(rs.getString("date"));
row.createCell(1).setCellValue(rs.getString("description"));
sheet.autoSizeColumn(1);
row.createCell(2).setCellValue(rs.getString("field1"));
sheet.autoSizeColumn(2);
row.createCell(3).setCellValue(rs.getString("field2"));
sheet.autoSizeColumn(3);
row.createCell(4).setCellValue(rs.getString("field3"));
sheet.autoSizeColumn(4);
row.createCell(5).setCellValue(rs.getString("field4"));
sheet.autoSizeColumn(5);
row.createCell(6).setCellValue(rs.getString("field5"));
sheet.autoSizeColumn(6);
row.createCell(7).setCellValue(rs.getString("field6"));
sheet.autoSizeColumn(7);
row.createCell(8).setCellValue(rs.getString("field7"));
sheet.autoSizeColumn(8);
row.createCell(9).setCellValue(rs.getString("activity"));
sheet.autoSizeColumn(9);
row.createCell(10).setCellValue(rs.getString("eid"));
sheet.autoSizeColumn(10);
row.createCell(11).setCellValue(rs.getString("intime"));
sheet.autoSizeColumn(11);
row.createCell(12).setCellValue(rs.getString("outtime"));
sheet.autoSizeColumn(12);
i++;
}
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
} catch (Exception ex) {
System.out.println(ex);
}
}
}
您可以使用其他可直接將數據集轉換爲excel和其他格式的apis。 –
我懷疑select語句返回兩行,你的代碼看起來好像是聲音 –
不知道你的表不能給出解決方案,但是看起來Kenneth是正確的。請嘗試對數據庫運行查詢,並確保連接返回所需的結果。順便說一下,考慮使用PreparedStatement並且不要將參數連接到查詢中(它使您的代碼容易受到SQL注入攻擊),還可以考慮將您的數據庫訪問代碼與表示層(編寫電子表格的地方)分開,只是爲了清晰度和可重用性 –