public static void outputDOCXX(DataSet ds, string pclassno, string term, string mg, string sess, string session, string pdate, string filter, TextWriter sw, bool namemask = false) { string temple_file = AppPath + @"\xmldata\NPOI_T1516.docx"; if (pclassno.ToUpper().StartsWith('P')) { temple_file = AppPath + @"\xmldata\NPOI_PT1920.docx"; } if (pclassno.EndsWith("E")) { temple_file = mg.Equals("m") ? AppPath + @"\xmldata" + @"\NPOI_VOCAMT1516.docx" : AppPath + @"\xmldata" + @"\NPOI_VOCAGT1516.docx"; } string out_filename = AppPath + @"\xmlout" + @"\MarkTable_" + pclassno + "_" + mg + ".docx"; Stream fs = File.OpenRead(temple_file); XWPFDocument doc = new XWPFDocument(fs); mklib.EsStudReport esrp = null; if (pclassno.StartsWith("P")) { esrp = new mklib.PEsStudReportFillCDS(); } else if (pclassno.EndsWith("E")) { esrp = new mklib.VEsStudReportFillCDS(); } else { esrp = new mklib.SEsStudReportFillCDS(); } if (filter == null) { filter = ""; } string pattern = @"\d+"; Regex rx = new Regex(pattern); MatchCollection matches = rx.Matches(filter); DataTable tb = ds.Tables["Table"]; int page_no = 0; for (int i = 0; i < tb.Rows.Count; i++) { string seat = tb.Rows[i]["curr_seat"].ToString(); string classno = tb.Rows[i]["curr_class"].ToString(); string dsej_ref = tb.Rows[i]["dsej_ref"].ToString(); string curr_class = tb.Rows[i]["curr_class"].ToString(); string classname = mklib.EsStudReport.class2n(classno); string stud_ref = tb.Rows[i]["stud_ref"].ToString(); if (matches.Count > 0) { int crt = 0; for (crt = 0; crt < matches.Count; crt++) { if (matches[crt].Value.Equals(seat)) { break; } } if (crt >= matches.Count) { continue; } } String marktxt = esrp.MrkTxt(term, mg, tb.Rows[i].GetChildRows("sr_mk"), tb.Rows[i].GetChildRows("sr_cd"), tb.Rows[i].GetChildRows("sr_ac"), tb.Rows[i].GetChildRows("sr_gc") ); int j = page_no * 4; page_no++; if (j >= doc.Tables.Count) { Console.WriteLine("ERROR doc tables are not enought!"); break; } var _tbl = doc.Tables[j]; string photo_path = @"c:\photo" + sess + @"\" + dsej_ref + ".jpg"; string qrbc_path = @"c:\photo" + sess + @"\qrcode\" + sess + curr_class + (seat.Length == 1 ? "0" : "") + seat + dsej_ref + ".jpg"; if (!File.Exists(photo_path)) { photo_path = @"c:/photo1920/none.jpg"; } if (!File.Exists(qrbc_path)) { qrbc_path = @"c:/photo1920/qrcode/none.jpg"; } FileStream gfs = null; { _tbl.Rows[0].GetCell(1).Paragraphs[0].ReplaceText("#PLphoto|", ""); gfs = new FileStream(photo_path, FileMode.Open, FileAccess.Read); XWPFRun gr = _tbl.Rows[0].GetCell(1).Paragraphs[0].CreateRun(); gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg", 1120000, 1380000); gfs.Close(); } { doc.Tables[j + 3].Rows[0].GetCell(3).Paragraphs[0].ReplaceText("#PLqrbc|", ""); gfs = new FileStream(qrbc_path, FileMode.Open, FileAccess.Read); XWPFRun gr = doc.Tables[j + 3].Rows[0].GetCell(3).Paragraphs[0].CreateRun(); gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg", 1480000, 1480000); gfs.Close(); } _tbl.Rows[1].GetCell(1).Paragraphs[0].ReplaceText("#TLsession|", session); _tbl.Rows[1].GetCell(3).Paragraphs[0].ReplaceText("#TLp_date|", pdate); _tbl.Rows[1].GetCell(5).Paragraphs[0].ReplaceText("#TLdsej_ref|", tb.Rows[i]["dsej_ref"].ToString()); _tbl.Rows[2].GetCell(1).Paragraphs[0].ReplaceText("#TLC_NAME|", tb.Rows[i]["C_NAME"].ToString()); _tbl.Rows[2].GetCell(1).Paragraphs[0].ReplaceText("#TLE_NAME|", tb.Rows[i]["E_NAME"].ToString()); _tbl.Rows[3].GetCell(1).Paragraphs[0].ReplaceText("#TLclassname|", mklib.EsStudReport.class2n(pclassno)); _tbl.Rows[3].GetCell(3).Paragraphs[0].ReplaceText("#TLseat|", seat); _tbl.Rows[3].GetCell(5).Paragraphs[0].ReplaceText("#TLstud_ref|", tb.Rows[i]["stud_ref"].ToString()); //#LSMarkdetai var _tblm = doc.Tables[j + 1]; Cell_NOPI_memo(marktxt, "#LSMarkdetail|", _tblm.Rows[2].GetCell(0)); var _tblp = doc.Tables[j + 2]; foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_cd")) { string conduct = mklib.EsStudReport.GetCDFMT(cRow, pclassno, "1"); string conduct2 = mklib.EsStudReport.GetCDFMT(cRow, pclassno, "2"); string conduct3 = mklib.EsStudReport.GetCDFMT(cRow, pclassno, "3"); string schooleval1 = cRow["SchoolEval1"].ToString(); string schooleval2 = cRow["SchoolEval2"].ToString(); string schooleval3 = cRow["SchoolEval3"].ToString(); Cell_NOPI_memo(conduct, "#LQconduct|", _tblp.Rows[1].GetCell(1)); Cell_NOPI_memo(conduct2, "#LQconduct2|", _tblp.Rows[2].GetCell(1)); Cell_NOPI_memo(conduct3, "#LQconduct3|", _tblp.Rows[3].GetCell(1)); Cell_NOPI_memo(schooleval1, "#LQschooleval1|", _tblp.Rows[1].GetCell(2)); Cell_NOPI_memo(schooleval2, "#LQschooleval2|", _tblp.Rows[2].GetCell(2)); Cell_NOPI_memo(schooleval3, "#LQschooleval3|", _tblp.Rows[3].GetCell(2)); } if (pclassno.ToUpper().StartsWith('S')) { foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_ac")) { _tblp.Rows[4].GetCell(1).Paragraphs[0].ReplaceText("#TLact_py|", cRow["act_py"].ToString()); } } foreach (DataRow cRow in tb.Rows[i].GetChildRows("sr_py")) { _tblp.Rows[1].GetCell(3).Paragraphs[0].ReplaceText("#TLpingyu1|", cRow["py1"].ToString()); _tblp.Rows[2].GetCell(3).Paragraphs[0].ReplaceText("#TLpingyu2|", cRow["py2"].ToString()); _tblp.Rows[3].GetCell(3).Paragraphs[0].ReplaceText("#TLpingyu3|", cRow["py3"].ToString()); } } for (int i = doc.Tables.Count - 1; i >= page_no * 4; i--) { var _t = doc.Tables[i]; doc.RemoveBodyElement(doc.GetPosOfTable(_t)); } using (FileStream _sw = File.Create(out_filename)) { doc.Write(_sw); } sw.Write(out_filename); }