private void ExportWordTable()
{
List <SINHVIENEntities> lstSINHVIEN = dapSINHVIEN.GetAll();
if (lstSINHVIEN != null)
{
//Khai báo đường dẫn của file sau khi lưu
string urlFileSave = "/AppFile/tmp/ListSV.docx";
urlFileSave = Server.MapPath(urlFileSave);
//Khai báo đường dẫn của file doc được đọc
string fileName = Server.MapPath("/AppFile/Docs/ExportWordTable.docx");
using (DocX doc = DocX.Load(fileName))
{
Novacode.Table tblSINHVIEN = doc.Tables[1];
Novacode.Row rowCn = tblSINHVIEN.Rows[1];
int count = 2;
Novacode.Row newRow;
for (int i = 0; i < lstSINHVIEN.Count; i++)
{
newRow = tblSINHVIEN.InsertRow(rowCn, count++);
newRow.Cells[0].ReplaceText("@stt", (i + 1).ToString());
newRow.Cells[1].ReplaceText("@hovaten", lstSINHVIEN[i].HOVATEN == null ? "" : lstSINHVIEN[i].HOVATEN);
newRow.Cells[2].ReplaceText("@ngaysinh", lstSINHVIEN[i].NGAYSINH == null ? "" : lstSINHVIEN[i].NGAYSINH.ToString("dd/MM/yyyy"));
newRow.Cells[3].ReplaceText("@gioitinh", lstSINHVIEN[i].GIOITINH == 1 ? "Nam" : "Nữ");
newRow.Cells[4].ReplaceText("@diachi", lstSINHVIEN[i].DIACHI == null ? "" : lstSINHVIEN[i].DIACHI);
newRow.Cells[5].ReplaceText("@sdt", lstSINHVIEN[i].SDT == null ? "" : lstSINHVIEN[i].SDT);
newRow.Cells[6].ReplaceText("@email", lstSINHVIEN[i].EMAIL == null ? "" : lstSINHVIEN[i].EMAIL);
newRow.Cells[7].ReplaceText("@lop", lstSINHVIEN[i].TENLOP);
}
tblSINHVIEN.RemoveRow(1);
doc.SaveAs(urlFileSave);
doc.Save();
Response.Buffer = true;
Response.Expires = 0;
Response.Clear();
string strHttpContext_ContentType = "application/msword";
HttpContext.Current.Response.ContentType = strHttpContext_ContentType;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=BangThongTinSinhVien" + ".docx");
Response.TransmitFile(urlFileSave);
Response.Flush();
Response.Close();
Response.End();
}
}
}