Showing posts with label excel. Show all posts
Showing posts with label excel. Show all posts

Friday, 9 July 2010

Wicket - Displaying a PDF onclick of a link or onSubmit of a Button

Very simple scenario , I wanted to display a PDF or an Excel on click of a link/ on submit of a button.

Since wicket offers flavors of ajaxbutton and ajax link one can easily make a mistake of the using them to render the pdf or excel.

I did the same. everything seemed to work unit the point where the response needed to be rendered and i would see a pdf or excel.

The problem was that using an ajax button or link we can only render a ajax response, but in this case i wanted to render a pdf or excel response (bytes).

It only clicked to me after a while of debugging that the browser had no way to identify how to render the ajax response which contained pdf bytes.


Lesson learnt , always use a Normal Button or a link to render the pdf as the web response.

an example would be like this:
Link excelLink = new Link("linForExcel"){
@Override
public void onClick() {
final OutputStream excelOutputStream = methodToReturnTheExcelOutputStream();
final ByteArrayResource byteArrayResource = new ByteArrayResource("application/vnd.ms-excel", ((ByteArrayOutputStream) excelOutputStream).toByteArray());
IResourceStream resourceStream = byteArrayResource.getResourceStream();
getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(resourceStream) {
public String getFileName() {
return "NameOfExcel.csv";
}
});
}

Monday, 18 May 2009

SQL Server PIVOT - Magic isn't it

Ever wonders how you could present data via a select statement horizontally instead of vertically. When your columns come from the data you are retrieving from the select statement. The answer is simple in TSQL called the pivot keyword. All it would do is to use the data as the pivot to form the columns. Consider the simple schema of the employee: Product
  • Name
  • ID
Product Sales
  • ID
  • Agent ID
  • Quantity
Agent
  • Name
  • ID
And lets say you want a result table which shows: Agent product year select a.name as 'Agent', p.name as "" from agent a inner join product_Sales pd on ps.agent_id=a.id inner join product p on p.id=ps.product_id PIVOT (SUM(Quantity) for Agent IN (Agent1, Agent2, Agent3)) AS pvt