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