Click here to Skip to main content
15,889,116 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Delete confirm() in gride view Pin
keyur satyadev11-Mar-09 21:53
keyur satyadev11-Mar-09 21:53 
GeneralRe: Delete confirm() in gride view Pin
toufiq_raja11-Mar-09 23:30
toufiq_raja11-Mar-09 23:30 
GeneralRe: Delete confirm() in gride view [modified] Pin
keyur satyadev12-Mar-09 2:27
keyur satyadev12-Mar-09 2:27 
AnswerRe: Delete confirm() in gride view Pin
Expert Coming11-Mar-09 22:44
Expert Coming11-Mar-09 22:44 
AnswerRe: Delete confirm() in gride view Pin
kumar_k50811-Mar-09 23:47
kumar_k50811-Mar-09 23:47 
GeneralRe: Delete confirm() in gride view Pin
anujbanka178412-Mar-09 0:53
anujbanka178412-Mar-09 0:53 
Questiona problem of AjaxPro and js Pin
peacefulheart11-Mar-09 19:34
peacefulheart11-Mar-09 19:34 
QuestionExport Datagrid to Excel Pin
rakeshs31211-Mar-09 18:56
rakeshs31211-Mar-09 18:56 
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.HtmlControls;
using System.IO;
using System.Xml.Xsl;
using System.Text;
using System.Threading;

/// <summary>
/// Summary description for ExportToExcel
/// </summary>
public class ExportToExcel
{
	System.Web.UI.Page  page;
	System.Web.HttpResponse Response;

	public ExportToExcel(System.Web.UI.Page page)
	{
		page = this.page;
		Response = System.Web.HttpContext.Current.Response;

	}
	public void ExportDataToExcel(DataTable datatable, string[] Headers, string SourcePath, string DestinationPath)
	{
		int i, j;
		string strSourceTemplate = "";
		string strDestFileName = "";
		string strHeaders = "";
		string strRow = "";
      
		System.Data.OleDb.OleDbConnection objXlsCon = null;
		System.Data.OleDb.OleDbCommand objCmd;

		try
		{
			objXlsCon = new System.Data.OleDb.OleDbConnection();
//			if (Headers.Length != datatable.Columns.Count)
//			{
//				throw new Exception("Export Headers List and Table columns should be of same length");
//			}
			if (datatable.Rows.Count > 0)
			{
				strSourceTemplate = SourcePath;
				strDestFileName = DestinationPath;
				//if (File.Exists(strDestFileName))
				//{
				//    File.Delete(strDestFileName);
				//}
				System.IO.File.Copy(strSourceTemplate, strDestFileName);

				objXlsCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDestFileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;MAXSCANROWS=1;\";";
				objXlsCon.Open();
				objCmd = new System.Data.OleDb.OleDbCommand();
				objCmd.Connection = objXlsCon;

				//getting template headers
				for (i = 0; i < Headers.Length; i++)
				{
					strHeaders += Headers[i].ToString() + ",";
				}
				string s=datatable.Columns[0].ColumnName;
				int k=0;
				//getting values 
				foreach (DataRow drow in datatable.Rows)
				{
					for (j = 0; j < datatable.Columns.Count; j++)
					{
						
						//if(Headers.Length > k)
						//if(drow[j].ToString() != "" && drow[j].ToString().Length<255)
						if(Headers.Length > k)
						{
							if(datatable.Columns[j].ColumnName == Headers[k].ToString())
							{
								if(drow[j].ToString() != "" )//&& drow[j].ToString().Length<255)
								{
									strRow += drow[j].ToString() + "','";
									
								}
								else
								{
									//							if(datatable.Columns[j].DataType.FullName == "System.String" || datatable.Columns[j].DataType.FullName == "System.DateTime")
									strRow +="-'"+",'";
									//							else
									//								strRow +="0"+",";

								}
								k++;
							}
							//k++;
						}
					}
                   
					objCmd.CommandText = "Insert into [Sheet1$] (" + strHeaders.Substring(0, strHeaders.Length - 1) + ") values ('" + strRow.Substring(0, strRow.Length - 3) + "')";
					objCmd.ExecuteNonQuery();
					strRow = "";
					j = 0;
					k=0;
				}

			}
		}
		catch (Exception ex)
		{
			throw ex;
		}
		finally
		{
			objCmd = null;
			objXlsCon.Close();
		}
       
	}
	public void Download(string strFileName, string SourceFolder)
	{
		string path = SourceFolder;
		Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName + ".xls");
		Response.ContentType = "application/ms-excel";
		FileStream fs = null;
		fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
		byte[] data = new byte[fs.Length];
		fs.Read(data, 0, (int)fs.Length);
		fs.Close();
		Response.BinaryWrite(data);

