public void Test54099() { XWPFDocument doc = new XWPFDocument(); CT_Tbl ctTable = new CT_Tbl(); XWPFTable table = new XWPFTable(ctTable, doc); XWPFTableRow tr = table.GetRow(0); XWPFTableCell cell = tr.GetCell(0); CT_Tc ctTc = cell.GetCTTc(); CT_TcPr tcPr = ctTc.AddNewTcPr(); CT_HMerge hMerge = tcPr.AddNewHMerge(); hMerge.val = (ST_Merge.restart); CT_TcBorders tblBorders = tcPr.AddNewTcBorders(); CT_VMerge vMerge = tcPr.AddNewVMerge(); }
public void SetBorderBottom(XWPFTable.XWPFBorderType type, int size, int space, String rgbColor) { CT_TcPr ctTcPr = null; if (!GetCTTc().IsSetTcPr()) { ctTcPr = GetCTTc().AddNewTcPr(); } CT_TcBorders borders = ctTcPr.AddNewTcBorders(); borders.bottom = new CT_Border(); CT_Border b = borders.bottom; b.val = XWPFTable.xwpfBorderTypeMap[type]; b.sz = (ulong)size; b.space = (ulong)space; b.color = (rgbColor); }
private static XWPFTableCell SetCell(XWPFTableCell cell, string text, int width, string color = "", bool isCenter = true, int fontSize = 0, bool isBold = false) { CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctpr = cttc.AddNewTcPr(); //垂直居中 ctpr.AddNewVAlign().val = ST_VerticalJc.center; //设置单元格列宽 ctpr.tcW = new CT_TblWidth(); ctpr.tcW.w = width.ToString(); ctpr.tcW.type = ST_TblWidth.dxa; //设置单元格背景色 if (!string.IsNullOrEmpty(color)) { cell.SetColor(color); } //设置文本 text = text == "PRI" ? "Y" : text; text = text == "YES" ? "Y" : text; text = text == "NO" ? "N" : text; text = text == "CURRENT_TIMESTAMP" ? "now()" : text; cell.RemoveParagraph(0); XWPFParagraph gp = cell.AddParagraph(); if (isCenter) { //水平居中 gp.Alignment = ParagraphAlignment.CENTER; } //文字格式 gp.Style = "NoSpacing"; XWPFRun gr = gp.CreateRun(); if (fontSize > 0) { gr.FontSize = fontSize; } gr.IsBold = isBold; gr.SetText(text); return(cell); }
public void GenerateWord(List <SpInfo> spInfos) { XWPFDocument doc = new XWPFDocument(); for (int i = 0; i < spInfos.Count; i++) { XWPFTable table = doc.CreateTable(); var firstRow = table.GetRow(0); firstRow.GetCell(0).SetText("預存程序名稱"); firstRow.AddNewTableCell().SetText(spInfos[i].SpName); CT_TcPr m_Pr = firstRow.GetCell(0).GetCTTc().AddNewTcPr(); m_Pr = Set(m_Pr); var secondRow = table.CreateRow(); secondRow.GetCell(0).SetText("建立日期"); secondRow.GetCell(1).SetText($"{spInfos[i].CreateDate:yyyy/MM/dd}"); var thirdRow = table.CreateRow(); thirdRow.GetCell(0).SetText("修改日期"); thirdRow.GetCell(1).SetText($"{spInfos[i].ModifyDate:yyyy/MM/dd}"); var forthRow = table.CreateRow(); forthRow.GetCell(0).SetText("說明"); forthRow.GetCell(1).SetText(spInfos[i].Description); var fifthRow = table.CreateRow(); fifthRow.GetCell(0).SetText("Script"); fifthRow.GetCell(1).SetText(spInfos[i].Script); doc.CreateParagraph(); } // 產出文件 // path可以自己換 using (FileStream fileStream = new FileStream(@"D:\\temp\\SpDoc.docx", FileMode.Create)) { doc.Write(fileStream); } }
/** * Get the vertical alignment of the cell. * @return the cell alignment enum value or null if no vertical alignment is set */ public XWPFVertAlign?GetVerticalAlignment() { XWPFVertAlign?vAlign = null; CT_TcPr tcpr = ctTc.tcPr; if (tcpr != null) { CT_VerticalJc va = tcpr.vAlign; if (va != null) { vAlign = stVertAlignTypeMap[va.val.Value]; } else { vAlign = XWPFVertAlign.TOP; } if (va != null && va.val != null) { vAlign = stVertAlignTypeMap[va.val.Value]; } } return(vAlign); }
/// <summary> /// 文档添加Table /// </summary> /// <param name="doc">文档</param> /// <param name="title">表头</param> /// <param name="dics">标列信息:key:字段名-value:列名</param> /// <param name="dt">数据源</param> protected virtual void DocAddTable(XWPFDocument doc, string title, Dictionary <string, string> dics, DataTable dt) { var rowCount = dt.Rows.Count; var colCount = dics.Count; //创建表格 var table = doc.CreateTable(1, 1); table.Width = 4900; //表头 var tCell = table.GetRow(0).GetCell(0); var tCtt = tCell.GetCTTc(); CT_TcPr ctPr = tCtt.AddNewTcPr(); ctPr.gridSpan = new CT_DecimalNumber { val = colCount.ToString() }; //合并列 tCtt.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; var tCpr = tCell.Paragraphs[0].CreateRun(); tCpr.IsBold = true; tCpr.SetText(title); //填充内容 for (int n = 0; n < rowCount + 1; n++) { var tr = table.CreateRow();//创建行时默认创建一个单元格 //标题 for (int g = 0; g < colCount; g++) { var dic = dics.ElementAt(g); var text = n == 0 ? dic.Value : dic.Key == "Null" ? "" : dt.Rows[n - 1][dic.Key].ToString();//Null,不展示数据 var td = g == 0 ? tr.GetCell(0) : tr.CreateCell(); td.SetText(text); } } doc.CreateParagraph();//回车 }
private ReturnMessage WriteDoc() { ReturnMessage retMsg = new ReturnMessage(string.Empty, true); FileStream fs = null; try { XWPFDocument doc = new XWPFDocument(); XWPFTable table = null; int index = 1; foreach (DataRow dr in dtInfo.Rows) { if (dr["表名"] != DBNull.Value && !string.IsNullOrEmpty(dr["表名"].ToString())) { //表名,以段落表示 CT_P ctp = doc.Document.body.AddNewP(); //XWPFParagraph p = doc.CreateParagraph(); XWPFParagraph p = new XWPFParagraph(ctp, doc); XWPFRun r = p.CreateRun(); //设置字体 r.GetCTR().AddNewRPr().AddNewRFonts().ascii = "宋体"; r.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "宋体"; r.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia; r.GetCTR().AddNewRPr().AddNewSz().val = (ulong)32;//3号字体; r.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)32; //设置行间距 //单倍为默认值(240twip)不需设置,1.5倍=240X1.5=360twip,2倍=240X2=480twip ctp.AddNewPPr().AddNewSpacing().line = "720"; //ctp.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact; //设置段落文本 r.SetText(index.ToString() + "." + dr["表名"].ToString()); //表结构,以表格显示 CT_Tbl m_CTTbl = doc.Document.body.AddNewTbl(); table = doc.CreateTable(1, 9); //标题行(固定) //列宽 CT_TcPr mPr = table.GetRow(0).GetCell(0).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "900"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "1500"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(2).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "500"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(3).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "1000"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(4).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "500"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(6).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "900"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(7).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "800"; mPr.tcW.type = ST_TblWidth.dxa; mPr = table.GetRow(0).GetCell(8).GetCTTc().AddNewTcPr(); mPr.tcW = new CT_TblWidth(); mPr.tcW.w = "1500"; mPr.tcW.type = ST_TblWidth.dxa; //填充文字 table.GetRow(0).GetCell(0).SetText("字段序号"); table.GetRow(0).GetCell(1).SetText("字段名"); table.GetRow(0).GetCell(2).SetText("主键"); table.GetRow(0).GetCell(3).SetText("类型"); table.GetRow(0).GetCell(4).SetText("长度"); table.GetRow(0).GetCell(5).SetText("精度"); table.GetRow(0).GetCell(6).SetText("小数位数"); table.GetRow(0).GetCell(7).SetText("允许空"); table.GetRow(0).GetCell(8).SetText("字段说明"); //内容行 XWPFTableRow row = table.CreateRow(); row.GetCell(0).SetText(dr["字段序号"].ToString()); row.GetCell(1).SetText(dr["字段名"].ToString()); row.GetCell(2).SetText(dr["主键"].ToString()); row.GetCell(3).SetText(dr["类型"].ToString()); row.GetCell(4).SetText(dr["长度"].ToString()); row.GetCell(5).SetText(dr["精度"].ToString()); row.GetCell(6).SetText(dr["小数位数"].ToString()); row.GetCell(7).SetText(dr["允许空"].ToString()); row.GetCell(8).SetText(""); // index++; } else { if (table != null) { //内容行 XWPFTableRow row = table.CreateRow(); row.GetCell(0).SetText(dr["字段序号"].ToString()); row.GetCell(1).SetText(dr["字段名"].ToString()); row.GetCell(2).SetText(dr["主键"].ToString()); row.GetCell(3).SetText(dr["类型"].ToString()); row.GetCell(4).SetText(dr["长度"].ToString()); row.GetCell(5).SetText(dr["精度"].ToString()); row.GetCell(6).SetText(dr["小数位数"].ToString()); row.GetCell(7).SetText(dr["允许空"].ToString()); row.GetCell(8).SetText(""); } } } //输出保存 string docAllPath = Application.StartupPath + "\\SqlDBDicFile.docx"; if (File.Exists(docAllPath)) { File.Delete(docAllPath); } fs = File.OpenWrite(docAllPath); doc.Write(fs); doc.Close(); return(retMsg); } catch (Exception ex) { retMsg.isSuccess = false; retMsg.Messages = ex.Message; return(retMsg); } finally { fs.Close(); fs.Dispose(); } }
public CT_Style() { this.tblStylePrField = new List<CT_TblStylePr>(); this.tcPrField = new CT_TcPr(); this.trPrField = new CT_TrPr(); this.tblPrField = new CT_TblPrBase(); this.rPrField = new CT_RPr(); this.pPrField = new CT_PPr(); this.rsidField = new CT_LongHexNumber(); this.personalReplyField = new CT_OnOff(); this.personalComposeField = new CT_OnOff(); this.personalField = new CT_OnOff(); this.lockedField = new CT_OnOff(); this.qFormatField = new CT_OnOff(); this.unhideWhenUsedField = new CT_OnOff(); this.semiHiddenField = new CT_OnOff(); this.uiPriorityField = new CT_DecimalNumber(); this.hiddenField = new CT_OnOff(); this.autoRedefineField = new CT_OnOff(); this.linkField = new CT_String(); this.nextField = new CT_String(); this.basedOnField = new CT_String(); this.aliasesField = new CT_String(); this.nameField = new CT_String(); }
private static void word_inster_table(XWPFDocument m_Docx, OutputData bean, int i, String photoPathName) { XWPFTable table = m_Docx.CreateTable(12, 2); CT_Tbl ctbl = m_Docx.Document.body.GetTblArray()[i]; CT_TblPr ctblpr = ctbl.AddNewTblPr(); ctblpr.jc = new CT_Jc(); ctblpr.jc.val = ST_Jc.center; table.Width = 3500; table.GetRow(0).GetCell(0).SetText("设备类型"); table.GetRow(0).GetCell(1).SetText(bean.DeviceType); table.GetRow(1).GetCell(0).SetText("公里标"); table.GetRow(1).GetCell(1).SetText(bean.KilometerMark); table.GetRow(2).GetCell(0).SetText("下行侧向"); table.GetRow(2).GetCell(1).SetText(bean.SideDirection); table.GetRow(3).GetCell(0).SetText("距线路中心距离(m)"); table.GetRow(4).GetCell(0).SetText("经度"); table.GetRow(4).GetCell(1).SetText(bean.Longitude); table.GetRow(5).GetCell(0).SetText("纬度"); table.GetRow(5).GetCell(1).SetText(bean.Latitude); table.GetRow(6).GetCell(0).SetText("杆塔类型"); table.GetRow(6).GetCell(1).SetText(bean.TowerType); table.GetRow(7).GetCell(0).SetText("杆塔高度"); table.GetRow(7).GetCell(1).SetText(bean.TowerHeight); table.GetRow(8).GetCell(0).SetText("天线1方向角"); table.GetRow(8).GetCell(1).SetText(bean.AntennaDirection1); table.GetRow(9).GetCell(0).SetText("天线2方向角"); table.GetRow(9).GetCell(1).SetText(bean.AntennaDirection2); table.GetRow(10).GetCell(0).SetText("天线3方向角"); table.GetRow(10).GetCell(1).SetText(bean.AntennaDirection3); table.GetRow(11).GetCell(0).SetText("天线4方向角"); table.GetRow(11).GetCell(1).SetText(bean.AntennaDirection4); CT_TcPr m_Pr = table.GetRow(2).GetCell(1).GetCTTc().AddNewTcPr(); m_Pr.tcW = new CT_TblWidth(); m_Pr.tcW.w = "3500"; m_Pr.tcW.type = ST_TblWidth.dxa; //设置单元格宽度 XWPFTableRow m_Row = table.InsertNewTableRow(0); XWPFTableCell cell = m_Row.CreateCell(); CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); ctPr.gridSpan = new CT_DecimalNumber(); ctPr.gridSpan.val = "2"; cttc.GetPList()[0].AddNewR().AddNewT().Value = "SITE: " + bean.MarkerId; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(photoPathName); System.IO.FileInfo[] files = dir.GetFiles(); foreach (System.IO.FileInfo file in files) { FileStream gfs = new FileStream(photoPathName + "\\" + file.Name, FileMode.Open, FileAccess.Read); Image image = Image.FromFile(photoPathName + "\\" + file.Name); Double ratio = (Double)image.Width / (Double)image.Height; image.Dispose(); XWPFParagraph gp = m_Docx.CreateParagraph(); gp.SetAlignment(ParagraphAlignment.CENTER); XWPFRun gr = gp.CreateRun(); if (ratio > 1) { gr.AddPicture(gfs, (int)PictureType.JPEG, file.Name, 3555556, 2000000); } else { gr.AddPicture(gfs, (int)PictureType.JPEG, file.Name, 2000000, 3555556); } gfs.Close(); } word_insert_space(3, m_Docx, 100); }
protected void Button1_Click(object sender, EventArgs e) { //图片位置 String m_PicPath = @"C:\ttt.jpeg"; FileStream gfs = null; MemoryStream ms = new MemoryStream(); XWPFDocument m_Docx = new XWPFDocument(); //页面设置 //A4:W=11906,h=16838 //CT_SectPr m_SectPr = m_Docx.Document.body.AddNewSectPr(); m_Docx.Document.body.sectPr = new CT_SectPr(); CT_SectPr m_SectPr = m_Docx.Document.body.sectPr; //页面设置A4纵向 m_SectPr.pgSz.h = (ulong)16838; m_SectPr.pgSz.w = (ulong)11906; XWPFParagraph gp = m_Docx.CreateParagraph(); // gp.GetCTPPr().AddNewJc().val = ST_Jc.center; //水平居中 XWPFRun gr = gp.CreateRun(); gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = "黑体"; gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体"; gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia; gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)44; //2号字体 gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)44; gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗 gr.GetCTR().AddNewRPr().AddNewColor().val = "red"; //字体颜色 gr.SetText("NPOI创建Word2007Docx"); gp = m_Docx.CreateParagraph(); //gp.GetCTPPr().AddNewJc().val = ST_Jc.both; // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); CT_RPr rpr = gr.GetCTR().AddNewRPr(); CT_Fonts rfonts = rpr.AddNewRFonts(); rfonts.ascii = "宋体"; rfonts.eastAsia = "宋体"; rpr.AddNewSz().val = (ulong)21;//5号字体 rpr.AddNewSzCs().val = (ulong)21; gr.SetText("NPOI,顾名思义,就是POI的.NET版本。那POI又是什么呢?POI是一套用Java写成的库,能够帮助开 发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等 。目前POI的稳定版本中支持Excel文件格式(xls和xlsx),其他的都属于不稳定版本(放在poi的scrachpad目录 中)。"); //创建表 XWPFTable table = m_Docx.CreateTable(1, 4); //创建一行4列表 CT_Tbl m_CTTbl = m_Docx.Document.body.GetTblArray()[0]; //获得文档第一张表 CT_TblPr m_CTTblPr = m_CTTbl.AddNewTblPr(); m_CTTblPr.AddNewTblW().w = "2000"; //表宽 m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa; m_CTTblPr.tblpPr = new CT_TblPPr(); //表定位 m_CTTblPr.tblpPr.tblpX = "4003"; //表左上角坐标 m_CTTblPr.tblpPr.tblpY = "365"; m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center; //若不为“Null”,则优先tblpX,即表由tblpXSpec定位 m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.center; //若不为“Null”,则优先tblpY,即表由tblpYSpec定位 m_CTTblPr.tblpPr.leftFromText = (ulong)180; m_CTTblPr.tblpPr.rightFromText = (ulong)180; m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text; m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page; //表1行4列充值:a,b,c,d table.GetRow(0).GetCell(0).SetText("a"); table.GetRow(0).GetCell(1).SetText("b"); table.GetRow(0).GetCell(2).SetText("c"); table.GetRow(0).GetCell(3).SetText("d"); CT_Row m_NewRow = new CT_Row();//创建1行 XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); //必须要!!! XWPFTableCell cell = m_Row.CreateCell(); //创建单元格,也创建了一个CT_P CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); //ctPr.gridSpan.val = "3";//合并3列 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "666"; cell = m_Row.CreateCell();//创建单元格,也创建了一个CT_P cell.GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cell.GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "e"; //合并3列,合并2行 //1行 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); //第1单元格 cell.SetText("f"); cell = m_Row.CreateCell(); //从第2单元格开始合并 cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); // ctPr.gridSpan.val = "3";//合并3列 ctPr.AddNewVMerge().val = ST_Merge.restart; //开始合并行 ctPr.AddNewVAlign().val = ST_VerticalJc.center; //垂直居中 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "777"; //2行 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); //第1单元格 cell.SetText("g"); cell = m_Row.CreateCell(); //第2单元格 cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); // ctPr.gridSpan.val = "3";//合并3列 ctPr.AddNewVMerge().val = ST_Merge.@continue;//继续合并行 //表插入图片 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell();//第1单元格 //inline方式插入图片 //gp = table.GetRow(table.Rows.Count - 1).GetCell(0).GetParagraph(table.GetRow(table.Rows.Count - 1).GetCell(0).GetCTTc().GetPList()[0]);//获得指定表单元格的段 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件 gr.AddPicture(gfs, (int)PictureType.PNG, "1.jpg", 500000, 500000); //插入图片 gfs.Close(); //Anchor方式插入图片 CT_Anchor an = new CT_Anchor(); an.distB = (uint)(0); an.distL = 114300u; an.distR = 114300U; an.distT = 0U; an.relativeHeight = 251658240u; an.behindDoc = false; //"0" an.locked = false; //"0" an.layoutInCell = true; //"1" an.allowOverlap = true; //"1" NPOI.OpenXmlFormats.Dml.CT_Point2D simplePos = new NPOI.OpenXmlFormats.Dml.CT_Point2D(); simplePos.x = (long)0; simplePos.y = (long)0; CT_EffectExtent effectExtent = new CT_EffectExtent(); effectExtent.b = 0L; effectExtent.l = 0L; effectExtent.r = 0L; effectExtent.t = 0L; //wrapSquare(四周) cell = m_Row.CreateCell(); //第2单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run CT_WrapSquare wrapSquare = new CT_WrapSquare(); wrapSquare.wrapText = ST_WrapText.bothSides; gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 // gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); //wrapTight(紧密) cell = m_Row.CreateCell(); //第3单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run CT_WrapTight wrapTight = new CT_WrapTight(); wrapTight.wrapText = ST_WrapText.bothSides; wrapTight.wrapPolygon = new CT_WrapPath(); wrapTight.wrapPolygon.edited = false; wrapTight.wrapPolygon.start = new CT_Point2D(); wrapTight.wrapPolygon.start.x = 0; wrapTight.wrapPolygon.start.y = 0; CT_Point2D lineTo = new CT_Point2D(); wrapTight.wrapPolygon.lineTo = new List <CT_Point2D>(); lineTo = new CT_Point2D(); lineTo.x = 0; lineTo.y = 1343; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 1343; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 0; wrapTight.wrapPolygon.lineTo.Add(lineTo); lineTo.x = 0; lineTo.y = 0; wrapTight.wrapPolygon.lineTo.Add(lineTo); gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 //gr.AddPicture(gfs, (int)PictureType.PNG, "1.png", 500000, 500000, 0, 0, wrapTight, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); //wrapThrough(穿越) cell = m_Row.CreateCell(); //第4单元格 gp = cell.GetParagraph(cell.GetCTTc().GetPList()[0]); gr = gp.CreateRun(); //创建run gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read); //读取图片文件 CT_WrapThrough wrapThrough = new CT_WrapThrough(); wrapThrough.wrapText = ST_WrapText.bothSides; wrapThrough.wrapPolygon = new CT_WrapPath(); wrapThrough.wrapPolygon.edited = false; wrapThrough.wrapPolygon.start = new CT_Point2D(); wrapThrough.wrapPolygon.start.x = 0; wrapThrough.wrapPolygon.start.y = 0; lineTo = new CT_Point2D(); wrapThrough.wrapPolygon.lineTo = new List <CT_Point2D>(); lineTo = new CT_Point2D(); lineTo.x = 0; lineTo.y = 1343; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 1343; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo = new CT_Point2D(); lineTo.x = 21405; lineTo.y = 0; wrapThrough.wrapPolygon.lineTo.Add(lineTo); lineTo.x = 0; lineTo.y = 0; wrapThrough.wrapPolygon.lineTo.Add(lineTo); // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 0, 0, wrapThrough, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); gp = m_Docx.CreateParagraph(); //gp.GetCTPPr().AddNewJc().val = ST_Jc.both; // gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); gr.SetText("NPOI是POI项目的.NET版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。使用NPOI你就可以在没有安装Office或者相应环境的机器上对WORD/EXCEL文档进行读写。NPOI是构建在POI3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。"); gp = m_Docx.CreateParagraph(); // gp.GetCTPPr().AddNewJc().val = ST_Jc.both; //gp.IndentationFirstLine = Indentation("宋体", 21, 2, FontStyle.Regular);//段首行缩进2字符 gp.IndentationFirstLine = 15; gr = gp.CreateRun(); gr.SetText("NPOI之所以强大,并不是因为它支持导出Excel,而是因为它支持导入Excel,并能“理解”OLE2文档结构,这也是其他一些Excel读写库比较弱的方面。通常,读入并理解结构远比导出来得复杂,因为导入你必须假设一切情况都是可能的,而生成你只要保证满足你自己需求就可以了,如果把导入需求和生成需求比做两个集合,那么生成需求通常都是导入需求的子集。"); //在本段中插图-wrapSquare //gr = gp.CreateRun();//创建run wrapSquare = new CT_WrapSquare(); wrapSquare.wrapText = ST_WrapText.bothSides; gfs = new FileStream(m_PicPath, FileMode.Open, FileAccess.Read);//读取图片文件 // gr.AddPicture(gfs, (int)PictureType.PNG, "15.png", 500000, 500000, 900000, 200000, wrapSquare, an, simplePos, ST_RelFromH.column, ST_RelFromV.paragraph, effectExtent); gr.AddPicture(gfs, (int)PictureType.PNG, "虚拟机", 500000, 500000); gfs.Close(); m_Docx.Write(ms); ms.Flush(); SaveToFile(ms, Path.GetPathRoot(Directory.GetCurrentDirectory()) + "\\NPOI.docx"); }
private void NPOITestFillData(string file) { #region 读取Word XWPFDocument doc; using (FileStream fileread = File.OpenRead(file)) { doc = new XWPFDocument(fileread); } #endregion List <string>[] data = new List <string> [3]; #region 组织填充数据 List <string> a = new List <string>(); List <string> b = new List <string>(); List <string> c = new List <string>(); a.Add("1.1"); a.Add("1.2"); a.Add("1.3"); a.Add("1.4"); a.Add("1.5"); a.Add("1.6"); a.Add("1.7"); b.Add("2.1"); b.Add("2.2"); b.Add("2.3"); b.Add("2.4"); b.Add("2.5"); b.Add("2.6"); c.Add("3.1"); c.Add("3.2"); c.Add("3.3"); c.Add("3.4"); c.Add("3.5"); c.Add("3.6"); c.Add("3.7"); c.Add("3.8"); c.Add("3.9"); data[0] = a; data[1] = b; data[2] = c; #endregion WordTable wt = new WordTable(data); wt.CaptionLineCount = 2; //标题行数 XWPFTable table = LocationTable(doc, "本年发生的非同一控制下企业合并情况"); if (wt.ObjectData != null) { for (int i = 0; i < wt.ObjectData.Length + wt.CaptionLineCount; i++) { if (i >= wt.CaptionLineCount) { XWPFTableRow row; string[] rowdata = wt.ObjectData[i - wt.CaptionLineCount].ToArray <string>(); if (i < table.Rows.Count) { row = table.GetRow(i); row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = 397; row.GetCTRow().trPr.GetTrHeightArray(0); for (int n = 0; n < rowdata.Length; n++) { XWPFTableCell cell = row.GetCell(n); //模板中的单元格少时,接收数据将会部分丢失 //也可以在下边if后添加else在该行后补充单元格 //按接收数据循环,所以单元格数多于接收数据时不需要另做处理,该行后边的部分单元格无法补填充 if (cell != null) { //SetText是追加内容,所以要先删除单元格内容(删除单元格内所有段落)再写入 for (int p = 0; p < cell.Paragraphs.Count; p++) { cell.RemoveParagraph(p); } for (int t = 0; t < cell.Tables.Count; t++) { //表格删除 //cell.RemoveTable(t); } cell.SetText(rowdata[n]); } } } else { //添加新行 //row = table.InsertNewTableRow(table.Rows.Count - 1); row = new XWPFTableRow(new CT_Row(), table); row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = 100; table.AddRow(row); for (int n = 0; n < rowdata.Length; n++) { XWPFTableCell cell = row.CreateCell(); CT_Tc tc = cell.GetCTTc(); CT_TcPr pr = tc.AddNewTcPr(); tc.GetPList()[0].AddNewR().AddNewT().Value = rowdata[n]; } } } } } #region 保存Word FileStream filewrite = new FileStream(file, FileMode.Create, FileAccess.Write); try { doc.Write(filewrite); } catch (Exception x) { lblStatus.Text = x.Message; } finally { filewrite.Close(); } #endregion }
//创建“评估结果”表格表头部分 void CreateAssessmentResultHeaderAndSetValue(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); StartMergeRow(cttc, ctPr, "评估结果"); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "评估项目"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "评估结论"); SetAlign(cell); cell = m_Row.CreateCell(); cell.SetText(""); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "建议"); SetAlign(cell); }
/// <summary> /// 创建评估结果单元格 /// </summary> /// <param name="table">评估表格</param> void CreateAssessmentResultsCell(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { //评估结果表头 CreateAssessmentResultHeaderAndSetValue(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); //创建位移评估结果单元格 CreateDisplacementAssessmentResultCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); //创建应力评估结果单元格 CreateStressAssessmentResultCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); //创建索力评估结果单元格 CreateCableForceAssessmentResultCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); }
/// <summary> /// 对合并单元格进行前处理 /// </summary> /// <param name="table"></param> /// <param name="m_NewRow"></param> /// <param name="m_Row"></param> /// <param name="cell"></param> /// <param name="cttc"></param> /// <param name="ctPr"></param> static void DealWithMergeCell(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); }
void CreateExceptionRecordsCell(ReportDownloadModel Datas, XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); StartMergeRow(cttc, ctPr, "异常记录"); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "检测类型"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "测点编号"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "测点位置"); SetAlign(cell); cell = m_Row.CreateCell(); SetBoldFontCell(cell, "异常次数"); SetAlign(cell); for (int i = 1; i <= Datas.ExceptionRecordNumber; i++) { m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行 //横向创建4个单元格 for (int k = 1; k < 5; k++) { cell = m_Row.CreateCell(); SetAlign(cell); } } }
//创建评估报告索力评估结果单元格 void CreateCableForceAssessmentResultCell(XWPFTable table, out CT_Row m_NewRow, out XWPFTableRow m_Row, out XWPFTableCell cell, out CT_Tc cttc, out CT_TcPr ctPr) { DealWithMergeCell(table, out m_NewRow, out m_Row, out cell, out cttc, out ctPr); ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行 cell = m_Row.CreateCell(); SetBoldFontCell(cell, "吊杆及系杆索力评估"); SetAlign(cell); for (int i = 1; i < 4; i++) { cell = m_Row.CreateCell(); SetAlign(cell); } }