Deepankar's profile.NET Rocks!!!PhotosBlogListsMore Tools Help

    Convert a DataSet to a String Variable

    Coverts a DataTable into an Excel string.  Use this method to save a datatable as an Excel file.  It loops through each of the DataTables in the DataSet. There is also an option of whether to print the table headers (column names).

    public static string DataSetToString(DataSet dataSet, string title ,  bool printHeaders)
    {
    StringBuilder sb = new StringBuilder();
    // start the excel file headers
    sb.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ");
    sb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=windows-1252\">");
    sb.Append("<meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 9\"><!--[if gte mso 9]>");
    sb.Append("<xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + title + "</x:Name><x:WorksheetOptions>");
    sb.Append("<x:Selected/><x:ProtectContents>False</x:ProtectContents><x:ProtectObjects>False</x:ProtectObjects>");
    sb.Append("<x:ProtectScenarios>False</x:ProtectScenarios></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>");
    sb.Append("<x:ProtectStructure>False</x:ProtectStructure><x:ProtectWindows>False</x:ProtectWindows></x:ExcelWorkbook></xml>");
    sb.Append("<![endif]--></head><body><table>");

    // Start the excel worksheet
    sb.Append("</table><table><tr><td colspan=\"4\"><h3><b>" + title + "</b></h3></td></tr></table>");
    sb.Append("</table><table><tr><td></td></tr></table>");

    for (int table = 0; table < dataSet.Tables.Count; table++)
    {
    sb.Append("<table>");
    if (printHeaders)
    {
    sb.Append(string.Format("<TR><td colspan = \"{0}\"><B>{1}</b></td></tr>",
    dataSet.Tables[table].Columns.Count,dataSet.Tables[table].TableName.ToUpper()));
    sb.Append("<TR BgColor = \"#CCCCCC\">");

    for (int iCol = 0; iCol < dataSet.Tables[table].Columns.Count; iCol++)
    {
    sb.Append("<td><b>" + dataSet.Tables[table].Columns[iCol].ColumnName + "</b></td>");
    }
    sb.Append("</TR>");
    }

    for (int iRow = 0; iRow < dataSet.Tables[table].Rows.Count; iRow++)
    {
    sb.Append("<TR>");
    for (int iCol = 0; iCol < dataSet.Tables[table].Columns.Count; iCol++)
    {
    sb.Append("<td>" + dataSet.Tables[table].Rows[iRow][iCol] + "</b></td>");
    }
    sb.Append("<TR>");
    }

    sb.Append("</table><table><tr><td></td></tr></table>");
    }
    return sb.ToString();
    }

    Comments (3)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    No namewrote:
    http://www.toplaptopbatteries.com/hp/dv6500.htm hp dv6500 battery
    http://www.toplaptopbatteries.com/hp/dv6600.htm hp dv6600 battery
    http://www.toplaptopbatteries.com/hp/presario-v3000.htm hp presario v3000 battery
    http://www.toplaptopbatteries.com/hp/presario-v6000.htm hp presario v6000 battery
    http://www.toplaptopbatteries.com/hp/presario-c700.htm hp presario c700 battery
    http://www.toplaptopbatteries.com/hp/hstnn-lb31.htm hp hstnn-lb31 battery
    http://www.toplaptopbatteries.com/hp/411462-421.htm hp 411462-421 battery
    http://www.toplaptopbatteries.com/hp/ev088aa.htm hp ev088aa battery
    http://www.toplaptopbatteries.com/hp/ex941aa.htm hp ex941aa battery
    http://www.toplaptopbatteries.com/hp/dv9000.htm hp dv9000 battery
    http://www.toplaptopbatteries.com/hp/dv9200.htm hp dv9200 battery
    http://www.toplaptopbatteries.com/hp/dv9100.htm hp dv9100 battery
    http://www.toplaptopbatteries.com/hp/dv9500.htm hp dv9500 battery
    http://www.toplaptopbatteries.com/hp/dv9600.htm hp dv9600 battery
    http://www.toplaptopbatteries.com/hp/dv9700.htm hp dv9700 battery
    http://www.toplaptopbatteries.com/hp/hstnn-lb33.htm hp hstnn-lb33 battery
    http://www.toplaptopbatteries.com/hp/416996-131.htm hp 416996-131 battery
    http://www.toplaptopbatteries.com/hp/hstnn-ib44.htm hp hstnn-ib44 battery
    http://www.toplaptopbatteries.com/hp/510.htm hp 510 battery
    http://www.toplaptopbatteries.com/hp/530.htm hp 530 battery
    http://www.toplaptopbatteries.com/hp/hstnn-fb44.htm hp hstnn-fb44 battery
    http://www.toplaptopbatteries.com/hp/hstnn-c29c.htm hp hstnn-c29c battery
    http://www.toplaptopbatteries.com/hp/nc6000.htm hp nc6000 battery
    http://www.toplaptopbatteries.com/hp/nx5000.htm hp nx5000 battery
    http://www.toplaptopbatteries.com/hp/nw8000.htm hp nw8000 battery
    http://www.toplaptopbatteries.com/hp/nc8000.htm hp nc8000 battery
    http://www.toplaptopbatteries.com/hp/v1000.htm hp v1000 battery
    http://www.toplaptopbatteries.com/hp/dg105a.htm hp dg105a battery
    http://www.toplaptopbatteries.com/sony/vgp-bps2a.htm sony vgp-bps2a battery
    http://www.toplaptopbatteries.com/sony/vgp-bps2b.htm sony vgp-bps2b battery
    http://www.toplaptopbatteries.com/sony/vgp-bps2.htm sony vgp-bps2 battery
    http://www.toplaptopbatteries.com/sony/vgp-bpl2.htm sony vgp-bpl2 battery
    http://www.toplaptopbatteries.com/sony/vgp-bps2b.htm sony vgp-bps2b battery
    http://www.toplaptopbatteries.com/sony/vgp-bps2c.htm sony vgp-bps2c battery
    http://www.toplaptopbatteries.com/sony/vgp-bpl2c.htm sony vgp-bpl2c battery
    http://www.toplaptopbatteries.com/toshiba/pa3399u-1bas.htm toshiba pa3399u-1bas battery
    http://www.toplaptopbatteries.com/toshiba/satellite-a100.htm toshiba satellite a100 battery
    http://www.toplaptopbatteries.com/toshiba/satellite-m100.htm toshiba satellite m100 battery
    http://www.toplaptopbatteries.com/toshiba/pa3356u-1bas.htm toshiba pa3356u-1bas battery
    http://www.toplaptopbatteries.com/toshiba/satellite-a50.htm toshiba satellite a50 battery
    http://www.toplaptopbatteries.com/toshiba/satellite-a55.htm toshiba satellite a55 battery
    http://www.toplaptopbatteries.com/toshiba/pa3451u.htm toshiba pa3451u battery
    http://www.toplaptopbatteries.com/toshiba/pa3457u.htm toshiba pa3457u battery
    http://www.toplaptopbatteries.com/toshiba/satellite-a80.htm toshiba satellite a80 battery
    http://www.toplaptopbatteries.com/toshiba/satellite-a85.htm toshiba satellite a85 battery
    Oct. 31
    No namewrote:

    Hi,Do you have used LCDs, second hand LCDs, used flat screens and used LCD monitors? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels. website:www.sstar-hk.com[ahifhbafddbbci]

    Oct. 17
    No namewrote:

    Hi,Do you need digital signage, digital sign, ad players and ad displays? Please go Here:www.amberdigital.com.hk(Amberdigital).we have explored and developed the international market with professionalism. We have built a widespread marketing network, and set up a capable management team dedicated to provide beyond-expectation services to our customers.

    amberdigital Contact Us

    website:www.amberdigital.com.hk
    alibaba:amberdigital.en.alibaba.com[hgabefbadifdac]

    Sept. 23

    Trackbacks

    The trackback URL for this entry is:
    http://deepankar.spaces.live.com/blog/cns!55EC2D3346C64489!570.trak
    Weblogs that reference this entry
    • None