c# datagridview - keep formatting on copy/paste into excel

hi all

I have a c# 2010 winforms  application which displays data in a datagridview. The table is formatted using colours and bold font etc etc.

When it is copied and pasted into excel the formatting is lost.

is there any way to keep the formatting on paste into excel?

cheers
cycledudeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You should use HTML, and set the clipboard format as HTML, and then it should keep the style from the DataGridView.
0
cycledudeAuthor Commented:
Sounds interesting

how do you use html in the datagridview?
0
Bob LearnedCommented:
Take a look here:

Copying an HTML-fragment to the Clipboard
http://blog.tcx.be/2005/08/copying-html-fragment-to-clipboard.html

using System;
using System.Text;
using System.Windows.Forms;

class Test {
public static void CopyHtmlToClipBoard(string html) {
   Encoding enc = Encoding.UTF8;

   string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
      + "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";

   string html_begin = "<html>\r\n<head>\r\n"
      + "<meta http-equiv=\"Content-Type\""
      + " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
      + "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
      + "<!--StartFragment-->";

   string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";

   string begin_sample = String.Format(begin, 0, 0, 0, 0);

   int count_begin = enc.GetByteCount(begin_sample);
   int count_html_begin = enc.GetByteCount(html_begin);
   int count_html = enc.GetByteCount(html);
   int count_html_end = enc.GetByteCount(html_end);

   string html_total = String.Format(
      begin
      , count_begin
      , count_begin + count_html_begin + count_html + count_html_end
      , count_begin + count_html_begin
      , count_begin + count_html_begin + count_html
      ) + html_begin + html + html_end;

   DataObject obj = new DataObject();
   obj.SetData(DataFormats.Html, new System.IO.MemoryStream(
      enc.GetBytes(html_total)));
   Clipboard.SetDataObject(obj, true);
}
}

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

cycledudeAuthor Commented:
Hi

How do you enter the html into the datagrid?
0
cycledudeAuthor Commented:
The way I got around this, was to paste special into excel, this kept the formatting of the existing spreadsheet, thus allowing the colours etc to remain intact in the spreadsheet...

bit of a cop out but it was the only way I could get the colour information
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cycledudeAuthor Commented:
could not find another solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.