Wednesday, 15 March 2017

Read Excel File as a Data Table using OLEDB


 string filepath = string.Empty;
            filepath = "C:\\TEST.xlsx";
            string sSourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " + filepath + ";  Extended Properties=Excel 8.0";
           
            OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
            //OleDbCommand cmd = new OleDbCommand("SELECT * FROM [CQALRPF$] WHERE [FACNO] =@id", sSourceConnection);
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", sSourceConnection);
            //cmd.Parameters.AddWithValue("@id", 523001740206);
           
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            oleda.Fill(ds);
            DataTable dt = ds.Tables[0];

//To Extract Datable to Excel File [xls Format]

StreamWriter wr = new StreamWriter(@"C:\Users\shareadmin\Desktop\On-Process\\Test_OnProcess.xls");

                try
                {

                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        wr.Write(dt.Columns[i].ToString().ToUpper() + "\t");
                    }

                    wr.WriteLine();

                    //write rows to excel file
                    for (int i = 0; i < (dt.Rows.Count); i++)
                    {
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (dt.Rows[i][j] != null)
                            {
                                wr.Write(Convert.ToString(dt.Rows[i][j]) + "\t");
                            }
                            else
                            {
                                wr.Write("\t");
                            }
                        }
                        //go to next line
                        wr.WriteLine();
                    }
                    //close file
                    wr.Close();
                   
                }

                catch (Exception ex)
                {
                    throw ex;
                }

Tuesday, 6 September 2016

To add 5 and half hours in a selected column of datatable

                 To add 5 and half hours in a selected column of datatable
                for (int i=0; i<dt.Rows.Count; i++)
                {
                    string date1 = dt.Rows[i][5].ToString();
                    string [] date2 = date1.Split(' ');
                    string[] date3 = date2[1].Split(':');
                    int hr = Convert.ToInt32(date3[0]) + 5;
                    int mm = Convert.ToInt32(date3[1]);
                    string mm1 = string.Empty;
                    if (Convert.ToInt32(date3[1]) > 30)
                    {
                        hr = hr + 1;
                        mm = mm - 30;
                        mm1 = mm.ToString("00");
                        //mm = Convert.ToInt32(mm1);
                    }
                    else
                    {
                        mm1 = Convert.ToString((Convert.ToInt32(date3[1]) + 30));
                    }

                    string date4 = Convert.ToString(hr) + ":" + Convert.ToString(mm1) + ":" + Convert.ToString(date3[2]);
                    string date5 = date2[0] + " " + date4;

                    dt.Rows[i][5] = date5;
                    //dt2.Columns.Add("Created");

                }                

Search Filter for List and Document Library

Reference Link

<script type="text/javascript">
 function RedirectUrl() {
 var tb = document.getElementById("tbSearch").value;
 var cs = document.getElementById("sfield").value;
 var url = "";

 if (tb != "") {
  if (cs == "Column5" || cs == "Column6"){
  url = "FilterField1=" + cs + "&FilterValue1=" + tb;
  window.location.href = "AllItems.aspx?" + url;
  }
  else {
  url = "FilterName=" + cs + "&FilterMultiValue=*" + tb + "*";
  window.location.href = "AllItems.aspx?" + url;
  }
  }
  else {
  return false;
  }
 }
 function ClearUrl() {
 window.location.href = "AllItems.aspx";
 }
</script>
Search Field: <select id="sfield">
<option selected value="Title" >Person Name</option>
<option value="Column2">Status</option>
<option value="Column4">Case #</option>
<option value="Column5">Date taken</option>
<option value="Column6">DOB</option>
<option value="Column8">other #</option>
<option value="Column10">Notes</option>
<option value="Column12">employee</option>
</select>
&nbsp;
Search text: <input type="text" id="tbSearch" />
<input type="button" id="btnSearch" value="Search" onclick="return RedirectUrl();" />
<input type="button" id="btnClear" value="Clear" onclick="return ClearUrl();" />

Inside of the List or Library use this java script in Script Editor