How to Upload and Export Data From Excel File.

To Export a set of data on List we can use the DLL call NPOI.  To Download the NPOI Dll Follow the Link Below https://npoi.codeplex.com/releases.

At First to Export List to Excel the Coding as below :

public FileResult ExportByLocationWise(int LocationId)
        {          
            //Create new Excel workbook
            var workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();

            //Create new Excel sheet
            var sheet = workbook.CreateSheet();

            //font style1: underlined, italic, red color, fontsize=20
            NPOI.SS.UserModel.IFont font1 = workbook.CreateFont();
            font1.Color = HSSFColor.Red.Index;
            font1.IsItalic = true;
            font1.Underline = FontUnderlineType.Double;
            font1.FontHeightInPoints = 20;

            //bind font with style 1
            NPOI.SS.UserModel.ICellStyle style1 = workbook.CreateCellStyle();
            style1.SetFont(font1);


            //(Optional) set the width of the columns    
            sheet.SetColumnWidth(0, 15 * 256);
            sheet.SetColumnWidth(1, 15 * 256);        

            //Create a header row
            var headerRow = sheet.CreateRow(0);

            //Set the column names in the header row
            headerRow.CreateCell(0,CellType.Numeric).SetCellValue("Id");
            headerRow.CreateCell(1).SetCellValue("Name");          
         

            //(Optional) freeze the header row so it is not scrolled
            sheet.CreateFreezePane(0, 1, 0, 1);

            int rowNumber = 1;

            //Populate the sheet with values from the grid data
            foreach (Customer itemPriceLocLst in List)
            {
                //Create a new row
                var row = sheet.CreateRow(rowNumber++);

                //Set values for the cells
                row.CreateCell(0).SetCellValue(Customer.Id);
                row.CreateCell(1).SetCellValue(Customer.Name);                        
            }

            //Write the workbook to a memory stream
            MemoryStream output = new MemoryStream();
            workbook.Write(output);

            //Return the result to the end user

            return File(output.ToArray(),   //The binary data of the XLS file
                "application/vnd.ms-excel", //MIME type of Excel files
                "Customer.xls"); //Suggested file name in the "Save as" dialog which will be displayed to the end user

        }

After that If you want to make some change and add the details, then we can upload the Excel and convert that excel to Data Table we can use it.

To Upload a Excel data and convert to Datatable Coding as below :
On filePath need to pass the Path of the File and that file need to be Closed.

public static DataTable ExcelToDataTable(string filePath)
        {
            DataTable dt = new DataTable();

            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            ISheet sheet = hssfworkbook.GetSheetAt(0);
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();

            IRow headerRow = sheet.GetRow(0);
            int cellCount = headerRow.LastCellNum;

            for (int j = 0; j < cellCount; j++)
            {
                ICell cell = headerRow.GetCell(j);
                dt.Columns.Add(cell.ToString());
            }

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                IRow row = sheet.GetRow(i);
                DataRow dataRow = dt.NewRow();
                if (row == null)
                {
                    break;
                }
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }

                dt.Rows.Add(dataRow);
            }
            return dt;
        }


So, from this Post. I think you can understand on how to upload and export Data from Excel Files.

Comments

Popular posts from this blog

The MVC Programming Model Basic

To Call Controller From Javascript in Ajax

How to User Remember Me on Login By HTTPCookie