Friday, 22 May 2015

google+crawl+date

1) get remote page
2) parse date/time 
As for the first step you can use
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://...");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();
where http:// must be a query for your web site, e.g. http://webcache.googleusercontent.com/search?&q=cache%3Aasp.net
Parsing text from htmlText can be done using string functions, or by Regex.

ref:

Wednesday, 20 May 2015

Convert to Tiff to pdf using itextsharp c#

            // creation of the document with a certain size and certain margins
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

            // creation of the different writers
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(Server.MapPath("~/App_Data/result.pdf"), System.IO.FileMode.Create));

            // load the tiff image and count the total pages
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(Server.MapPath("~/App_Data/source.tif"));
            int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            for (int k = 0; k < total; ++k)
            {
                bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
               
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Tiff);
                img.SetAbsolutePosition(0, 0);
                img.ScaleAbsoluteHeight(document.PageSize.Height);
                img.ScaleAbsoluteWidth(document.PageSize.Width);
                cb.AddImage(img);

                document.NewPage();               
            }
            document.Close();

Tuesday, 17 March 2015

Multiple sheet Excel Download c#

 private void writeDataToExcelFile(
         DataSet ds)
    {
        var workbook = new HSSFWorkbook();
        String sheetName = "Orignal";
        string filename = Convert.ToString(ds.Tables[0].Rows[0]["filename"]);
        for (int i = 0; i < ds.Tables.Count; i++)
        {
            DataTable dt = ds.Tables[i];
            if (dt.Rows.Count > 0)
            {
                if (i == 0)
                    sheetName = "Original";
                else sheetName = "plagiarism";
                var Sheet = workbook.CreateSheet(sheetName);

                int cols = 0;
                var myRow = Sheet.CreateRow(0);
                var style1 = workbook.CreateCellStyle();

                // cell background
                style1.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.VIOLET.index;

                // font color
                var font1 = workbook.CreateFont();
                font1.Color = NPOI.HSSF.Util.HSSFColor.BLUE.index;
                style1.SetFont(font1);


                foreach (DataColumn column in dt.Columns)
                {
                    var Cell = myRow.CreateCell(cols);
                    Cell.SetCellValue(column.ColumnName);
                    Cell.CellStyle = style1;
                    cols++;
                }


                for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
                {
                    myRow = Sheet.CreateRow(rowNum + 1);
                    for (int cellNum = 0; cellNum < dt.Columns.Count; cellNum++)
                    {
                        var Cell = myRow.CreateCell(cellNum);
                        Cell.SetCellValue(Convert.ToString(dt.Rows[rowNum][cellNum]));
                    }

                }
            }
        }
        using (var buffer = new MemoryStream())
        {
            workbook.Write(buffer);

            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "test.xls"));
            Response.Clear();
            Response.BinaryWrite(buffer.GetBuffer());
            Response.End();
        }


    }

Thursday, 12 March 2015

