Wednesday, 30 December 2015

Create and save excel file using StringBuilder in server folder using asp.net c#

public void Save_stringBuilder()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border='1' width='800' align='center' style='text-align:center'><tr><td colspan='3' style=' background-color:#000; height:100; color:#fff; font-size:x-large'>Header</td></tr><tr><td height='100' align='center'><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\a.gif' width='70' height='80'>&nbsp;</td><td>&nbsp;</td><td><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\b.gif' width='70' height='80'></td></tr><tr><td>&asldkfj;laksjdk fj;k as askdjflajksdf;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&asdkf;ajsdlkfj asdjfljasd faskldjflkas;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&asdkjfflkasjd aksdjfl;jalsd skjdflkkjasd fas;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td  height='100' align='center'><img src='C:\\Users\\Krishna\\Desktop\\SaveGridViewAsExcelOnDisk\\b.gif' width='70' height='80' /> </td><td>&nbsp;</td></tr></table>");

        string path = Server.MapPath("~/Files/");
        if (!Directory.Exists(path))  // create folder if not exist
        {
            Directory.CreateDirectory(path);
        }
        using (StringWriter sw = new StringWriter(sb))
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                StreamWriter writer = File.AppendText(path + "SavestringBuilder.xls");
                hw.BeginRender();             
                string html = sb.ToString();
                writer.WriteLine(html);// write file on server folder
                writer.Close();
            }
        }


    }

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...