1. 程式人生 > >NPOI匯出word,以及對table的一些設定

NPOI匯出word,以及對table的一些設定

最終效果圖:

   

程式碼:

複製程式碼

  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         }