Tuesday, June 16, 2015

C# Tricks








Convert an Array 

 double[] p = p = Array.ConvertAll(query, val => (double)val);



Excel Library From Microsoft

int[] list = new int[] { 6,50,99,35,1,2,7};

Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;

double percentile = wsf.Percentile_Inc(list, 0.9);




Getting Current User

string u = System.Web.HttpContext.Current.User.Identity.Name.Substring(10);

Import Excel to DataTable C#

1:   Add EPPlus.xml & EPPlus.dll
2:   using OfficeOpenXml;
3:   var tbl = new DataTable();

   #region Getting Excel data and store into Memory
   if (uploadExcel.HasFile && Path.GetExtension(uploadExcel.FileName) == ".xlsx")
        {

            using (var excel = new ExcelPackage(uploadExcel.PostedFile.InputStream))
            {

                var ws = excel.Workbook.Worksheets.First();
                var hasHeader = true;  // adjust accordingly
                // add DataColumns to DataTable
                foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
                    tbl.Columns.Add(hasHeader ? firstRowCell.Text
                        : String.Format("Column {0}", firstRowCell.Start.Column));

                // add DataRows to DataTable
                int startRow = hasHeader ? 2 : 1;
                for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
                {
                    var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                    DataRow row = tbl.NewRow();
                    foreach (var cell in wsRow)
                        row[cell.Start.Column - 1] = cell.Text;
                    tbl.Rows.Add(row);
                }
            }

No comments:

Post a Comment

Secure you Asp .NET by Web.config & Global.ascx?

Add to Global.ascx protected void Application_BeginRequest(object sender,EventArgs e)     {         //to remove x frame         Resp...