Thursday, June 27, 2019

Re: How to generate and download Excel Files with GWT

Hi, Can you please let me know which module did add gwt.xml file to work with WritableWorkbook. I have a situation to work with Excel in GWT but not able to find any module

On Tuesday, 4 May 2010 19:22:28 UTC+5:30, Dima wrote:
Hi,

I try to create a test.xsl on the server without saving the file on
the server. My application is running fine in hosted mode, but once
compiled and uploud(Tomcat) I get a nasty exception. Probably my
servleturl is not correct.

My code:

Client:----------------------------
    final String link = GWT.getModuleBaseURL() + "myfiledownload";

                          RequestBuilder builder = new
RequestBuilder(RequestBuilder.GET,link);

                          try {
                               builder.sendRequest(null, new RequestCallback() {
                            public void onError(Request request, Throwable t) {
                              Window.alert("Error bei getExcel");
                            }
                          public void onResponseReceived(Request request,Response response)

                                  if(response.getStatusCode() == 200) {
                                          Window.Location.replace(link);
                            } else if(response.getStatusCode() == 404) {
                              Window.alert("Service not available.");
                            }
                          }
                         });
                         } catch (RequestException re) {
                           GWT.log("Error", re);
                         }
-------------------------------------------------------------------
web.xml

<servlet>
        <servlet-name>myfiledownload</servlet-name>
        <servlet-class>module.server.MyFileServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>myfiledownload</servlet-name>
        <url-pattern>/myfiledownload</url-pattern>
   </servlet-mapping>
--------------------------------------------
MyApp.gwt.xml

<module>
...
<servlet path="/myfiledownload" class="module.server.MyFileServlet"/>
</module>
---------------------------------------
Server

package module.server;


public class MyFileServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;

public void  doGet(HttpServletRequest request, HttpServletResponse
response) throws  IOException {
            try {
              // Get Excel Data
              ByteArrayOutputStream bytes = generateExcelReport();

              // Initialize Http Response Headers
              response.setHeader("Content-disposition", "attachment;
filename=exportUsers.xls");
              response.setContentType("application/vnd.ms-excel");

              // Write data on response output stream
              if (bytes != null) {
                response.getOutputStream().write(bytes.toByteArray());
              }
            } catch (Exception e) {
              response.setContentType("text/plain");
              response.getWriter().print("An error as occured");
            }
          }

public ByteArrayOutputStream generateExcelReport() throws IOException,
WriteException{


           /* Stream containing excel data */
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                /* Create Excel WorkBook and Sheet */
                WritableWorkbook workBook = Workbook.createWorkbook(outputStream);
                WritableSheet sheet = workBook.createSheet("User List", 0);

                /* Generates Headers Cells */
                WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12,
WritableFont.BOLD);
                WritableCellFormat headerCellFormat = new
WritableCellFormat(headerFont);
                headerCellFormat.setBackground(Colour.PALE_BLUE);
                sheet.addCell(new Label(1, 1, "LastName", headerCellFormat));
                sheet.addCell(new Label(2, 1, "FirstName", headerCellFormat));

                /* Generates Data Cells */
                WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
                WritableCellFormat dataCellFormat = new
WritableCellFormat(dataFont);
                int currentRow = 2;


                /* Write & Close Excel WorkBook */
                workBook.write();
                workBook.close();

                return outputStream;

       }
}
---------------------------------------

Please help my...

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/6449bd84-6002-4d7c-ac41-d40333dd6861%40googlegroups.com.

No comments:

Post a Comment