Just wrapping before you add the string to Word should work fine. Excel is a whole different ballgame, it seems that Excel only picks up the HTML tags after its been copied and pasted from the clipboard.
My suggestion would be to add the content to the cell (including <body><b></b></body> tags), select the cell/s, copy with .Copy and paste with .Paste to see if this works. I would test but dont really have the time today.
Thats the best i can come up with at the moment as i know for a fact that pasting html wrapped text into excel works perfectly fine so hopefully this should work for automation aswell. Im sure if anyone has a better solution they will post, if you're still
having problems let me know and i will find the time to do some testing.
nileshvk
Member
37 Points
57 Posts
Create word/excel document in c# with HTML Formatting
Jul 13, 2012 02:52 PM|LINK
Hi,
I am able to generate word/excel file using c# code, My requirement is to show HTML formatting(bold,italic,font size,etc...) in generated report.
e.g. String str = <Bold> This is Bold string </Bold>
Should appear in word/excel like
This is Bold string
Any help would be highly appreciated.
teh munk
Participant
1466 Points
297 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 13, 2012 03:17 PM|LINK
Have you wrapped the content you are putting in the word document with;
tags?
Also <b></b> is the correct HTML tag to use to embolden text.
nileshvk
Member
37 Points
57 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 13, 2012 03:40 PM|LINK
Wrapping contents did not work.
teh munk
Participant
1466 Points
297 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 13, 2012 04:06 PM|LINK
Please post code.
nileshvk
Member
37 Points
57 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 13, 2012 04:14 PM|LINK
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Visible = true;
Workbook xlWorkbook = xlApp.Workbooks.Add(Type.Missing); //Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.ActiveSheet;
Worksheet xlNewSheet = null; Sheets xlSheets = null; Excel.Range range;
xlSheets = xlWorkbook.Sheets as Sheets; //// The first argument below inserts the new worksheet as the first one
xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing); xlNewSheet.Name = "Summary";
//Excel.Range range = ExcelWorkSheet.Application.get_Range("A1", "B10"); //range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Blue.ToArgb();
xlNewSheet.get_Range("A1", "H25").Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Red.ToArgb(); xlNewSheet.get_Range("A1", "H25").BorderAround(Type.Missing, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Type.Missing);
xlNewSheet.get_Range("A1", "H25").Font.Name = "Arial";
xlNewSheet.Cells[3, 2] = "<html><body><b>FeedBack Report</b></body></html>";
Mohamed Isma...
Member
102 Points
89 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 13, 2012 04:44 PM|LINK
check it out :)
http://support.microsoft.com/kb/316384
Sincerely,
Mohamed Ismail
Please remember to Mark the replies as Answers if they help & unmark them if they provide no help.
nileshvk
Member
37 Points
57 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 16, 2012 02:52 AM|LINK
My problem is not to automate word using c#, but the problem is in showing html formatting on automated word.
teh munk
Participant
1466 Points
297 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 16, 2012 10:30 AM|LINK
Just wrapping before you add the string to Word should work fine. Excel is a whole different ballgame, it seems that Excel only picks up the HTML tags after its been copied and pasted from the clipboard.
My suggestion would be to add the content to the cell (including <body><b></b></body> tags), select the cell/s, copy with .Copy and paste with .Paste to see if this works. I would test but dont really have the time today.
Thats the best i can come up with at the moment as i know for a fact that pasting html wrapped text into excel works perfectly fine so hopefully this should work for automation aswell. Im sure if anyone has a better solution they will post, if you're still having problems let me know and i will find the time to do some testing.
nileshvk
Member
37 Points
57 Posts
Re: Create word/excel document in c# with HTML Formatting
Jul 17, 2012 02:49 AM|LINK
Copy-Paste simply works good, not sure how this can help us in automation.
It would be gr8 if you could provide some solution.
Sage Gu - MS...
Contributor
6693 Points
578 Posts
Microsoft
Re: Create word/excel document in c# with HTML Formatting
Jul 20, 2012 05:01 AM|LINK
Hi nileshvk,
Please try to test the following code below. After click the button, you will get the generated word document with HTML formatting.
aspx file
<body> <form id="form1" runat="server"> <div id="word" runat="server"> <table border="1"> <tr> <td> <span style="color:Red"><b>First Name</b></span> </td> <td> <b>Last Name</b> </td> </tr> <tr> <td> FirstName1 </td> <td> LastName1 </td> </tr> <tr> <td> FirstName2 </td> <td> LastName2 </td> </tr> </table> </div> <asp:Button ID="btnHtmlToWord" runat="server" Text="HtmlToWord" OnClick="btnHtmlToWord_Click" /> </form> </body>code behind
protected void Page_Load(object sender, EventArgs e) { } protected void btnHtmlToWord_Click(object sender, EventArgs e) { Response.AppendHeader("content-disposition", "attachment;filename=FileEName.doc"); Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.ms-word"; this.EnableViewState = false; Response.Write(word.InnerHtml); Response.End(); }You can change the extension of file types for generating excel document.
Regards,
Sage Gu - MSFT
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework