原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral
XWPFDocument doc = new XWPFDocument(); //创建新的word文档 XWPFParagraph p0 = doc.CreateParagraph(); //向新文档中添加段落 p0.SetAlignment(ParagraphAlignment.LEFT); //段落对其方式为居中 XWPFRun r0 = p0.CreateRun(); r0.SetFontFamily("宋体"); r0.SetFontSize(20); r0.SetBold(true); r0.SetText("未登录过学生的账号密码");//向该段落中添加文字 XWPFParagraph p1 = doc.CreateParagraph(); p1.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r1 = p1.CreateRun(); r1.SetFontFamily("宋体"); r1.SetFontSize(15); r1.SetBold(true); r1.SetText("(备注:已登录过的学生密码不显示)"); XWPFParagraph p2 = doc.CreateParagraph(); p2.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r2 = p2.CreateRun(); r2.SetFontFamily("宋体"); r2.SetFontSize(10); r2.SetBold(true); r2.SetText("学校:XX一中"); XWPFParagraph p3 = doc.CreateParagraph(); p3.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r3 = p2.CreateRun(); r3.SetFontFamily("宋体"); r3.SetFontSize(10); r3.SetBold(true); r3.SetText("班级:(7)"); XWPFParagraph p4 = doc.CreateParagraph(); p4.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r4 = p4.CreateRun(); r4.SetFontFamily("宋体"); r4.SetFontSize(10); r4.SetBold(true); r4.SetText("班主任:ddd"); XWPFParagraph p5 = doc.CreateParagraph(); p5.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r5 = p5.CreateRun(); r5.SetFontFamily("宋体"); r5.SetFontSize(10); r5.SetBold(true); r5.SetText("可以在此处添加备注:"); XWPFTable table = doc.CreateTable(5, 5); table.Width = 1600;//设置表格宽度 XWPFParagraph pIO = table.GetRow(0).GetCell(0).AddParagraph(); XWPFRun rIO = pIO.CreateRun(); rIO.SetFontFamily("微软雅黑"); rIO.SetFontSize(12); rIO.SetBold(true); rIO.SetText("表格第一行第一列"); XWPFParagraph pINo = table.GetRow(0).GetCell(1).AddParagraph(); XWPFRun rINo = pINo.CreateRun(); rINo.SetFontFamily("微软雅黑"); rINo.SetFontSize(12); rINo.SetBold(true); rINo.SetText("表格第一行第二列"); XWPFParagraph pIMm = table.GetRow(0).GetCell(2).AddParagraph(); XWPFRun rIMm = pIMm.CreateRun(); rIMm.SetFontFamily("微软雅黑"); rIMm.SetFontSize(12); rIMm.SetBold(true); rIMm.SetText("表格第一行第三列"); XWPFParagraph pIName = table.GetRow(0).GetCell(3).AddParagraph(); XWPFRun rIName = pIName.CreateRun(); rIName.SetFontFamily("微软雅黑"); rIName.SetFontSize(12); rIName.SetBold(true); rIName.SetText("表格第一行第4列"); FileStream sw = File.Create("cutput.docx"); //... doc.Write(sw); //... sw.Close(); //在服务端生成文件 FileInfo file = new FileInfo("cutput.docx");//文件保存路径及名称 //注意: 文件保存的父文件夹需添加Everyone用户,并给予其完全控制权限 Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("1.docx", System.Text.Encoding.UTF8));//导出的word文档的名字 Response.AppendHeader("Content-Length", file.Length.ToString()); Response.WriteFile(file.FullName); Response.Flush(); //以上将生成的word文件发送至用户浏览器 File.Delete("cutput.docx"); //清除服务端生成的word文件 }
时间: 2024-10-10 08:25:12