-1

i am importing database field into an excel sheet its getting created but problem is some field has more data so i want to increase the size of that particular column width should get increase... here is my code



    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);
            }
        }
    }

3

1 Answer 1

0

thnks sir i got the solution by using this method sheetName.autoSizeColumn(cellnum);
after receiving data in cell its working,so here is proper way..

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

here 1 is column index which we want to increase the size so according to data it will expand..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.