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

Parasu
  • 192
  • 2
  • 13
sourav78611
  • 99
  • 2
  • 17
  • 1
    Have a look at [Apache POI Excel - how to configure columns to be expanded?](http://stackoverflow.com/questions/4611018/apache-poi-excel-how-to-configure-columns-to-be-expanded) – Braj May 02 '14 at 11:25
  • thank you sir i have checked your given link and got the solution its working.. – sourav78611 May 02 '14 at 12:25
  • Post it as answer to make it helpful for others. – Braj May 02 '14 at 12:29

1 Answers1

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..

sourav78611
  • 99
  • 2
  • 17