Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Thu Mar 31, 2016 7:31 am
I have downloaded Spire.Doc and I am trying to create a word document on user button click but I don't understand how to acheive this as I can find all solution example which shows opening an existing word document and then writing onto that. But in my case I need to create the word after reading the table from ASPX page and then export into word. Please suggest me on this. Thanks
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Thu Mar 31, 2016 7:52 am
Hi,
Thanks for your posting.
Please try the following solution.
- Code: Select all
Document document = new Document();
using (MemoryStream buffer = new MemoryStream())
{
using (TextWriter writer = new StreamWriter(buffer, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
this.RenderControl(htmlWriter);
htmlWriter.Flush();
writer.Flush();
buffer.Position = 0;
string stylesheet = "Default.css";
using (StreamReader reader = new System.IO.StreamReader(buffer, Encoding.UTF8))
{
String baseHref = Request.Url.AbsoluteUri;
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
document.HtmlExportOptions.CssStyleSheetType = CssStyleSheetType.External;
document.HtmlExportOptions.CssStyleSheetFileName = stylesheet;
document.SaveToFile("Sample.docx", FileFormat.Docx, Response, HttpContentType.Attachment);
}
}
}
}
Best Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am
Thu Mar 31, 2016 8:14 am
Thanks for your reply. But I am getting this error : "Index was out of range. Must be non-negative and less than the size of the collection." into this line:
- Code: Select all
document.LoadHTML(reader, baseHref, Spire.Doc.Documents.XHTMLValidationType.None);
Please suggest me what shall I do in this situation.
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Thu Mar 31, 2016 8:21 am
Hi,
Please share your aspx page to help us reproduce your issue. You also can send it to
amy.zhao@e-iceblue.com.
Thank you.
Best Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am
Thu Mar 31, 2016 8:29 am
Thank you so much for your early response. Actually I have found one issue in my aspx. There was a empty table when I have removed that part now it is working fine. Here is the line that I have removed:
- Code: Select all
<table id="tblHR" runat="server" ></table>
I also need to print this page with header and footer so, will you please suggest me how do I include Header and Footer? Thanks.
-
barsan101
-
- Posts: 7
- Joined: Thu Mar 31, 2016 7:26 am
Thu Mar 31, 2016 8:47 am
Hi,
Thanks for your feedback.
For adding footer and header issue, I recommend you to add header and footer using Spire.Doc HeadersFooters function following the below tutorial.
http://www.e-iceblue.com/Tutorials/Spir ... ument.htmlBest Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am
Fri Apr 01, 2016 2:18 am
Hi,
Sorry, the valid url has been updated. Please try again.
Best Regards,
Amy
E-iceblue support team
-
amy.zhao
-
- Posts: 2766
- Joined: Wed Jun 27, 2012 8:50 am