标签:
参考网址:http://www.aiuxian.com/article/p-1970779.html
NPOI版本:2.1.3.1
最终效果图:
代码:
1 /// <summary> 2 /// 导出word培训班自然情况表 3 /// </summary> 4 [HttpPost] 5 public void PrintWord() 6 { 7 int id = Int32.Parse(Request.Form["id"]); 8 var query = from n in db.Nature 9 where n.NatureId == id && n.IsDelete == false 10 select new NatureInfo 11 { 12 NatureBeginTime = n.NatureBeginTime, 13 NatureEndTime = n.NatureEndTime, 14 ActualNum = n.ActualNum, 15 PlanNum = n.PlanNum, 16 NaturePlace = n.NaturePlace, 17 NatureMajor = n.NatureMajor, 18 TrainName = n.TrainName, 19 TotalHours = n.TotalHours, 20 TeachingHours = n.TeachingHours, 21 Remark = n.Remark 22 }; 23 DataTable dt = query.ToDataTable(rec => new object[] { query }); 24 #region 培训班自然情况表 25 XWPFDocument doc = new XWPFDocument();//创建word文档 26 MemoryStream ms = new MemoryStream();//创建流 27 28 XWPFParagraph p0 = doc.CreateParagraph();//创建段落 29 p0.Alignment = ParagraphAlignment.CENTER;//居中显示 30 XWPFRun r0 = p0.CreateRun(); 31 //设置字体 32 r0.FontFamily = "宋体"; 33 //设置字体大小 34 r0.FontSize = 18; 35 //字体是否加粗,这里加粗了 36 r0.SetBold(true); 37 r0.SetText("培训班自然情况表");//写入文本 38 39 XWPFParagraph p1 = doc.CreateParagraph(); 40 p1.Alignment = ParagraphAlignment.LEFT; 41 XWPFRun r1 = p1.CreateRun(); 42 r1.SetText("培训机构:石家庄职工培训基地"); 43 44 XWPFTable table = doc.CreateTable(1, 4);//创建table 45 table.RemoveRow(0);//去掉第一行空白的 46 table.SetColumnWidth(0, 6 * 256);//设置列的宽度 47 table.SetColumnWidth(1, 10 * 256); 48 table.SetColumnWidth(2, 6 * 256); 49 table.SetColumnWidth(3, 10 * 256); 50 CT_Tbl m_CTTbl = doc.Document.body.GetTblArray()[0];//获得文档第一张表 51 m_CTTbl.AddNewTblPr().jc = new CT_Jc(); 52 m_CTTbl.AddNewTblPr().jc.val = ST_Jc.center;//表在页面水平居中 53 54 foreach (DataRow r in dt.Rows) 55 { 56 CT_Row nr = new CT_Row(); 57 XWPFTableRow mr = new XWPFTableRow(nr, table);//创建行 58 mr.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高 59 nr.AddNewTrPr().AddNewTrHeight().val = (ulong)1000;//设置行高(这两行都得有) 60 table.AddRow(mr);//将行添加到table中 61 62 mr.CreateCell().SetText("培训班名称"); 63 mr.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);//文字垂直居中(即上下居中) 64 XWPFTableCell c1 = mr.CreateCell();//创建单元格 65 CT_Tc ct = c1.GetCTTc(); 66 CT_TcPr cp = ct.AddNewTcPr(); 67 68 69 cp.gridSpan = new CT_DecimalNumber(); 70 cp.gridSpan.val = "3"; //合并3列 71 cp.AddNewVAlign().val = ST_VerticalJc.center; 72 ct.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//单元格内容居中显示 73 ct.GetPList()[0].AddNewR().AddNewT().Value = r["TrainName"].ToString(); 74 75 CT_Row cr1 = new CT_Row(); 76 XWPFTableRow tr1 = new XWPFTableRow(cr1, table); 77 tr1.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 78 cr1.AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 79 table.AddRow(tr1); 80 tr1.CreateCell().SetText("培训地点"); 81 tr1.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 82 tr1.CreateCell().SetText(r["NaturePlace"].ToString()); 83 tr1.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 84 tr1.CreateCell().SetText("培训时间"); 85 tr1.GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 86 tr1.CreateCell().SetText(Convert.ToDateTime(r["NatureBeginTime"].ToString()).ToShortDateString() + "~" + Convert.ToDateTime(r["NatureEndTime"].ToString()).ToShortDateString()); 87 tr1.GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 88 89 CT_Row cr2 = new CT_Row(); 90 XWPFTableRow tr2 = new XWPFTableRow(cr2, table); 91 tr2.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 92 cr2.AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 93 table.AddRow(tr2); 94 tr2.CreateCell().SetText("计划人数"); 95 tr2.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 96 tr2.CreateCell().SetText(r["PlanNum"].ToString()); 97 tr2.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 98 tr2.CreateCell().SetText("实际人数"); 99 tr2.GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 100 tr2.CreateCell().SetText(r["ActualNum"].ToString()); 101 tr2.GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 102 103 CT_Row cr3 = new CT_Row(); 104 XWPFTableRow tr3 = new XWPFTableRow(cr3, table);//创建行 105 tr3.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 106 cr3.AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 107 table.AddRow(tr3);//将行添加到table中 108 tr3.CreateCell().SetText("总学时"); 109 tr3.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 110 XWPFTableCell tc3 = tr3.CreateCell();//创建单元格 111 CT_Tc ct3 = tc3.GetCTTc(); 112 CT_TcPr cp3 = ct3.AddNewTcPr(); 113 cp3.gridSpan = new CT_DecimalNumber(); 114 cp3.gridSpan.val = "3"; //合并3列 115 ct3.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.left;//单元格内容居中显示 116 ct3.GetPList()[0].AddNewR().AddNewT().Value = r["TotalHours"].ToString(); 117 tr3.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 118 119 CT_Row cr4 = new CT_Row(); 120 XWPFTableRow tr4 = new XWPFTableRow(cr4, table);//创建行 121 tr4.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)3000; 122 123 cr4.AddNewTrPr().AddNewTrHeight().val = (ulong)3000; 124 table.AddRow(tr4);//将行添加到table中 125 tr4.CreateCell().SetText("课程"); 126 tr4.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 127 XWPFTableCell tc4 = tr4.CreateCell();//创建单元格 128 CT_Tc ct4 = tc4.GetCTTc(); 129 CT_TcPr cp4 = ct4.AddNewTcPr(); 130 cp4.gridSpan = new CT_DecimalNumber(); 131 cp4.gridSpan.val = "3"; //合并3列 132 ct4.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.left;//单元格内容居中显示 133 ct4.GetPList()[0].AddNewR().AddNewT().Value = r["NatureMajor"].ToString(); 134 tr4.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 135 136 CT_Row cr5 = new CT_Row(); 137 XWPFTableRow tr5 = new XWPFTableRow(cr5, table);//创建行 138 tr5.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 139 cr5.AddNewTrPr().AddNewTrHeight().val = (ulong)1000; 140 table.AddRow(tr5);//将行添加到table中 141 tr5.CreateCell().SetText("授课学时"); 142 tr5.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 143 XWPFTableCell tc5 = tr5.CreateCell();//创建单元格 144 CT_Tc ct5 = tc5.GetCTTc(); 145 CT_TcPr cp5 = ct5.AddNewTcPr(); 146 cp5.gridSpan = new CT_DecimalNumber(); 147 cp5.gridSpan.val = "3"; //合并3列 148 ct5.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.left;//单元格内容居中显示 149 ct5.GetPList()[0].AddNewR().AddNewT().Value = r["TeachingHours"].ToString(); 150 tr5.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 151 152 CT_Row cr6 = new CT_Row(); 153 XWPFTableRow tr6 = new XWPFTableRow(cr6, table);//创建行 154 tr6.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)2000; 155 cr6.AddNewTrPr().AddNewTrHeight().val = (ulong)2000; 156 table.AddRow(tr6);//将行添加到table中 157 tr6.CreateCell().SetText("备注"); 158 tr6.GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 159 XWPFTableCell tc6 = tr6.CreateCell();//创建单元格 160 CT_Tc ct6 = tc6.GetCTTc(); 161 CT_TcPr cp6 = ct6.AddNewTcPr(); 162 cp6.gridSpan = new CT_DecimalNumber(); 163 cp6.gridSpan.val = "3"; //合并3列 164 ct6.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.left;//单元格内容居中显示 165 ct6.GetPList()[0].AddNewR().AddNewT().Value = r["Remark"].ToString(); 166 tr6.GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); 167 } 168 #endregion 169 170 // 写入到客户端 171 doc.Write(ms); 172 // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 173 Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", "培训班自然情况表" + System.DateTime.Now.ToString("yyyymmddhhmmssfff "))); 174 Response.Charset = "UTF-8"; 175 Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); 176 // 指定返回的是一个不能被客户端读取的流,必须被下载 177 Response.ContentType = "application/ms-word"; 178 // 把文件流发送到客户端 179 Response.BinaryWrite(ms.ToArray()); 180 doc = null; 181 ms.Close(); 182 ms.Dispose(); 183 }
标签:
原文地址:http://www.cnblogs.com/wsn1203/p/5409203.html