Export multiple DataSets to multiple Excel sheets dynamically formatted according to the record's data type

   public string DataSetsToExcel(List<DataSet> dataSets, string fileName)
    {
        Microsoft.Office.Interop.Excel.Application xlApp =
                  new Microsoft.Office.Interop.Excel.Application();
        Workbook xlWorkbook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Sheets xlSheets = null;
        Worksheet xlWorksheet = null;

        foreach (DataSet dataSet in dataSets)
        {
            System.Data.DataTable dataTable = dataSet.Tables[0];
            int rowNo = dataTable.Rows.Count;
            int columnNo = dataTable.Columns.Count;
            int colIndex = 0;

            //Create Excel Sheets
            xlSheets = xlWorkbook.Sheets;
            xlWorksheet = (Worksheet)xlSheets.Add(xlSheets[1],
                           Type.Missing, Type.Missing, Type.Missing);
            xlWorksheet.Name = dataSet.DataSetName;

            //Generate Field Names
            foreach (DataColumn dataColumn in dataTable.Columns)
            {
                colIndex++;
                xlApp.Cells[1, colIndex] = dataColumn.ColumnName;

                xlWorksheet.Range[xlApp.Cells[1, colIndex],
           xlApp.Cells[1, colIndex]].Interior.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbCornflowerBlue;
                xlWorksheet.Range[xlApp.Cells[1, colIndex],
      xlApp.Cells[1, colIndex]].Font.Bold = true;
                xlWorksheet.Range[xlApp.Cells[1, colIndex],
xlApp.Cells[1, colIndex]].Font.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbAntiqueWhite;
            }

            object[,] objData = new object[rowNo, columnNo];

            //Convert DataSet to Cell Data
            for (int row = 0; row < rowNo; row++)
            {

                for (int col = 0; col < columnNo; col++)
                {
                    objData[row, col] = dataTable.Rows[row][col];
                }
            }

            //Add the Data
            Range range = xlWorksheet.Range[xlApp.Cells[2, 1], xlApp.Cells[rowNo + 1, columnNo]];
            range.Value2 = objData;

            //Format Data Type of Columns
            colIndex = 0;
            foreach (DataColumn dataColumn in dataTable.Columns)
            {
                colIndex++;
                string format = "@";
                switch (dataColumn.DataType.Name)
                {
                    case "Boolean":
                        break;
                    case "Byte":
                        break;
                    case "Char":
                        break;
                    case "DateTime":
                        format = "dd/mm/yyyy";
                        break;
                    case "Decimal":
                        format = "$* #,##0.00;[Red]-$* #,##0.00";
                        break;
                    case "Double":
                        break;
                    case "Int16":
                        format = "0";
                        break;
                    case "Int32":
                        format = "0";
                        break;
                    case "Int64":
                        format = "0";
                        break;
                    case "SByte":
                        break;
                    case "Single":
                        break;
                    case "TimeSpan":
                        break;
                    case "UInt16":
                        break;
                    case "UInt32":
                        break;
                    case "UInt64":
                        break;
                    default: //String
                        break;
                }
                //Format the Column accodring to Data Type
                xlWorksheet.Range[xlApp.Cells[2, colIndex],
                      xlApp.Cells[rowNo + 1, colIndex]].NumberFormat = format;

            }
        }

        //Remove the Default Worksheet
        ((Worksheet)xlApp.ActiveWorkbook.Sheets[xlApp.ActiveWorkbook.Sheets.Count]).Delete();

        /*
          foreach (Microsoft.Office.Interop.Excel.Range usedRange in xlWorksheet.Rows)
          {
           
                  usedRange.Cells.Interior.Color = Microsoft.Office.Interop.Excel.XlRgbColor.rgbCornflowerBlue;
                  break;
          }*/
        //Save
        if (File.Exists(HttpContext.Current.Server.MapPath("~/Excel/" + fileName + ".xls")))
            File.Delete(HttpContext.Current.Server.MapPath("~/Excel/" + fileName + ".xls"));
        xlWorkbook.SaveAs(HttpContext.Current.Server.MapPath("~/Excel/" + fileName + ".xls"),
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            XlSaveAsAccessMode.xlNoChange,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value,
            System.Reflection.Missing.Value);

        xlWorkbook.Close();
        xlApp.Quit();
        GC.Collect();
        return fileName;
    }

Thursday, 5 February 2015

Radgrid is not cancelling edit forms

rg.MasterTableView.ClearEditItems(); rg.MasterTableView.IsItemInserted = false;

Wednesday, 4 February 2015

Cannot get all rows from Rad Grid-View with DataPager

RadGrid1.AllowPagging = false;
RadGrid1.Rebind();

        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {

        }

RadGrid1.AllowPagging = true;
RadGrid1.Rebind();
(or)
RadGrid1.MasterTableView.AllowPagging = false;

        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {

        }

RadGrid1.MasterTableView.AllowPagging = true;

Tuesday, 6 January 2015

Add on drop event to a div javascript

You need to enable dropping on the target element. Add the ondragover attribute to your div like this:
<div id="bin" ondrop="drop(event)" ondragover="allowDrop(event)">
    <img src="icono.png" style="width:100px; margin-left:500px;display:inline;"/><p>
</div>
and an allowDrop function such as this:
function allowDrop(e) {
    e.preventDefault();
}
http://stackoverflow.com/questions/21999030/add-on-drop-event-to-a-div-no-jquery