Monday, May 10, 2010

how to convert a dataset to excel?

public Stream ConvertDatsetToExcel(DataSet dataset)


{

Stream attachment = null;

string excelContent = "";



excelContent += "<>"; if (dataset.Tables[0].Rows.Count > 0) { excelContent += "<>"; for (int index = 0; index < dataset.Tables[0].Columns.Count; index++) { excelContent += "<>"; } excelContent += " <>"; for (int rowNo = 0; rowNo < dataset.Tables[0].Rows.Count; rowNo++) { excelContent += " <>"; for (int colNo = 0; colNo < dataset.Tables[0].Columns.Count; colNo++) { excelContent += "<>"; } excelContent += " <>"; } } else { excelContent += " <>"; for (int index = 0; index < dataset.Tables[0].Columns.Count; index++) { excelContent += "<>"; } excelContent += " <>"; } excelContent += "
";

excelContent += dataset.Tables[0].Columns[index].ColumnName;

excelContent += "
";

excelContent += dataset.Tables[0].Rows[rowNo][colNo].ToString();

excelContent += "
";

excelContent += dataset.Tables[0].Columns[index].ColumnName;

excelContent += "
";



attachment = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(excelContent));



return attachment;

}


for further information visit
http://www.scribd.com/groups/discussion/40777-how-to-convert-a-datset-to-excel-content-using-c-net