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
Related
I Have a ReportViewer that is work 100%, but I need it to open in PDF direct
and there is a sample of my code where I'm binding the reportviewer with the data.
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportDataSource source = new ReportDataSource("dsGetTrnsactions", dt);
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(source);
ReportViewer1.DataBind();
ReportViewer1.LocalReport.Refresh();
You need to get the byte array that rappresents the PDF file then you need to open a new window with PDF file. Try this:
byte[] file = ReportViewer1.LocalReport.Render(some parameters);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=Test.pdf");
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(file);
Response.End();
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.
Can we generate an excel document using openXML from a List , but with cutom columns . More like a template . If master excel sheet contains 5 Columns [Column names are Object Properties ] The export functionality export the mentioned 5 Columns only. If template excel file contains 6 Columns the generated file should only ocntains the 6 columns in the order as in template.
Is this way of generation of excel document is possible using OpenXML or not ?
I usually follow the below code for exel generation , but issue if some one want to remove any columns or change order in generated excel i have to rearrange the code every time
System.Web.UI.WebControls.GridView gridvw = new System.Web.UI.WebControls.GridView();
gridvw.DataSource = details.ToList(); //bind the datatable to the gridview
gridvw.DataBind();
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", name));
Response.ContentType = "application/excel";
Response.BufferOutput = true;
StringWriter swr = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(swr);
gridvw.RenderControl(tw);
Response.Write(swr.ToString());
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 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.