Export html report to excel - c#

I have an html report having different sections like report header, report footer and the report data which needs to be exported to excel as it is. I am able to do this but the format of the report in excel is not proper. How can i make the format of the report as it is in the html report
i have used the following code to export html report with header and data to excel
Response.ContentType = "application/vnd.ms-excel";
string filename = "ECC_EMEA_" + String.Format("{0:dd_MMM_yy}", DateTime.Now.Date) + "_Summary.xls";
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
string htmlString = sb.ToString();
for (int i = 0; i < dt.Columns.Count;i++ )
Response.Write("\t"); ;
Response.Write(htmlString);
Response.End();

ms office html rendering engine is really limited... if you want excel to display report in a proper format you have to build it only with supported html elements, css...
from my experience and from what i remember something like this is not supported (had a similar task to yours)
<table STYLE="background-image:url('url')">
you have to google to see what other limitations are and what elements, css properties are supported.
EDITED
here I have found a msdn link. Word 2007 HTML and CSS Rendering Capabilities in Outlook 2007. Excel html, css limitations will be quite similar to this.

Related

How to force view in browser of pdf and word docs

I am required to open various files types such as pdf and word in browser I see that the most accepted solution seems to be the simlar as to what I have done
public string GetDocument(Guid UserId, string Filename)
{
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
return File(doc, mimeType);
}
My quesiton is how do i turn my raw file on the server to something that can be served to the browser I persume I have to change it to byte if so how would one change a pdf to bytes. And for that matter word files
You can use something like:
public void GetDocument(Guid UserId, string Filename)
{
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
Response.WriteFile(fileName);
Response.End();
}
Note, Response.WriteFile expects the FULL path to the file, so if the filename variable isn't the full path, you'll need to map it.
In the case of the PDF, most browsers will open in inside the browser, for other file types your mileage may vary, depending on whether the browser has the capability to display that file type inline.
You cannot force it from the server end.
Some browsers (such as Chrome) can render a PDF just fine, but a PDF rendered from a server will only open in Chrome only if the .pdf file extension is associated with Chrome. You can't change the associations without access to the machine on which the browser is running.
As far as I know, you cannot open a Word doc directly in any browser. You have to use a viewer.
Using a viewer you can display PDF or Word docs within the page-- this is different from downloading the file and displaying it natively, but it may suit your purpose.
A few viewers are freely available. Here's two.
Google docs viewer
Office Web Apps viewer

How to export to excel from a report on SSRS via ReportViewer control

Maybe this question was asked already feel free to point me to the right direction if it is.
I have a report on SSRS and my Dataset/Datasource is there of course. The Dataset is a Dynamic Query.
The idea is to export the result of the report via Excel.
I have a web server control ReportViewer in my Aspx. Does anyone knows if there is a method to export to excel and how I can code it in my code behind.
Please let me know if you need more info and I will post it.
EDIT:
Checking the info and the other links I did the following Method.
private void CreateEXCEL(Dictionary<string, string> parametros, string nombreReporte)
{
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
List<ReportParameter> parameters = new List<ReportParameter>();
foreach (var d in parametros)
{
parameters.Add(new ReportParameter(d.Key, d.Value));
}
// Setup the report viewer object and get the array of bytes
MyReportViewer.ProcessingMode = ProcessingMode.Remote;
MyReportViewer.ServerReport.ReportPath = nombreReporte;
MyReportViewer.ServerReport.SetParameters(parameters);
byte[] bytes = MyReportViewer.ServerReport.Render("EXCEL", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
// Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
Response.Buffer = true;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + nombreReporte + "." + extension);
Response.BinaryWrite(bytes); // create the file
Response.Flush(); // send it to the client to download
}
But when I execute my web app and call this method I have the following error:
The Server commited a protocol infraction Section=ResponseStatusLine
Did anyone come accross a problem like this?
You can use .Render method:
LocalReport.Render
ServerReport.Render
The example provided by MSDN is for Excel but you can use the same method for PDF and any other supported formats.
Note that is also possible to manually export to Excel/PDF using the built-in functionality of ReportViewer.
Hi buddy if your columns are dynamic in the dataset then there is no such optio n in SSRS. But I can give you a hint like when you edit and SSRS it would like XML.
Keeping that in mind you can construct the entire structure of the dataset as an RDLC file from your code.
To be specific you need to generate RDLC file during runtime. Use the some common template and replace the template place holders with column value's
Personally I assure you it won't take much time to export the data. But you should put some effort to achieve this.
I'm telling this solution from my past experience. I have done something like this :)
Feel free to ask if you need more information. But don't ask code.
The Server commited a protocol infraction Section=ResponseStatusLine
Here's an answer that might help:
https://stackoverflow.com/a/4289266/3854195

