Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi,

I am exporting data into Excel using a datatable. It is working fine and giving me a number of columns in Excel. However, I want to add and make bold some custom column which should appear above the datatable column in Excel.

If I can change their color then please let me know. See code below.

public void ExportToSpreadsheet(DataTable table, string name)
      {
          HttpContext context = HttpContext.Current;
          context.Response.Clear();
          string ColValue;

          string ColName = "";

          for (int i = 0; i < table.Columns.Count; i++)
          {
              ColName = table.Columns[i].ColumnName.ToString() + "\t";
              context.Response.Write(ColName);
          }



          context.Response.Write(Environment.NewLine);
          foreach (DataRow row in table.Rows)
          {
              for (int i = 0; i < table.Columns.Count; i++)
              {
                  //row[i] = row[i].ToString().Trim().Replace("\t", string.Empty) ;
                  ColName = table.Columns[i].ColumnName.ToString();

                  DateTime tempDate = new DateTime();
                  if (DateTime.TryParse(row[i].ToString().Trim(), out tempDate) == true)
                      ColValue = tempDate.ToString("dd/MM/yyyy");
                  else
                  {
                      ColValue = row[i].ToString();
                  }
                  ColValue = ColValue.ToString().Replace(",", string.Empty) + "\t";
                  ColValue = ColValue.ToString().Replace(Environment.NewLine, " ");
                  ColValue = ColValue.ToString().Replace("\n", " ");
                  ColValue = ColValue.ToString().Replace("&nbsp;", "");
                  ColValue = ColValue.ToString().Replace("-Select-;", "");
                  if (table.Columns[i].DataType.Name == "Boolean")
                  {
                      ColValue = ColValue.ToString().Replace("True", "Yes");
                      ColValue = ColValue.ToString().Replace("False", "No");
                  }

                  context.Response.Write(ColValue);

              }

              context.Response.Write(Environment.NewLine);

          }

          context.Response.ContentType = "application/ms-excel";
          context.Response.AppendHeader("Content-Disposition", "attachment; filename = " + name + ".xls");
          context.Response.End();


      }
Posted
Updated 10-Mar-11 0:39am
v2

1 solution

Without automating Excel, which you shouldn't really do using server side code (and may run into permissions issues anyway), a way to achieve this is to use the similar code above but output valid HTML and save with the .xls extension.

This will let you bold certain columns + apply general HTML styling.

e.g look at examples here

http://forums.asp.net/t/1214938.aspx[^]

For example, create a new text file on your computer. Paste the following HTML into it & save the file. Then change the file extension to .xls & open it in Excel. Notice we've got bold headers + a 'red' style.

XML
<HTML>
<HEAD>
<style type='text/css'>
    table
    {
        cellpadding: 0px;
        font-size: xx-small;
        border-collapse:collapse;
    }
    td.some-style
    {
        border: 1px solid black;
        color: red;
    }
</style>
</HEAD>
<BODY>
<TABLE border=0>
        <TR>
        <Td></Td>
        <Td></Td>
        <Td></Td>
        <Td class="some-style">This is an example</Td>
        <Td></Td>
     </TR>
    <thead>
        <TR>
        <Th> ProductID</Th>
        <Th> Product Name</Th>
        <Th> Sales Person</Th>
        <Th> Date Closed</Th>
        <Th> Sale Amount</Th>
        </TR>
    </thead>
    <TR>
        <TD>1</TD>
        <TD>Code Project</TD>
        <TD>Dylan Morley</TD>
        <TD>10/03/2010</TD>
        <TD>50.00</TD>
    </TR>
    <TR>
        <TD>2</TD>
        <TD>Microsoft</TD>
        <TD>Dylan Morley</TD>
        <TD>10/03/2010</TD>
        <TD>50.00</TD>
    </TR>
</TABLE>
</BODY>
</HTML>



You could create this HTML quite easily in your export routine


NB: This would only be a valid solution for machines with Excel 2003 or greater installed, which I think is when then first introduced HTML capabilities?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900