		Response.End();

	}

}



This is my cs file to export datagrid to Excel.
But this code is exporting total records
in to excel. I want to insert only 10 records.
Paging is there in the DataGrid,at a time i am
displaying 10 records.When i am giving Export
it must insert only 10 records.What should i
change in this code.Thanks in advance
AnswerRe: Export Datagrid to Excel Pin
Ashfield11-Mar-09 21:53
Ashfield11-Mar-09 21:53 
QuestionRe-using usercontrol into a different project. Pin
Juvil John11-Mar-09 18:10
Juvil John11-Mar-09 18:10 
AnswerRe: Re-using usercontrol into a different project. Pin
Rajeesh MP11-Mar-09 19:17
Rajeesh MP11-Mar-09 19:17 
AnswerRe: Re-using usercontrol into a different project. Pin
Juvil John12-Mar-09 13:20
Juvil John12-Mar-09 13:20 
QuestionSQL Server Express, Authentication, Visual Studio, and a Web Host that doesn't support SQL Server:( Pin
rmterenz11-Mar-09 17:46
rmterenz11-Mar-09 17:46 
AnswerRe: SQL Server Express, Authentication, Visual Studio, and a Web Host that doesn't support SQL Server:( Pin
Rajeesh MP11-Mar-09 19:26
Rajeesh MP11-Mar-09 19:26 
Questionset a page as the main page Pin
prasadbuddhika11-Mar-09 17:44
prasadbuddhika11-Mar-09 17:44 
AnswerRe: set a page as the main page Pin
Abhijit Jana11-Mar-09 18:43
professionalAbhijit Jana11-Mar-09 18:43 
QuestionModify the SqlDataSource without altering the gridview Pin
TerRO_GirL11-Mar-09 11:43
TerRO_GirL11-Mar-09 11:43 
QuestionGridview SqlDataSource SelectCommand Pin
Tomb42111-Mar-09 10:29
Tomb42111-Mar-09 10:29 
AnswerRe: Gridview SqlDataSource SelectCommand Pin
Tomb42111-Mar-09 11:16
Tomb42111-Mar-09 11:16 
GeneralRe: Resolved: Gridview SqlDataSource SelectCommand Pin
Tomb42117-Mar-09 9:23
Tomb42117-Mar-09 9:23 
QuestionHow to send the ID field of a datalist to another page? Pin
Arroci11-Mar-09 9:17
Arroci11-Mar-09 9:17 
AnswerRe: How to send the ID field of a datalist to another page? Pin
Christian Graus11-Mar-09 9:33
protectorChristian Graus11-Mar-09 9:33 
AnswerRe: How to send the ID field of a datalist to another page? Pin
Yusuf11-Mar-09 9:50
Yusuf11-Mar-09 9:50 
GeneralRe: How to send the ID field of a datalist to another page? Pin
Christian Graus11-Mar-09 9:54
protectorChristian Graus11-Mar-09 9:54 
AnswerRe: How to send the ID field of a datalist to another page? Pin
Arroci11-Mar-09 11:18
Arroci11-Mar-09 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.