public void xlsexport(System.Data.DataTable dtGridsource, string Filename) { int rowIndex = -1; int columnIndex = -1; string filename = Filename; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename)); HSSFWorkbook hssfworkbook; hssfworkbook = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "Adya Wifi"; hssfworkbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = dtGridsource.TableName.ToString(); hssfworkbook.SummaryInformation = si; ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1"); IRow row = sheet1.CreateRow(0); for (int i = 0; i < dtGridsource.Columns.Count; i++) { row.CreateCell(i).SetCellValue(dtGridsource.Columns[i].ColumnName.ToString()); } for (rowIndex = 0; rowIndex < dtGridsource.Rows.Count; rowIndex++) { IRow row1 = sheet1.CreateRow(rowIndex + 1); for (columnIndex = 0; columnIndex < dtGridsource.Columns.Count; columnIndex++) { row1.CreateCell(columnIndex).SetCellValue(dtGridsource.Rows[rowIndex][columnIndex].ToString()); } } MemoryStream file = new MemoryStream(); hssfworkbook.Write(file); HttpContext.Current.Response.BinaryWrite(file.GetBuffer()); HttpContext.Current.ApplicationInstance.CompleteRequest(); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)