Reputation: 31
I am using Microsoft.Office.Interop.Word to convert html to word.I am creating html file on local machine then converting it to word file.But word file is not showing correct formatting. It is just showing images. I have referred stackoverflow questions.But I found no luck. My sample html file is as shown below:-
<!doctype html>
<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<title="Device Information" />
<style>
table.main {
width: 700px;
height: 450px;
background: radial-gradient(#1367AB 0%, #154075 60%);
}
td.firsttablerow1 {
width: 190px;
}
td.firsttablerow2 {
width: auto;
color: white;
}
tr.image1 {
margin: "15px,20px,30px,40px";
align-items: stretch;
height: 100px;
}
tr.image2 {
margin: "0px,20px,30px,40px";
align-items: stretch;
}
tr.text1 {
color: #DDE9F2;
align-items: flex-end;
font-size: 9;
}
</style>
</head>
<body>
<table align="center" class="main">
<tr>
<td class="firsttablerow1">
<table>
<tr class="image1">
<td>
<img src='C:\Jellyfish.jpg' width='200' height='90'>
</td>
</tr>
<tr class="image2">
<td>
<img src='C:\Desert.jpg' width='150' height='90'>
</td>
</tr>
<tr class="text1">
<td>"abc"</td>
</tr>
</table>
</td>
<td class="firsttablerow2">"xyz"</td>
</tr>
</table>
</body>
</html>
My C# code to convert html to word is as shown below.
MSWord.Application word = new MSWord.Application { Visible = false };
word.Documents.Open("htmlfilename", Format: WdOpenFormat.wdOpenFormatWebPages);
MSWord.Document doc = word.Documents[@"htmlfilename"];
doc.SaveAs2(@"wordFileName", WdSaveFormat.wdFormatDocument);
doc.Close();
doc = null;
word.Quit();
word = null;
Edit:-After investigating further ,I found that word is getting created .But It is unable to change background by using radial-gradient .Is there any other way to add radial background?
Upvotes: 2
Views: 18974
Reputation: 1
Install-Package HtmlToOpenXml.dll -Version 2.1.0 Install-Package DocumentFormat.OpenXml -Version 2.11.3
public async Task GenerateWordDocumentAsync(string text) {
// Create a memory stream to hold the word document data
var wordStream = new MemoryStream();
// Create a Wordprocessing document in memory
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(wordStream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
// Add a main document part to the Word file
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document(new Body());
// Convert HTML content to Word content and append to the document
HtmlConverter converter = new(mainPart);
var documentBody = mainPart.Document.Body;
var paragraphs = converter.Parse(text);
documentBody.Append(paragraphs);
// Save the Word document
wordDoc.Save();
}
// Reset the position of the stream to the beginning before returning it
wordStream.Seek(0, SeekOrigin.Begin);
return wordStream; // Return the stream
}
Upvotes: 0
Reputation: 963
Use OpenXml.
on .NET 5.0
Install-Package HtmlToOpenXml.dll -Version 2.1.0
Install-Package DocumentFormat.OpenXml -Version 2.11.3
ExportService
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using HtmlToOpenXml;
public MemoryStream ExportToWord()
{
string htmlValue = "myHtmlCodes";
using MemoryStream stream = new MemoryStream();
using WordprocessingDocument package = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document);
MainDocumentPart mainPart = package.MainDocumentPart;
if (mainPart == null)
{
mainPart = package.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
converter.ParseHtml(htmlValue);
mainPart.Document.Save();
return stream;
}
ExportController
return File(
fileContents: stream.ToArray(),
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileDownloadName: $"{fileName}.docx");
Upvotes: 9
Reputation: 8
Hope this solution helps in converting and creating word Docs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xceed.Words.NET;
using System.Diagnostics;
using System.IO;
namespace MVC_WordDocument.Controllers
{
public class GenerateDocumentController : Controller
{
// GET: GenerateDocument
public ActionResult GenerateDocument()
{
return View();
}
[HttpPost]
public void GenerateDocument(string generate)
{
DocX doc = null;
doc = DocX.Create(Server.MapPath("~/ StudentInformation.docx"), DocumentTypes.Document);
string Text = @"Student Information";
doc.InsertParagraph(Text, false).Alignment = Alignment.both;
doc.InsertParagraph(" ");
Table table = doc.AddTable(8, 7);
table.Alignment = Alignment.center;
table.Design = TableDesign.LightGridAccent2;
table.Rows[0].Cells[0].Paragraphs.First().Append("Student Number");
table.Rows[0].Cells[1].Paragraphs.First().Append("Name");
table.Rows[0].Cells[2].Paragraphs.First().Append("Surname");
table.Rows[0].Cells[3].Paragraphs.First().Append("Email");
table.Rows[0].Cells[4].Paragraphs.First().Append("Telephone No:");
table.Rows[0].Cells[5].Paragraphs.First().Append("Mobile Number");
table.Rows[0].Cells[6].Paragraphs.First().Append("Active");
doc.InsertTable(table);
doc.Save();
MemoryStream ms = new MemoryStream();
doc.SaveAs(ms);
byte[] byteArray = ms.ToArray();
ms.Flush();
ms.Close();
ms.Dispose();
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=StudentINFO.docx");
Response.AddHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/msword";
Response.BinaryWrite(byteArray);
Response.End();
}
}
}
Upvotes: -4
Reputation: 507
Can try this
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
Object oMissing = System.Reflection.Missing.Value;
wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
Object filepath = "c:\\page.html";
Object confirmconversion = System.Reflection.Missing.Value;
Object readOnly = false;
Object saveto = "c:\\doc.pdf";
Object oallowsubstitution = System.Reflection.Missing.Value;
wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object fileFormat = WdSaveFormat.wdFormatPDF;
wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
ref oMissing);
Upvotes: 1