根据数据库字段自动增加excel文件的单元格大小

时间:2014-05-02 11:24:42

标签: java excel servlets

我正在将数据库字段导入到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("field1");
                rowhead.createCell(2).setCellValue("field2");
                rowhead.createCell(3).setCellValue("description");
                rowhead.createCell(4).setCellValue("activity");
                rowhead.createCell(5).setCellValue("eid");
                rowhead.createCell(6).setCellValue("logintime");
                rowhead.createCell(7).setCellValue("logouttime");
                Connection con = ConnectionManager.getConnection();
                Statement st = con.createStatement();
                ResultSet rs = st
                        .executeQuery("select u.date, u.field1, u.field2, " +
                                "u.description,  u.activity, u.e_id, d.logintimee, " +
                                "d.logouttime, d.e_id from updatework AS u, " +
                                "employee1 as d where d.e_id = u.e_id AND u.e_id='"
                                + 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("field1"));
                    row.createCell(2).setCellValue(rs.getString("field2"));
                    row.createCell(3).setCellValue(rs.getString("description"));
                    row.createCell(4).setCellValue(rs.getString("activity"));
                    row.createCell(5).setCellValue(rs.getString("eid"));
                    row.createCell(6).setCellValue(rs.getString("logintimee"));
                    row.createCell(7).setCellValue(rs.getString("logouttime"));
                    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);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

先生,我使用这种方法得到了解决方案         sheetName.autoSizeColumn(cellnum);
在单元格中接收数据后,这是正确的方法..

   row.createCell(1).setCellValue(rs.getString("field1"));
      sheet.autoSizeColumn(1);

这里1是列索引,我们想要根据它将扩展的数据增加大小..