asp.net - Downloading in pdf format -


this how i'm downloading gridview in excel. pls pdf download code: string strfilename = "salary_statement" + datetime.now.tostring("ddmmyyyy"); gridview1.allowpaging = false;

        gridview1.headerrow.cells[2].visible = true;         gridview1.headerrow.cells[3].visible = true;         (int = 0; < gridview1.rows.count; i++)         {             gridviewrow row = gridview1.rows[i];          }           gridview1.headerrow.style.add("background-color", "#e5e1e1");          (int = 0; < gridview1.rows.count; i++)         {             gridviewrow row = gridview1.rows[i];             row.attributes.add("class", "textmode");         }         response.clear();         response.buffer = true;         response.addheader("content-disposition",         "attachment;filename=" + strfilename + ".xls");         response.charset = "";         response.contenttype = "application/vnd.ms-excel";         stringwriter sw = new stringwriter();         htmltextwriter hw = new htmltextwriter(sw);         gridview1.rendercontrol(hw);          string style = @"<style> .textmode { mso-number-format:\@; } </style>";         response.write(style);         response.output.write(sw.tostring());         response.flush();          response.end();         gridview1.allowpaging = true;//ab 

try this. following code shows how download

http://www.codeproject.com/questions/317301/code-of-downloading-pdf-file-in-asp-net

using system;  using system.data;  using system.configuration;  using system.web;  using system.web.security;  using system.web.ui;  using system.web.ui.webcontrols;  using system.web.ui.webcontrols.webparts;  using system.web.ui.htmlcontrols;  using itextsharp.text;  using itextsharp.text.html;  using itextsharp.text.pdf;       protected void exporttopdf(gridview gvreport, bool landscape)      {          int noofcolumns = 0, noofrows = 0;          datatable tbl = null;             if (gvreport.autogeneratecolumns)          {              tbl = gvreport.datasource datatable; // gets datasource of gridview control.              noofcolumns = tbl.columns.count;              noofrows = tbl.rows.count;          }          else          {              noofcolumns = gvreport.columns.count;              noofrows = gvreport.rows.count;          }             float headertextsize = 8;          float reportnamesize = 10;          float reporttextsize = 8;          float applicationnamesize = 7;             // creates pdf document          document document = null;          if (landscape == true)          {              // sets document a4 size , rotates orientation of page landscape.              document = new document(pagesize.a4.rotate(), 0, 0, 15, 5);          }          else          {              document = new document(pagesize.a4, 0, 0, 15, 5);          }             // creates pdfptable column count of table equal no of columns of gridview or gridview datasource.          itextsharp.text.pdf.pdfptable maintable = new itextsharp.text.pdf.pdfptable(noofcolumns);             // sets first 4 rows of table header rows repeated in pages.          maintable.headerrows = 4;             // creates pdfptable 2 columns hold header in exported pdf.          itextsharp.text.pdf.pdfptable headertable = new itextsharp.text.pdf.pdfptable(2);             // creates phrase hold application name @ left hand side of header.          phrase phapplicationname = new phrase("sample application", fontfactory.getfont("arial", applicationnamesize, itextsharp.text.font.normal));             // creates pdfpcell accepts phrase parameter.          pdfpcell clapplicationname = new pdfpcell(phapplicationname);          // sets border of cell zero.          clapplicationname.border = pdfpcell.no_border;          // sets horizontal alignment of pdfpcell left.          clapplicationname.horizontalalignment = element.align_left;             // creates phrase show current date @ right hand side of header.          phrase phdate = new phrase(datetime.now.date.tostring("dd/mm/yyyy"), fontfactory.getfont("arial", applicationnamesize, itextsharp.text.font.normal));             // creates pdfpcell accepts date phrase parameter.          pdfpcell cldate = new pdfpcell(phdate);          // sets horizontal alignment of pdfpcell right.          cldate.horizontalalignment = element.align_right;          // sets border of cell zero.          cldate.border = pdfpcell.no_border;             // adds cell holds application name headertable.          headertable.addcell(clapplicationname);          // adds cell holds date headertable.          headertable.addcell(cldate);          // sets border of headertable zero.          headertable.defaultcell.border = pdfpcell.no_border;             // creates pdfpcell accepts headertable parameter , adds cell main pdfptable.          pdfpcell cellheader = new pdfpcell(headertable);          cellheader.border = pdfpcell.no_border;          // sets column span of header cell noofcolumns.          cellheader.colspan = noofcolumns;          // adds above header cell table.          maintable.addcell(cellheader);             // creates phrase holds file name.          phrase phheader = new phrase("sample export", fontfactory.getfont("arial", reportnamesize, itextsharp.text.font.bold));          pdfpcell clheader = new pdfpcell(phheader);          clheader.colspan = noofcolumns;          clheader.border = pdfpcell.no_border;          clheader.horizontalalignment = element.align_center;          maintable.addcell(clheader);             // creates phrase new line.          phrase phspace = new phrase("\n");          pdfpcell clspace = new pdfpcell(phspace);          clspace.border = pdfpcell.no_border;          clspace.colspan = noofcolumns;          maintable.addcell(clspace);             // sets gridview column names table headers.          (int = 0; < noofcolumns; i++)          {              phrase ph = null;                 if (gvreport.autogeneratecolumns)              {                  ph = new phrase(tbl.columns[i].columnname, fontfactory.getfont("arial", headertextsize, itextsharp.text.font.bold));              }              else              {                  ph = new phrase(gvreport.columns[i].headertext, fontfactory.getfont("arial", headertextsize, itextsharp.text.font.bold));              }                 maintable.addcell(ph);          }             // reads gridview rows , adds them maintable          (int rowno = 0; rowno < noofrows; rowno++)          {              (int columnno = 0; columnno < noofcolumns; columnno++)              {                  if (gvreport.autogeneratecolumns)                  {                      string s = gvreport.rows[rowno].cells[columnno].text.trim();                      phrase ph = new phrase(s, fontfactory.getfont("arial", reporttextsize, itextsharp.text.font.normal));                      maintable.addcell(ph);                  }                  else                  {                      if (gvreport.columns[columnno] templatefield)                      {                          databoundliteralcontrol lc = gvreport.rows[rowno].cells[columnno].controls[0] databoundliteralcontrol;                          string s = lc.text.trim();                          phrase ph = new phrase(s, fontfactory.getfont("arial", reporttextsize, itextsharp.text.font.normal));                          maintable.addcell(ph);                      }                      else                      {                          string s = gvreport.rows[rowno].cells[columnno].text.trim();                          phrase ph = new phrase(s, fontfactory.getfont("arial", reporttextsize, itextsharp.text.font.normal));                          maintable.addcell(ph);                      }                  }              }                 // tells maintable complete row if cell left incomplete.              maintable.completerow();          }             // gets instance of document created , writes output stream of response object.          pdfwriter.getinstance(document, response.outputstream);             // creates footer pdf document.          headerfooter pdffooter = new headerfooter(new phrase(), true);          pdffooter.alignment = element.align_center;          pdffooter.border = itextsharp.text.rectangle.no_border;             // sets document footer pdffooter.          document.footer = pdffooter;          // opens document.          document.open();          // adds maintable document.          document.add(maintable);          // closes document.          document.close();             response.contenttype = "application/pdf";          response.addheader("content-disposition", "attachment; filename= sampleexport.pdf");          response.end();      }


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -