void Export2Word()
{
//Tạo c c đối tượng application, document, table của MS Word
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc;
Microsoft.Office.Interop.Word.Table table;
//Hiện (mở) ứng dụng word
app.Visible = true;
//Tham số truyền v…o c c h…m c¢ đối l… tuỳ chọn
object obj = Type.Missing;
//Tạo một t…i liệu mới (để chứa dữ liệu xuất ra)
doc = app.Documents.Add(ref obj, ref obj, ref obj, ref obj);
Microsoft.Office.Interop.Word.Range range = doc.Range(ref obj, ref obj);
//Thˆm một bảng c¢ 17 cột v… số h…ng bằng với số h…ng trong datatable.
table = doc.Tables.Add(range, objDataTable.Rows.Count, 17, ref obj, ref obj);
//Xuất dữ liệu từ datatable sang bảng (trong word). Ch£ ý: đối với c c đối tượng tập hợp
// trong word th phần tử đầu tiˆn c¢ chỉ số l… 1 thay v 0 như trong C#
for (int i = 0; i < objDataTable.Rows.Count; i++)
{
doc.Tables[1].Rows[i + 1].Cells[1].Range.Text = objDataTable.Rows["IDlopdat"].ToString();
doc.Tables[1].Rows[i + 1].Cells[2].Range.Text = objDataTable.Rows["tenloaidat"].ToString();
doc.Tables[1].Rows[i + 1].Cells[3].Range.Text = objDataTable.Rows["h"].ToString();
doc.Tables[1].Rows[i + 1].Cells[4].Range.Text = objDataTable.Rows["C"].ToString();
doc.Tables[1].Rows[i + 1].Cells[5].Range.Text = objDataTable.Rows["γk"].ToString();
doc.Tables[1].Rows[i + 1].Cells[6].Range.Text = objDataTable.Rows["í"].ToString();
doc.Tables[1].Rows[i + 1].Cells[7].Range.Text = objDataTable.Rows["a"].ToString();
doc.Tables[1].Rows[i + 1].Cells[8].Range.Text = objDataTable.Rows["Cc"].ToString();
doc.Tables[1].Rows[i + 1].Cells[9].Range.Text = objDataTable.Rows["Cà"].ToString();
doc.Tables[1].Rows[i + 1].Cells[10].Range.Text = objDataTable.Rows["Ch"].ToString();
doc.Tables[1].Rows[i + 1].Cells[11].Range.Text = objDataTable.Rows["Cr"].ToString();
doc.Tables[1].Rows[i + 1].Cells[12].Range.Text = objDataTable.Rows["e"].ToString();
doc.Tables[1].Rows[i + 1].Cells[13].Range.Text = objDataTable.Rows["Es"].ToString();
doc.Tables[1].Rows[i + 1].Cells[14].Range.Text = objDataTable.Rows["k"].ToString();
doc.Tables[1].Rows[i + 1].Cells[15].Range.Text = objDataTable.Rows["ν"].ToString();
doc.Tables[1].Rows[i + 1].Cells[16].Range.Text = objDataTable.Rows["W"].ToString();
doc.Tables[1].Rows[i + 1].Cells[17].Range.Text = objDataTable.Rows["Cv"].ToString();
}
//Thˆm đường viền cho Table nếu cần.
doc.Select();
Microsoft.Office.Interop.Word.WdLineStyle BorderValue = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderVertical].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineStyle = BorderValue;
}
private void button2_Click(object sender, EventArgs e)
{
Export2Word();
}