比較簡單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
更新時間:2010年01月15日 17:17:26 作者:
史上最簡單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
復(fù)制代碼 代碼如下:
/// <summary>
/// 設(shè)置Word模板,word表格樣式在此設(shè)置
/// </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 xmlns='http://www.w3.org/1999/xhtml'>");
html.Append("<html><head><title>測試word</title></head>");
html.Append("<body>");
//html.Append("<table cellpadding='0' cellspacing='0' border='1'>");
html.Append(dsTr);
//html.Append("</table>");
html.Append("</body>");
html.Append("</html>");
return html.ToString();
}
/// <summary>
/// 保存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>
/// 導(dǎo)入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//保存路徑
string path = string.Format("{0}{1}", Application.StartupPath, "保存物理路徑");
//多個表 這里的dataset需要換成從數(shù)據(jù)庫查詢出來的dataset
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>表名字</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("生成成功!");
}
嘿嘿!大功告成啦!這種方法快捷,但是不能設(shè)置word其他的分頁功能等等,不過應(yīng)對于從數(shù)據(jù)庫中把所有表導(dǎo)入到wrod是綽綽有余啦!~
俺乃小小小小鳥兒,如過哪位大大大大鳥有更好的方案好的Code請多多指教哦!~Thanks
您可能感興趣的文章:
相關(guān)文章
asp.net實現(xiàn)微信公眾賬號接口開發(fā)教程
這篇文章主要是一篇關(guān)于asp.net實現(xiàn)微信公眾賬號接口開發(fā)教程,感興趣的小伙伴們可以參考一下2015-12-12WCF如何綁定netTcpBinding寄宿到控制臺應(yīng)用程序詳解
這篇文章主要給大家介紹了關(guān)于WCF如何綁定netTcpBinding寄宿到控制臺應(yīng)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用WCF具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07asp.net core2.2多用戶驗證與授權(quán)示例詳解
這篇文章主要給大家介紹了關(guān)于asp.net core2.2多用戶驗證與授權(quán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Linux服務(wù)器下利用Docker部署.net Core項目的全過程
這篇文章主要給大家介紹了關(guān)于在Linux服務(wù)器下利用Docker部署.net Core項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用.net Core具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07C#數(shù)據(jù)綁定控件中的DataSource屬性淺談
使用該屬性指定用來填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對象, 如用于訪問數(shù)據(jù)庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對象2013-02-02