Exporting Excel Sheet without getting any warning message

We find many articles in order to export the data into excel sheet.

By using the below code we can export data to excel sheet without getting any warning message when we are trying to open the file.


In order to do this we need to  Add library files of NPOI into our project. This can be added though nuget or you can directly download from the below link and refer the dll into your project.

http://npoi.codeplex.com/releases




After Refering the dlls into your project add the below namespaces

using NPOI.HSSF.UserModel;

using NPOI.SS.UserModel;

public FileContentResult ExportReport()
        {
System.Web.UI.WebControls.GridView gridvw = new System.Web.UI.WebControls.GridView();
            gridvw.AutoGenerateColumns = false;

            BoundField bfield = new BoundField();
            bfield.HeaderText = "ID";
            bfield.DataField = "ID";
            bfield.ItemStyle.CssClass = "textmode";
            gridvw.Columns.Add(bfield);

            BoundField bfield1 = new BoundField();
            bfield1.HeaderText = "Name";
            bfield1.DataField = "Name";
            gridvw.Columns.Add(bfield1);

  gridvw.DataSource = Session["ExcelReportList"];  //bind the datatable to the gridview
            gridvw.DataBind();

var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("sheet1");

            //create bold font for headers
            var font = workbook.CreateFont();
            font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
            //(Optional) set the width of the columns
            sheet.SetColumnWidth(0, 20 * 180);
sheet.SetColumnWidth(1, 20 * 180);
var columns = new[] { "ID", "Name"};
            var headerRow = sheet.CreateRow(0);

            //create header
            for (int i = 0; i < columns.Length; i++)
            {
                var cell = headerRow.CreateCell(i);
                cell.SetCellValue(columns[i]);
                cell.CellStyle = workbook.CreateCellStyle();
                cell.CellStyle.SetFont(font);
            }

int rowNumber = 1;
 int rowcount = 0;
            foreach (GridViewRow row in gridvw.Rows)
            {
                var rows = sheet.CreateRow(rowNumber++);
                for (int k = 0; k < gridvw.Columns.Count && rowcount < gridvw.Rows.Count; k++)
                {
                    var cell = rows.CreateCell(k);
                   
                    String cellText = row.Cells[k].Text;
                    if (cellText == "&nbsp;")
                    {
                        cellText = cellText.Replace("&nbsp;", "");
                    }
                    cell.SetCellValue(columns[k]);
                    cell.SetCellValue((cellText).ToString());
                                    }rowcount++;
            }
            var stream = new MemoryStream();
            workbook.Write(stream);
            stream.Close();
            return File(stream.ToArray(), "application/vnd.ms-excel", "Export.xls");

}

Now the Excel file will open without displaying any warning message.

0 comments:

Post a Comment

Vikram Chandra Theme by BloggerThemes & NewWPThemes Sponsored by iBlogtoBlog