How to generate and download Excel Files with GWT

1,316 views
Skip to first unread message

Dima

unread,
May 4, 2010, 9:52:28 AM5/4/10
to Google Web Toolkit
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-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

zmo

unread,
Mar 6, 2012, 6:08:34 AM3/6/12
to google-we...@googlegroups.com
Try to use GWT.getHostPageBaseURL() + "myfiledownload";


Mallikarjuna Sarvala

unread,
Jun 28, 2019, 4:52:12 AM6/28/19
to GWT Users
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
To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages