Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:47:17+00:00 2026-06-12T00:47:17+00:00

i am having a resultset and i have to write all the data available

  • 0

i am having a resultset and i have to write all the data available in resultset to a text file and populate the same to user for downloading.

i have done the below code to export to excel using poi, same way how to do for text file.

if(exportTo.equals("excel"))
        {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".xls\"");
            try {
                HSSFWorkbook hwb = new HSSFWorkbook();
                HSSFSheet sheet = hwb.createSheet(reportName); 
                HSSFRow row = null;

                HSSFHeader header = sheet.getHeader();
                header.setCenter("POC");
                header.setLeft("POC");
                header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
                                HSSFHeader.fontSize((short) 16) + reportName);

                //to add water mark
                /*HSSFPatriarch dp = sheet.createDrawingPatriarch();
                HSSFClientAnchor anchor = new HSSFClientAnchor
                    (0, 0, 1023, 255, (short) 2, 4, (short) 13, 26);
                HSSFTextbox txtbox = dp.createTextbox(anchor);
                HSSFRichTextString rtxt = new HSSFRichTextString("POC");
                HSSFFont draftFont = hwb.createFont();
                draftFont.setColor((short) 27);
                draftFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
                draftFont.setFontHeightInPoints((short) 192);
                draftFont.setFontName("Verdana");
                rtxt.applyFont(draftFont);
                txtbox.setString(rtxt);
                txtbox.setLineStyle(HSSFShape.LINESTYLE_NONE);
                txtbox.setNoFill(true);*/

                HSSFCellStyle style = hwb.createCellStyle();
                style.setBorderTop((short) 6); // double lines border
                style.setBorderBottom((short) 1); // single line border
                style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

                HSSFFont font = hwb.createFont();
                font.setBoldweight((short) 700);

                // Create Styles for sheet.
                HSSFCellStyle headerStyle = hwb.createCellStyle();
                headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
                headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                headerStyle.setFont(font);
                headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
                headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
                headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                headerStyle.setAlignment((short) 2);

                // create Title for the sheet
                HSSFCellStyle titleStyle = hwb.createCellStyle();

                HSSFFont titleFont = hwb.createFont();
                titleFont.setFontName(HSSFFont.FONT_ARIAL);
                titleFont.setFontHeightInPoints((short) 15);
                titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                titleFont.setColor(HSSFColor.BLUE.index);
                titleStyle.setFont(titleFont);
                titleStyle.setAlignment((short)2);

                row = sheet.createRow((short)1);
                HSSFCell secondCell = row.createCell((short) 0);
                secondCell.setCellValue(new HSSFRichTextString(reportName).toString());
                secondCell.setCellStyle(titleStyle);    
                sheet.addMergedRegion(new Region(1, (short)0, 1, (short)headerCount));

                int sno=0;
                HSSFRow rowhead = sheet.createRow((short)4);
                for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                    String headerName = (String) headerMap.get(it.next());
                    HSSFCell headerCell = rowhead.createCell((short)sno);
                    headerCell.setCellStyle(headerStyle);
                    headerCell.setCellValue(headerName);
                    sno++;
                }

                HSSFCellStyle rowStyle=hwb.createCellStyle();
                rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
                rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
                rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
                rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
                rowStyle.setAlignment((short) 2);

                row = custDAO.creadExcelTable(query, sheet, row,rowStyle);
                hwb.write(response.getOutputStream());
                response.flushBuffer();

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
public HSSFRow creadExcelTable(String query,HSSFSheet sheet,HSSFRow row,HSSFCellStyle rowStyle ){
        int numberOfColumns=0,sno=0,index=5,iterator=1;
        Connection connection = getConnection();
        if (connection != null) {
            try {
                PreparedStatement reportTablePS = connection.prepareStatement(query);
                ResultSet reportTable_rst = reportTablePS.executeQuery();
                ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData();
                numberOfColumns = reportTable_rsmd.getColumnCount();
                int i =0;
                while (reportTable_rst.next()) {
                     row = sheet.createRow((short)index);
                     sheet.setColumnWidth((short)index, (short)100);

                    /* if(i == 0){
                         i = 1;
                         rowStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index);
                         rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                         System.out.println("BLUE_GREY");
                     }
                    else {
                        i = 0;
                        System.out.println("LEMON");
                        rowStyle.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
                        rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    }*/

                     HSSFCell serialCell = row.createCell((short)sno);
                     serialCell.setCellStyle(rowStyle);
                     serialCell.setCellValue(iterator);

                            for (int columnIterator = 1; columnIterator <= numberOfColumns; columnIterator++) {
                                 String column = reportTable_rst.getString(columnIterator);
                                 sheet.setColumnWidth((short)columnIterator, (short)3000);
                                 HSSFCell rowCell = row.createCell((short)columnIterator);
                                 rowCell.setCellStyle(rowStyle);
                                 rowCell.setCellValue(column);
                            }
                            index++;
                            iterator++; 
            }
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                try {
                    closeConnection(connection, null, null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return row;
    }

now i have done updto this and really don’t know how to go about

 if(exportTo.equals("text")){
            response.setContentType("text/plain");
            response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".txt\"");
            try {

            } catch (Exception e) {
                // TODO: handle exception
            }

        }

this one for creating file in a specified location

Writer writer = null;

try {
    String text = "This is a text file";

    File file = new File("write.txt");
    writer = new BufferedWriter(new FileWriter(file));
    writer.write(text);
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (writer != null) {
            writer.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

but i want to export the file with dialog, Please help me how to go about.

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:47:18+00:00Added an answer on June 12, 2026 at 12:47 am

    I suppose you don’t get a dialog box, because your browser can handle text files by itself.
    The browser reads the MIME type of the http response (which has been set with response.setContentType("text/plain");)
    Most browsers open html, images and text themselves and redirect other file types like audio, pdf’s or Office documents to other applications.
    So you may need to adjust your browser settings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an (access) database table which contains data I would like to populate
I have UIPicker that I need to write search mechanism for. The data that
Hey All I currently have a problem with my insert php file I have
I am having trouble displaying results recieved from asp.net WebMethod. I have a HTML
I try to limit resultset in size without having to change each SQL statement
I am having some difficulty returning all the results I would expect when leaving
What I want to achieve is having a ListFragment which displays data loaded from
I have not used Junit before and have not done unit testing automatically. Scenario:
I am performing a scattered read of 8-bit data from a file (De-Interleaving a
I'm having a major problem with writing values into a new CSV file. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.