public void DisplayTable(DataTable dt)
{
if (dt.Rows.Count < 1)
{
return;
}
//插入表格的位置
Object start = this.Characters.Count-1;
Object end = this.Characters.Count;
Word.Range rng = this.Range(ref start, ref end);
//表格對象
rng.InsertBefore(Environment.NewLine);
rng.Select();
Word.Table tb = this.Tables.Add(rng, dt.Rows.Count + 1, dt.Columns.Count,ref missing,ref missing );
//設置表格寬度
tb.Columns.SetWidth(50, Word.WdRulerStyle.wdAdjustProportional);
//輸出列表題
for (int i = 0; i < dt.Columns.Count; i++)
{
tb.Cell(1, i + 1).Range.InsertAfter(dt.Columns[i].Caption);
}
//輸出內容
for(int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
tb.Cell(i + 2, j + 1).Range.InsertAfter(dt.Rows[i][j].ToString());
}
}
private void buttonReadDatabase_Click(object sender, EventArgs e)
{
try
{
//OleDbConnection oleConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=2006年圖書銷售情況.xls;Extended PropertIEs=Excel 8.0");
OleDbConnection oleConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=2006年圖書銷售情況.xls;Extended PropertIEs=Excel 8.0");
OleDbDataAdapter oleDa = new OleDbDataAdapter("select * from [BookSell$]", oleConn);
DataTable dt = new DataTable();
oleDa.Fill(dt);
Globals.ThisDocument.DisplayTable(dt);
}
catch
{
;
}
}
需要注意的
1.Old數據庫的連接字符串
2.Word中的表是從1行1列開始的.而DataTable是從0,0開始的
需要全部源代碼的和我聯系,等我做完了我會把他上傳到我的資源,歡迎下載
QQ:723838570
email: [email protected]