The following code exports some data, downloading it in an XLS file.
What I want is to give all the three list and export each list of data in a separate sheet. How can I do that?
My code looks like this:
public bool ExportQuestionSet(int QuestionSetNo)
{
ExportResponse response = new ExportResponse();
QuestionSetTbl questionSetTbl = _questionSetDAO.GetQuestionSetByQuestionSetNo(QuestionSetNo);
QuestionSetContract questionSetContract = GetQuestionSetByQuestionSetNo(QuestionSetNo);
GridView gv = new GridView();
gv.DataSource = questionSetContract.QuestionsInfoList;
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=QuestionSet.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return response.TaskComplete;
}
You can't output HTML and call it Excel. This works well for a reasonably formatted table, but it isn't an Excel file and will never be recognized as such. The file can be read, but it isn't possible to export multiple sheets.
Use a library to create a proper Excel file.
I would suggest using OpenXml, as it's quite fast and will create the correct Excel file. Using the sample in this thread: https://stackoverflow.com/a/11812551/1361993 you can just create a dataset with multiple datatables, which will in turn be outputted to the respective sheets in the file.
Related
I want to make a button that makes the download of a DataTable object using an HTTP request.
I already have this code that can print .xls files by changing the contentType as indicated in comment.
However, this does not work with xlsx file.
private void DownloadExcel(DataSet ds, string excelFileNameNowDateTime)
{
System.IO.StringWriter tw = new System.IO.StringWriter(); ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
System.Web.UI.WebControls.DataGrid dgGrid = new System.Web.UI.WebControls.DataGrid();
dgGrid.DataSource = ds.Tables[0];
// Get the HTML for the control.
dgGrid.HeaderStyle.Font.Bold = false;
dgGrid.DataBind();
dgGrid.RenderControl(hw);
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//string contentType = "application/vnd.ms-excel";
Response.ContentType = contentType;
this.EnableViewState = true;
Response.Clear();
//Response.ContentEncoding = System.Text.Encoding.Default;
Response.AddHeader("content-disposition", String.Format(#"attachment; filename={0}", excelFileNameNowDateTime + ".xlsx"));
Response.Write(tw.ToString());
CloseHTTPResponse();
}
While opening the file is giving me the error
Excel cannot open the file .xlsx because the file format is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Any idea on why this does not work?
It looks to me like you're trying to send HTML contents with .xlsx extension. This won't work since XLSX is fundamentally not HTML, but zipped XML with quite specific (and convoluted) schema. The simplest way to achieve your goal would be to use one of the existing 3rd party libraries that can generate XLSX on the fly. I personally had used ClosedXML for a similar task, but there are many other options at NuGet.
This code doesn't produce an Excel file, it produces an HTML file with a fake extension. Excel won't be fooled, it will try to import this HTML file using the user locale's defaults. This will easily lead to problems if the decimal separator is different or the cells contain text that interferes with HTML.
There's no reason for such code. XLSX is a ZIP package containing well-formed XML files. You can use the Open XML SDK to create Excel files at a low level or you can use libraries like EPPlus (57M downloads), ClosedXML (27M), NPOI (22M) and more, to create real Excel files.
With EPPlus, creating an Excel file from a DataTable is really a single command, sheet.Cells.LoadFromDataTable(dt);
public byte[] Export(DataTable dt)
{
using (var p = new ExcelPackage())
{
var ws = p.Workbook.Worksheets.Add("SomeSheet");
var table=ws.Cells.LoadFromDataTable(dt);
var excelData = package.GetAsByteArray();
return excelData;
}
}
After that, you write the output to the Response stream :
var bytes = Export(dt);
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", $"attachment;filename={excelFileNameNowDateTime}.xlsx");
Response.BinaryWrite(bytes);
Response.End();
For ASP.NET (no .NET Core Required solution).
using ClosedXML.Excel;
private void DownloadExcelClosedXML(DataTable dt)
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add(dt);
using (var stream = new MemoryStream())
{
workbook.SaveAs(stream);
var content = stream.ToArray();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheet.sheet";
Response.AddHeader("content-disposition", "attachment; filename=name_you_file.xlsx");
Response.BinaryWrite(content);
Response.End();
}
}
}
I have this method of creating an Excel spreedsheet with my data listed below.
List<ReportData> rd = ReportData.getReportData(startTime, endTime, lNumber, sNumber);
DataTable dt = buildDataTable(rd);
//we actually ahve to have a gridview to put into excel
GridView gv = new GridView();
gv.DataSource = dt;
gv.DataBind();
//flush everything from response to be sure we get a clean start
Response.ClearContent();
//now we create excel file
Response.AddHeader("content-disposition", "attachment;filename=UserRpt.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
gv.RenderControl(tw);
//send xls as response
Response.Write(sw.ToString());
Response.End();
My question is how can make it create more pages and write the data to them without creating multiple reports for each individual thing? Example: Page one display the first gv, second page display the second gv and so on. When I add a new grid it does not create a new page it just puts everything back to back. I hope this makes sense I will be happy to try to clarify anything. Thank you for your help!
There are lots of libraries that will help you with this. Some require Office to be installed, others don't.
Maybe try
http://www.nuget.org/packages/ExcelGenerator
or
http://closedxml.codeplex.com/
I am working as Social Media Developer at Aspose and our Aspose.Cells API can be used to implement your requirement with ease. Please see the following sample code in this regard which exports the data from 3 different gridviews to three different excel sheets:
//Create a new Workbook
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
//Add 2 more worksheets other than default worksheet
for (int i = 1; i <= 2; i++)
{
//Add a new worksheet
workbook.Worksheets.Add();
}
//Import data from gridviews to worksheets
workbook.Worksheets[0].Cells.ImportGridView(Gridview1, 0, 0, true, false, true);
workbook.Worksheets[1].Cells.ImportGridView(Gridview2, 0, 0, true, false, true);
workbook.Worksheets[2].Cells.ImportGridView(Gridview3, 0, 0, true, false, true);
//Save the worksheet
workbook.Save("C:\\Data\\Aspose.xls");
I'm using this function to create an Excel file from my SQL Server query
DataTable dt = new DataTable();
SqlConnection objcon = new SqlConnection(connectionString);
string sql = "My SELECT SQL";
SqlDataAdapter objda = new SqlDataAdapter(sql, objcon);
objda.Fill(dt);
GridView gvreport = new GridView();
gvreport.DataSource = dt;
gvreport.DataBind();
string fileName = string.Format("fileNameHere");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvreport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
return null;
Which worked great until I now realized that it does not work so well when its trying to write over 11,000 rows.
The Excel file itself is fine but after around 11,000 rows it suddenly breaks during one and after that all rows are blank. The rows themselves are still "created" with an border and everything but they are all blank.
Any idea what is causing this? Or is there any better way to create an Excel file like this?
Well, in your case you are not actually generating a real Excel file, just plain HTML and telling Excel to open it and convert it. You may encounter other problems using this approach with OpenOffice or LibreOffice or any other tool that actually expects a real Excel file.
I would look into a more robust solution, by using either OpenXml directly, or indirectly through ClosedXml or some other similar component.
For example, if you were to use ClosedXml, you can do something like this:
var wb = new XLWorkbook();
var dataTable = GetTable("Information");
// Add a DataTable as a worksheet
wb.Worksheets.Add(dataTable);
wb.SaveAs("AddingDataTableAsWorksheet.xlsx");
Extracted from: https://closedxml.codeplex.com/wikipage?title=Adding%20DataTable%20as%20Worksheet
However, in your case instead of saving to a physical file you'd output the stream directly to the Response.
i have been trying export grid view to doc . and facing issues .
List of data contains data from database in from of List<>.
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = ListofData;
GridView1.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Export-Grid.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
problem with this approach is
1) when i open Export-Grid.doc in Office 2013 it prompts me that u want to open file as HTml Document , PDF document etc . if i try to open document using HTML it writes all data in file but in html form and if i use Plain text option it do same thing writes html data.
2) if i try to open Export-Grid.doc using open office it open the document but columns width is to big
i m using asp.net mvc. can some one help me how can i fix this or let me know wat m doing wrong ???
any help will be appreciated
I have to export some data from an asp.net page to excel, so I use basically a table to create the custom headers I need then a GridView. All the data displays correctly when exported to excel, but when I added an Logo image to the html, it doesn't show up on the Excel file when exported.
Since I need to export it to Excel 2007 or later, I know I can use the Open XML stuff from Microsoft to export the data, the problem is that I already have everything done on the code and I wanted to know if there is another way to do that instead of doing all over again using Open XML.
If there isn't a way to do that without using Open XML, can anyone show me how I could export the data to it? I tried once but I didn't have much success. =/
BTW, I'm using C#.
Thanks in advance!
I've updated the code and now it looks like this, but I still can't see the image... =/
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
frmPlanoAcao.RenderControl(htmlWrite);
StringWriter w = new StringWriter();
HtmlTextWriter t = new HtmlTextWriter(w);
imgLogo.RenderControl(t);
var byte_array = System.Text.Encoding.ASCII.GetBytes(w.ToString());
Response.Write(stringWrite.ToString());
this.EnableViewState = false;
Response.Clear();
Response.Buffer = false;
Response.Charset = "ISO-8859-1";
Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252);
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "plano_acao.xls"));
Response.ContentType = "application/vnd.ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
Response.Write(getExcelStyling());
Response.OutputStream.Write(byte_array, 0, byte_array.Length);
Response.Write(stringWrite.ToString());
Response.Write("</body>");
Response.Write("</html>");
Response.End();
Try the following code. I have tested at local IIS and it is working properly.
Including the image like Header Image/Logo on top of the grid data.
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgrExport.DataSource = dtExport;
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = #"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());
Response.End();
you can customize your image's height and width as per your requirement. Same height and width setting will be required for the <TD> tag.
You have to get images in StringWriter and use System.Text.Encoding.ASCII.GetBytes(stringwriter string) in array of bytes.
then write these as outputStream.Write(byte_array, 0, byte_array.Length);
where outputstream is HttpContext Response outputstream.
If you say, to convert Grid, Images, everything in one go to Excel, i have not tried. But, i can say, you can individually have both of them into Excel by their way.
If you are using C# and want to export to Excel I would recommend EPPLUS
http://epplus.codeplex.com/
Will save you a whole lot of troubles now and in the future.