# Export large data from DB to MSExcel using DSP page

**URL:** <https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837>\
**Category:** Forum\
**Tags:** DSP, export-to-excel\
**Created:** [April 26, 2021, 2:04pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837 "2021-04-26T14:04:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![knaresh455](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@knaresh455](https://techcommunity.softwareag.com/u/knaresh455)\
**Post date:** [April 26, 2021, 2:04pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/1 "2021-04-26T14:04:29Z")

</div>

Hi ,  
I have requirement to fetch data from DB and move it to Native Excel file using DSP. Where DSP page will have simple download button and end-user allowed to download file. data size will be about 20 MB. I tried below steps:

1. Read data from DB using Adapter.
2. Push data to IS Cache. ( for faster fetch)
3. call fetch data service in DSP like table.
4. Call JS to move Table data to Excel file.
5. Call another JS to hide table content and show only download button to download excel file.

Code is working fine and able to download file but issue is it taking move than 4 min time to perform all above options.

Can some one suggest more suitable options here to reduce download time.

---

<div class="post-metadata">

**Author:** ![John\_Carter4](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_carter4/32/19818_2.png) [@John\_Carter4](https://techcommunity.softwareag.com/u/John_Carter4)\
**Post date:** [April 26, 2021, 2:26pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/2 "2021-04-26T14:26:46Z")

</div>

Your service seems rather complicated, why not;

1. build service that reads data from DB using adapter.
2. service sets response to CSV formatted text.
3. set response headers so that response gets treated as downloadable file
4. Enable service cache.
5. User clicks on link with service as target.

Use the service ‘pub.flow:setResponseHeaders’ to ensure the browser will propose a file download and set the headers as follows;

 ![Screenshot 2021-04-26 at 16.22.44](https://global.discourse-cdn.com/techcommunity/original/3X/7/e/7eac956054cff544cdc273397475bddab011a4fa.jpeg)

This way you don’t need any custom js code.  
regards,  
John.

---

<div class="post-metadata">

**Author:** ![knaresh455](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@knaresh455](https://techcommunity.softwareag.com/u/knaresh455)\
**Post date:** [April 26, 2021, 3:35pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/3 "2021-04-26T15:35:02Z")

</div>

Thankyou for quick reply John.  
One quick question here.  
“2. service sets response to CSV formatted text.” - Here I need file in native excel format.  
Like proper header and values under them.  
 ![image](https://global.discourse-cdn.com/techcommunity/original/3X/8/2/823e1797e74eab7626f8fad322ce3784520688eb.png) does this works with above steps?

---

<div class="post-metadata">

**Author:** ![John\_Carter4](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_carter4/32/19818_2.png) [@John\_Carter4](https://techcommunity.softwareag.com/u/John_Carter4)\
**Post date:** [April 26, 2021, 4:41pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/4 "2021-04-26T16:41:32Z")

</div>

just make sure that you add a first row giving column names in csv file, then format after importing.  
I wouldn’t impose a specific product on a user in a web scenario, not everyone will want to use excel, therefore if you use a CSV format, a Mac user can import into Numbers, a linux user can use libreOffice etc.

regards  
John.

---

<div class="post-metadata">

**Author:** ![reamon](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@reamon](https://techcommunity.softwareag.com/u/reamon)\
**Post date:** [April 26, 2021, 8:09pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/5 "2021-04-26T20:09:14Z")

</div>

My 2 cents:

- As @John_Carter4 notes, use CSV as the response payload. Returning a native Excel file will involve getting a library of some sort to create that and you’ll be constantly dealing with compatibility and interoperability issues. Keep it simple.

- Most likely the service cache is not needed. More often than not, the service cache gets in the way because you’re constantly needing to tweak the cache expiration and chasing “why did it not get the latest data” issues.

- Take care to NOT LOAD all of the data from the DB into memory. Use streaming techniques to read a bunch, write a bunch.

---

<div class="post-metadata">

**Author:** ![Holger\_von\_Thomsen](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/holger_von_thomsen/32/178_2.png) [@Holger\_von\_Thomsen](https://techcommunity.softwareag.com/u/Holger_von_Thomsen)\
**Post date:** [April 27, 2021, 3:20pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/6 "2021-04-27T15:20:23Z")

</div>

Additional Note:

It might be possible to create the CSV via FlatFile techniques as this can ease the mapping from the database to FlatFile-CSV-structure resp. -document.

Regards,  
Holger

---

<div class="post-metadata">

**Author:** ![knaresh455](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@knaresh455](https://techcommunity.softwareag.com/u/knaresh455)\
**Post date:** [April 29, 2021, 8:43am UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/7 "2021-04-29T08:43:50Z")

</div>

Thankyou for suggestions.  
Could you please give more insights about streaming techniques to read a bunch and write bunch.

---

<div class="post-metadata">

**Author:** ![John\_Carter4](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_carter4/32/19818_2.png) [@John\_Carter4](https://techcommunity.softwareag.com/u/John_Carter4)\
**Post date:** [April 29, 2021, 9:04am UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/8 "2021-04-29T09:04:41Z")

</div>

Use the service ‘pub.flow:setResponse2’, rather than relying on the implicit service output.  
Then map the input stream from your original file/etc to the “responseStream” input.

The hard part is getting your input stream, because if you are reading records from a DB, you will need to convert each record into a json or xml string fragment and then append it to a temporary file. After which you would then read the file as input stream, which you would then map to setResponse2 as above.

Ideally you would want to avoid having to create a file, however I don’t think you can do that in flow. you would have to write a java service to avoid that.  
regards,  
John.

---

<div class="post-metadata">

**Author:** ![reamon](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@reamon](https://techcommunity.softwareag.com/u/reamon)\
**Post date:** [April 29, 2021, 3:24pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/9 "2021-04-29T15:24:04Z")

</div>

Good info from @John_Carter4 for the approach.

> [@John\_Carter4](#):
>
> The hard part is getting your input stream,

Indeed. The use of a temp file is likely the way to go.

Here is an alternative to consider, which would forego the temp file but have its own set of complexities. This thread has an example of the basic tasks to be addressed to have data from the DB be the source of the InputStream more directly.

> <https://stackoverflow.com/questions/11209818/java-sql-result-to-inputstream>

Aspects you’ll need to consider:

- The example returns the raw results of the query. You’ll want to convert each row to the CSV you need as @John_Carter4 noted.
- The example does everything in Java. Okay for a Java environment but less so for wM Integration Server. You’ll want to determine if doing this all in Java is okay for your environment. Or figure out a way, if any, to leverage JDBC adapter service to get the result set. Or perhaps a ref cursor.

---

<div class="post-metadata">

**Author:** ![John\_Carter4](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/john_carter4/32/19818_2.png) [@John\_Carter4](https://techcommunity.softwareag.com/u/John_Carter4)\
**Post date:** [April 30, 2021, 7:06am UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/10 "2021-04-30T07:06:48Z")

</div>

A custom OutputStream is the only way I can think of to ensure that you don’t have to do everything in java.

1. Implement a custom class that implements both InputStream and OutputStream.
2. Create three java services, one to create an instance of the above class, another that writes to the object and the last to close the inputStream.
3. You would have to call setResponse with the output of the create service (2) as the ResponseStream input before calling your JDBC logic, this might sound counterintuitive, but trust me.
4. Loop overt JDBC calls and use write service (2) to write the fragment to the output stream.

Your custom class via the java service would then pipe the output to the input stream assigned to setResponse.

After the loop has finished, make sure you call the close service, otherwise the caller of your service will not finish and eventually trigger a timeout error.

I think I wrote something similar a long time ago, but I don’t if I still have the code. I would have to go on a rummage.  
John.

---

<div class="post-metadata">

**Author:** ![reamon](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@reamon](https://techcommunity.softwareag.com/u/reamon)\
**Post date:** [April 30, 2021, 2:59pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/11 "2021-04-30T14:59:36Z")

</div>

> [@John\_Carter4](#):
>
> Implement a custom class that implements both InputStream and OutputStream.

Having done this before (tie output and input stream related together, but not with JDBC-obtained data), I can tell you this can be a challenge. The key is that the reading from the InputStream drives the writing to the OutputStream. For anyone not confident/strong in Java, proceed with caution.

Edit: If @John_Carter4 finds the code in a trunk in the attic, it would be cool to see it!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [July 29, 2021, 3:00pm UTC](https://techcommunity.softwareag.com/t/export-large-data-from-db-to-msexcel-using-dsp-page/240837/12 "2021-07-29T15:00:29Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
