原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral
最近项目需要用到导出word,巨硬的office组件,大多数人都不喜欢用(我就其中之一)。所有在网上尝试了几个插件。能够导出word的,除了NPOI之外,还有docX(免费),pageoffice(收费),aspose(也是收费的,虽然我有license,但是公司的另一个项目组用了破解版的,导致我也不敢用。毕竟license也是网上down下来的。)收费的,我想大家伙儿和我一样都不会去考虑,所有我只是尝试了一下docX,docX的代码量相对于NPOI来说少很多,但是这货中文的资料太少了(鄙视一下CSDN,妈蛋,是个用户写个代码就往上传,还他丫的下载一个10个积分……),捣腾了半天,最终能够生成word,但是要是往里面加图片,确实不知道怎弄。(如果你有幸看了这篇文字,并且还会,还望不吝)。
NPOI这个东西好是好,但是有个问题,如果不说,可能很多人都不会注意。它的版本总是让人感到错愕。一个不一样的版本,写法就会产生很多变化,下面代码中,注释掉的部分,就是因为引用的版本发生变化,改写了部分代码。
protected void Button1_Click(object sender, EventArgs e) { XWPFDocument doc = new XWPFDocument(); //创建新的word文档 XWPFParagraph p0 = doc.CreateParagraph(); //向新文档中添加段落 p0.Alignment = ParagraphAlignment.LEFT; //p0.SetAlignment(ParagraphAlignment.LEFT); //段落对其方式为居中 XWPFRun r0 = p0.CreateRun(); r0.FontFamily = "宋体"; //r0.SetFontFamily("宋体"); r0.FontSize = 20; //r0.SetFontSize(20); r0.SetBold(true); r0.SetText("未登录过学生的账号密码");//向该段落中添加文字 XWPFParagraph p1 = doc.CreateParagraph(); p1.Alignment = ParagraphAlignment.LEFT; //p1.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r1 = p1.CreateRun(); r1.FontFamily = "宋体"; //r1.SetFontFamily("宋体"); r1.FontSize = 15; //r1.SetFontSize(15); r1.SetBold(true); r1.SetText("(备注:已登录过的学生密码不显示)"); XWPFParagraph p2 = doc.CreateParagraph(); p2.Alignment = ParagraphAlignment.LEFT; //p2.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r2 = p2.CreateRun(); //r2.SetFontFamily("宋体"); r2.FontFamily = "宋体"; //r2.SetFontSize(10); r2.FontSize = 10; r2.SetBold(true); r2.SetText("学校:XX一中"); XWPFParagraph p3 = doc.CreateParagraph(); p3.Alignment = ParagraphAlignment.LEFT; //p3.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r3 = p2.CreateRun(); //r3.SetFontFamily("宋体"); r3.FontFamily = "宋体"; //r3.SetFontSize(10); r3.FontSize = 10; r3.SetBold(true); r3.SetText("班级:(7)"); XWPFParagraph p4 = doc.CreateParagraph(); p4.Alignment = ParagraphAlignment.LEFT; //p4.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r4 = p4.CreateRun(); r4.FontFamily = "宋体"; //r4.SetFontFamily("宋体"); r4.FontSize = 10; //r4.SetFontSize(10); r4.SetBold(true); r4.SetText("班主任:ddd"); XWPFParagraph p5 = doc.CreateParagraph(); p5.Alignment = ParagraphAlignment.LEFT; //p5.SetAlignment(ParagraphAlignment.LEFT); XWPFRun r5 = p5.CreateRun(); r5.FontFamily = "宋体"; //r5.SetFontFamily("宋体"); r5.FontSize = 10; //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.FontFamily = "微软雅黑"; //rIO.SetFontFamily("微软雅黑"); rIO.FontSize = 10; //rIO.SetFontSize(12); rIO.SetBold(true); rIO.SetText("表格第一行第一列"); XWPFParagraph pINo = table.GetRow(0).GetCell(1).AddParagraph(); XWPFRun rINo = pINo.CreateRun(); //rINo.SetFontFamily("微软雅黑"); rINo.FontFamily = "微软雅黑"; //rINo.SetFontSize(12); rINo.FontSize = 10; rINo.SetBold(true); rINo.SetText("表格第一行第二列"); XWPFParagraph pIMm = table.GetRow(0).GetCell(2).AddParagraph(); XWPFRun rIMm = pIMm.CreateRun(); //rIMm.SetFontFamily("微软雅黑"); rIMm.FontFamily = "微软雅黑"; //rIMm.SetFontSize(12); rIMm.FontSize = 10; rIMm.SetBold(true); rIMm.SetText("表格第一行第三列"); XWPFParagraph pIName = table.GetRow(0).GetCell(3).AddParagraph(); XWPFRun rIName = pIName.CreateRun(); //rIName.SetFontFamily("微软雅黑"); rIName.FontFamily = "微软雅黑"; //rIName.SetFontSize(12); rIName.FontSize = 10; rIName.SetBold(true); rIName.SetText("表格第一行第4列");
/*这段代码是插入图片的*/ XWPFParagraph gp = doc.CreateParagraph(); XWPFRun gr = gp.CreateRun(); var widthEmus = (int)(400.0 * 9525); var heightEmus = (int)(300.0 * 9525); using (FileStream picData = new FileStream("f:\\1.jpg", FileMode.Open, FileAccess.Read)) { gr.AddPicture(picData, (int)PictureType.PNG, "11.png", widthEmus, heightEmus); }
/*这段代码是插入图片的*/
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文件 }