How to make a real XLS file on asp.net c#?

I have this code to extract a table to computer in a .xls file:
// I have a string which contains HTML table codes, named as excelTable
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Disposition", String.Format("Attachment;Filename=file.xls", ));
response.Buffer = true;
response.ContentEncoding = System.Text.Encoding.Default;
response.ContentType = "application/vnd.ms-excel";
response.Write(excelTable);
response.End();
And I have seen that this is not a real .xls file. I can open it in Notepad and see my standart html table codes.
Now I understand that defining ContentType is not enough. So what else can I do to generate a pure .xls or .xlsx file? Or Should I certainly use Excel Libraries like OpenXML, Interop, etc. ?
Actual XLS/XLSX data must be sent back - that is, data generated with a library (e.g EEPlus) or other suitable source.
This common hack/approach works because Excel "knows how to read HTML". It doesn't actually turn the HTML into XSL/XLSX, although it can be saved as new spreadsheet once loaded by Excel.
Changing the content type will have no effect on the data, and instead will effectively stop this approach from working: the content type is used for associating the program (i.e. Excel) which will be used to open/read the data.
So yes: to generate a real XLS/XLSX document, use a library.

ASP.Net Export Data to Excel

Wondering what is a good library I can use with VS2005 to export data to a excel file. The file has some formatting like background colors and colspans.
Thanks
Here is some code that uses a trick to output HTML to an Excel file. I have found that you can trick excel into opening an HTML by setting the content type of the output to "application/excel".
In the code below secresults is an HTML div like so:
<div id="secresults" runat="server" visible="false" class="secresults">
Content or data here.
</div>
In code behind:
Response.ClearContent();
string filename = "Output" + istartDate.ToShortDateString() + ".xls";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
secresults.RenderControl(htw);
Response.Write(sw.toString());
Response.End();
I have found that you can use some html formatting in excel. To test which formatting you can use you can create an html file and rename it to a .xls file, then open it with excel. You can get a pretty good idea about what HTML Excel will read.
I would recommend taking the class from the following website, and adapting it to your needs.
Mikes Knowledge Base - ExportToExcel
By default, this class takes a DataSet, DataTable or List<>, and exports it into a genuine Excel 2007 .xlsx file, using the OpenXML libraries (also provided).
It doesn't currently attempt to add any formating to the Excel cells (DataTables only store values, not formating, colors, horizontal alignment, etc !) but it should be a good place to start from.
All source code is provided, free of charge, so you can adapt it as required.
Good luck !
I have used this library in the past, but I normally just spit out a CSV file.
C# class library for exporting data to CSV/Excel file
If you have predefined layout of the document, Templater will probably fit your description.
Take a look at the example on how to use it.
I've used this codeplex project (Excel Package). One technique is to start with a formatted template, then modify the template. That is much easier than applying a lot of styling commands starting with an empty spreadsheet.

Excel Report Setting page setup options in C#

I am using the following approach to create a dynamic Excel Report from HTML in my ASP.NET Application.
While creating the excel report, I want to set the Page Setup options such that the report fits into one page.
This is the approach I'm using to create the excel report...
Please tell me how to fit this report to one page or to zoom in the report?
string sContentType = "application/vnd.excel";
string sFileName = "Detail Qutation.xls";
Hashtable oData = new Hashtable();
oData.Add("Content-Disposition", "attachment;filename=" + sFileName);
oData.Add("Charset", "");
oData.Add("ContentType", sContentType);
oData.Add("Content", sTable.ToString());
oData.Add("IsBinaryContent", false);
this.StoreObject("Document", oData);
You might need to render using htmltextwriter as explained in
http://forums.asp.net/p/1063850/1538066.aspx#1538066
and take the concept to get a solution.

Categories

Resources