Easy method of exporting an html table to excel file in ASP.NET? - html

Have a report generated from the DB, want to add an export button so they can get the same report in a excel readable sheet of some type. The key here is ease of implementation so a CSV is fine over XLS if it's easier.

Excel is actually somewhat good at reading HTML. Espcially if your HTML contains just a single table. If you want to tell the browser to open the file up in excel, you can use
Response.ContentType = "application/vnd.ms-excel"
This tells the browser to open the document in Excel, instead of just rendering it by itself. There are a few problems though. I don't think it will work if somebody wants to open it with OO.org calc instead. Also, when trying to save it, it will never convert the file to a real excel file, unless the user explicity changes the file type. If it's just an intranet app for your organization, this may not be a problem. The plus side is, is that you can use colours, borders, and even formulas, which can't be done when using straight CSV.

You can use HtmlTextWriter:
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
grid.RenderControl(htmlWriter);
where grid is a DataGrid object. You can use other types of controls as well.

Related

R Writing Excel Document

My question is whether or not anybody knows of a better way to do what I'm already doing. I'm creating a report as a list, and trying to render it both in HTML and Excel.
I'm developing a shiny app that generates reports for Qualtrics surveys.
The results table is a list of HTML strings that I paste together and display in a shinydashboard. Here's a dput of the example results tables.
Here's how I'm creating the html results tables list -- the html_tabelize() function in my package. Here's a dput of the example input.
In the shiny server.R file the way I create the Excel file is with the following code:
output$downloadResults <- downloadHandler(
filename = 'tables.xls',
content = function(file) {
write(html_tabelize(main()[['blocks']]), file)
}
)
To summarize: I get the blocks, I run html_tabelize on them, and then I write the HTML output to a file called "tables.xls". When I open that file, because Excel can render HTML, it renders something like this:
My concern and problem with what I'm doing are two-fold:
If I were writing an Excel document instead of simply rendering HTML in Excel, then I could perhaps get a better formatted document. I'd like that.
When you download the results tables xls file and try to open it, you get a warning from Excel. I don't want the users of my app to see this warning, because it's distracting and could worry them about something that isn't really a concern.
I know that options exist for writing Excel files in R, but so far what I've seen indicates that their input must be either a data frame, or a list of data frames. The list I am rendering from has different types of components, like the question text, as well as data frames of results. Originally I was using pandoc, but pandoc, even when run from R, is a system binary, and it's difficult to list as a dependency (and if I can't list it as a dependency, it's tough to make sure it's installed for the users of my app). Additionally, I found out pandoc doesn't even convert to "real" Excel -- it also just saves HTML in a .xls file. Does anybody have any suggestions as to how I can improve this part of my app?

Merge multiple access reports to one pdf file using vba

I wan't to merge multiple access reports to one pdf file using vba code. This vba code needs to work on the computers at my work. These computers only contain Adobe Reader, and I am not able to install Acrobat because I am not Administrator. So now my code generates for all the reports a seperate pdf. I had some code to merge these pdf files to one pdf file where I use 'Acrobat.CAcroApp'. But i get an error on line:
Set AcroApp = CreateObject("AcroExch.App")
I think I am not able to do this cause the computers only have Adobe Reader installed. Is there a possibility to create one pdf file for multiple reports/pdfs without using Acrobat.
Thx in advance
2 solutions.
Make a master report that has each individual report embedded as a sub report. If it's just a few, it should work fine, but too many may bog down / crash the application.
Here's a VBA way of doing it here.
Without acrobat reader this is indeed not going to work. I, however, am using the following dirty workaround for users without acrobat;
Export all your reports to rich text ("*.RTF" format) in the same folder. Afterwards, you open a word application via access vba, and loop through the RTF files and then copy them into your word file, with a page break after every insert. Then, you save the word document as a .PDF file.
This is a method prone to errors, so if a more experienced user has a better way, please do tell. I'm interested as well!

Drop-down list from a txt file

I want to know if it is possible to create a Drop-down list in HTML using Only a txt file?
I have a list of places/countries, instead of creating for each, can i import it somehow from a txt file while each country is set in a single line?
(i know how it's done using php, i just wonder if it could be done without it)
Using pure html? No.
You will either need to use a preprocessor (sed,perl,etc) to generate the html page or a dynamic language to do it at run time - html is a static language.
You should be able to do it with javascript, but the only way I can think of involves making the txt file available on the web and using a XMLHttpRequest() to get it. There is the new file api in html5, but that is aimed at local (to the client) files.

How do I download contents of an html table generated by play 1.2.7 backend on java in xls

I've generated a table using play's #{list} tag and get pretty decent results. Now I need to be able to generate and download an xls version of the table and have no idea what to do. Any pointers at all will be much appreciated
Well you have various options.
Excel will open HTML files. So instead of rendering your table as HTML you can it to stream it to the browser and set the content type as XLS.
While Excel will open it this it will still be an HTML file rather than an XLS(X) document.
You can generate as CSV from your data model and stream this to the browser. Again this will be a CSV rather than a proper XLS(X) document.
There also seem to be some solutions around which can do it using Javscript. See as a starting point: Generate excel sheet from html tables using jquery
Finally you can can use something like Apache POI or JXLS to generate a 'proper' xls(x) document and stream this to the browser. I have some code here that will export HTML to 'proper' xlsx file if this is the route you wish to go. Workflow is then to create some HTML from your data model and use this to convert to Excel rather than having to programmatically build the Excel document using POI. https://github.com/alanhay/html-exporter

(OpenXML) Add data pages to xml package without framework

lately I've been into combining multiple OpenXML speadsheets via PHPExcel which
showed me that this framework has certain issues which makes it pretty much unusable
for what I want to do (my related SO question).
To make it short: it's hard to guarantee that all formatting features of Excel 2007 will
persist a file merge performed with that particular framework.
Anways, now I'm thinking of a more general approach. I want to open a template XLSX
which contains various formatting and add some plain alphanumeric data worksheets 'at the end' of the workbook.
Is sensefully possible to do the following:
unzip template XLSX
parse XML files
add worksheets
save xml files
rezip files to get valuid XLSX
Any hints or experiences would be highly appreciated.
thanks in advance
K
I haven't worked with .xlsx too much, but I've altered .docx files by manually adding and editing the XML.
The biggest concern with adding new parts to a document is to make sure you update the .rels files. The best way to figure out what needs to be updated is to create a new .xlsx document in Excel, add a worksheet, save the file and then unzip it to see what has changed. You can also use the DocumentReflector tool that comes with the OpenXML SDK if you want to see the internals of the file without having to unzip it.
I found the OpenXML reference manual very helpful when hand editing files because it tells you what elements you have to keep and what elements are optional to make a valid document. It makes it easier to work with when you can remove some of the extraneous elements that Excel adds automatically.