Simple Solution to import data information into wrod documentation (C # for word)

Source: Internet
Author: User

Copy codeThe Code is as follows:
/// <Summary>
/// Set the Word template. The word table style is set here.
/// </Summary>
/// <Param name = "dsTr"> </param>
/// <Returns> </returns>
Protected string SetWordTemplate (string dsTr)
{
StringBuilder html = new StringBuilder ();
Html. Append ("<! DOCTYPE html PUBLIC '-// W3C // dtd xhtml 1.0 Transitional // en' 'HTTP: // www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> ");
Html. Append ("Html. Append ("Html. Append ("<body> ");
// Html. Append ("<table cellpadding = '0' cellspacing = '0' border = '1'> ");
Html. Append (dsTr );
// Html. Append ("</table> ");
Html. Append ("</body> ");
Html. Append ("Return html. ToString ();
}
/// <Summary>
/// Save the Word
/// </Summary>
/// <Param name = "savePath"> </param>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Protected bool SaveWord (string savePath, string data)
{
Try
{
Using (StreamWriter sw = new StreamWriter (savePath, true, Encoding. Default ))
{
Sw. WriteLine (data );
Sw. Close ();
Sw. Dispose ();
}
Return true;
}
Catch
{
Return false;
}
Return false;
}
/// <Summary>
/// Import
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, EventArgs e)
{
// Save path
String path = string. Format ("{0} {1}", Application. StartupPath, "save physical path ");
// Replace the dataset in multiple tables with the dataset queried from the database.
Foreach (DataTable dt in new DataSet (). Tables)
{
StringBuilder _ tables = new StringBuilder ();
For (int I = 0; I <dt. Rows. Count; I ++)
{
If (I = 0)
{
_ Tables. Append ("<table cellpadding = '0' cellspacing = '0' border = '1'> ");
_ Tables. Append ("<tr> <td> table name </td> </tr> ");
}
_ Tables. append ("<tr> <td>" + I + "</td> <td>" + I + "</td> <td>" + I + "</ td> </tr> ");
If (I = (dt. Rows. Count-1 ))
_ Tables. Append ("</table> ");
}
SaveWord (path, this. SetWordTemplate (_ tables. ToString ()));
}
MessageBox. Show ("generated successfully! ");
}

Hey! Success! This method is quick, but other paging functions of word cannot be set. However, it is more than enough to import all tables from the database to wrod !~
I am a little bird. If you have any large bird or large bird, please kindly advise on the Code with better solutions !~ Thanks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.